473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

specified cast is invalid

109 100+
Hi

I am new to programming. learning c# , asp.net.

write a simple program: to get the id from database and save it to arraylist to find the max(id).

and getting exception: Specified cast is invalid.

at line: int low = (int)arrm[i];

Appreciate any help.

Thanks
Sep 12 '07 #1
27 2892
Shashi Sadasivan
1,435 Expert 1GB
Hi

I am new to programming. learning c# , asp.net.

write a simple program: to get the id from database and save it to arraylist to find the max(id).

and getting exception: Specified cast is invalid.

at line: int low = (int)arrm[i];

Appreciate any help.

Thanks
is arrm your table or the row?
if it is the table then arrm[i] is a row,
maybe what you want is
Expand|Select|Wrap|Line Numbers
  1. int i = (int)arrm[i][j];
Sep 12 '07 #2
arial
109 100+
arram is a object of my array list.

Arraylist arrm = new Arraylist();

and here is part of my script:

cmd4.CommandText = "select id from dpt" ;
cmd4.ExecuteNonQuery();
SqlDataReader reader = cmd4.ExecuteReader();
while(reader.Read())
{
values = new object[reader.FieldCount];
reader.GetValues(values);
arrm.Add(values);
}
arrm.Sort();
reader.Close();
cmd4.Connection.Close();

int max = 0;
for (int i = 0; i < arrm.Count; i++)
{
int min = (int)arrm[i];
if (max < min)
max = min;
}

}

Thanks,
Sep 13 '07 #3
Plater
7,872 Expert 4TB
You could use the .GetType().Name on the arrm[i] to see what type the value is actually being returned as.

I would guess that if ID is defined as say "bigint" in the database, you would need to cast it to "Int64" and not just "int"
Sep 13 '07 #4
r035198x
13,262 8TB
arram is a object of my array list.

Arraylist arrm = new Arraylist();

and here is part of my script:

cmd4.CommandText = "select id from dpt" ;
cmd4.ExecuteNonQuery();
SqlDataReader reader = cmd4.ExecuteReader();
while(reader.Read())
{
values = new object[reader.FieldCount];
reader.GetValues(values);
arrm.Add(values);
}
arrm.Sort();
reader.Close();
cmd4.Connection.Close();

int max = 0;
for (int i = 0; i < arrm.Count; i++)
{
int min = (int)arrm[i];
if (max < min)
max = min;
}

}

Thanks,
1.) Please use code tags when posting code.
2.) You have
Expand|Select|Wrap|Line Numbers
  1. arrm.Add(values);
which adds an array of objects into an arraylist. Then you have
Expand|Select|Wrap|Line Numbers
  1.              int min = (int)arrm[i];
which won't work because you're now trying to cast an object array to an int.
Sep 13 '07 #5
arial
109 100+
ID is varchar field in sql database.

and .Getvalue().Name returns arraylist.

What I want to do is, find a max ID from table. I also tried Convert.ToInt32(arrm[i]), but still no luck on getting what I want.

Thanks,
Sep 13 '07 #6
Plater
7,872 Expert 4TB
There are SQL statements that will return to you the largest one. Google some SQL examples about it.

What r0 said was right (I missed it, good catch on his part) you are trying to cast an array of objects to an intiger.

And it's GetType().Name, which is available for everything and will identify what the object type is.
You don't need it now that it's been identified as an object[].
Sep 13 '07 #7
arial
109 100+
Yes, I know sql statement will give me largest value but I need to use simple
select id from dpt and store this query result into array and find a max because I am going to use this array to do some other stuff.

If I can successfully get my first part right get max value and then it would be easy do the other parts.

so, is there a way to convert object type to int?

Thanks
Sep 13 '07 #8
Plater
7,872 Expert 4TB
Since you are only returning a single value, using getValues() is a waste.

Expand|Select|Wrap|Line Numbers
  1. cmd4.CommandText = "select id from dpt" ; 
  2. cmd4.ExecuteNonQuery();
  3. SqlDataReader reader = cmd4.ExecuteReader();
  4. while(reader.Read())
  5. {
  6. int temp=reader.GetInt32(1);
  7. arrm.Add(temp);
  8. }
  9. arrm.Sort();
  10. reader.Close();
  11. cmd4.Connection.Close();
  12.  
  13. int max = 0;
  14. for (int i = 0; i < arrm.Count; i++)
  15. {
  16. int min = (int)arrm[i];
  17. if (max < min)
  18. max = min;
  19. }
  20.  
  21. }
  22.  
This might not be exactly right as I can't currently check it in a project, but hopefully it will make more sense as now your (int)arrm[i] shouldn't throw an exception.
Sep 14 '07 #9
arial
109 100+
Thanks Plater for all your help.

By after making a changes you suggested Now I am getting new error:

Index was outside the bounds of the array.

at line: int temp=reader.GetInt32(1);
Sep 14 '07 #10
r035198x
13,262 8TB
Thanks Plater for all your help.

By after making a changes you suggested Now I am getting new error:

Index was outside the bounds of the array.

at line: int temp=reader.GetInt32(1);
Can you post what you have now(using code=css tags ofcourse)?
Sep 14 '07 #11
arial
109 100+
I am not sure how to do CSS. so, here is what I have.

Expand|Select|Wrap|Line Numbers
  1. cmd4.CommandText = "select id from dpt" ; 
Expand|Select|Wrap|Line Numbers
  1. cmd4.ExecuteNonQuery();
Expand|Select|Wrap|Line Numbers
  1. SqlDataReader reader = cmd4.ExecuteReader();
Expand|Select|Wrap|Line Numbers
  1. while(reader.Read())
Expand|Select|Wrap|Line Numbers
  1. {
Expand|Select|Wrap|Line Numbers
  1. int temp=reader.GetInt32(1);
Expand|Select|Wrap|Line Numbers
  1. arrm.Add(temp);}
Expand|Select|Wrap|Line Numbers
  1. reader.Close();
Expand|Select|Wrap|Line Numbers
  1. cmcmd4.Connection.Close();
Expand|Select|Wrap|Line Numbers
  1. int max = 0;
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0;i<arrm.Count;i++)
Expand|Select|Wrap|Line Numbers
  1. {
Expand|Select|Wrap|Line Numbers
  1. int min = (int) arram[i];
Expand|Select|Wrap|Line Numbers
  1. if (max < min)
Expand|Select|Wrap|Line Numbers
  1. max = min;
Expand|Select|Wrap|Line Numbers
  1. }
Sep 14 '07 #12
r035198x
13,262 8TB
I am not sure how to do CSS. so, here is what I have.

Expand|Select|Wrap|Line Numbers
  1. cmd4.CommandText = "select id from dpt" ; 
Expand|Select|Wrap|Line Numbers
  1. cmd4.ExecuteNonQuery();
Expand|Select|Wrap|Line Numbers
  1. SqlDataReader reader = cmd4.ExecuteReader();
Expand|Select|Wrap|Line Numbers
  1. while(reader.Read())
Expand|Select|Wrap|Line Numbers
  1. {
Expand|Select|Wrap|Line Numbers
  1. int temp=reader.GetInt32(1);
Expand|Select|Wrap|Line Numbers
  1. arrm.Add(temp);}
Expand|Select|Wrap|Line Numbers
  1. reader.Close();
Expand|Select|Wrap|Line Numbers
  1. cmcmd4.Connection.Close();
Expand|Select|Wrap|Line Numbers
  1. int max = 0;
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0;i<arrm.Count;i++)
Expand|Select|Wrap|Line Numbers
  1. {
Expand|Select|Wrap|Line Numbers
  1. int min = (int) arram[i];
Expand|Select|Wrap|Line Numbers
  1. if (max < min)
Expand|Select|Wrap|Line Numbers
  1. max = min;
Expand|Select|Wrap|Line Numbers
  1. }
No that's not what I meant. I meant that you wrap all the code in one set of code tags not broken up like that and then when you have all the code wrapped in one set of code tags, you add =css to the opening code tag so that your opening code tag looks like this
[code=css].
Sep 14 '07 #13
arial
109 100+
Ok. I got it, here it is:

Expand|Select|Wrap|Line Numbers
  1.  cmd4.CommandText = "select id from dpt" ; 
  2. cmd4.ExecuteNonQuery();
  3. SqlDataReader reader = cmd4.ExecuteReader();
  4. while(reader.Read())
  5. {
  6. int temp=reader.GetInt32(1);
  7. arrm.Add(temp);
  8. }
  9. arrm.Sort();
  10. reader.Close();
  11. cmd4.Connection.Close();
  12.  
  13. int max = 0;
  14. for (int i = 0; i < arrm.Count; i++)
  15. {
  16. int min = (int)arrm[i];
  17. if (max < min)
  18. max = min;
  19. }
Thanks,
Sep 14 '07 #14
Plater
7,872 Expert 4TB
Try making it:
int temp=reader.GetInt32(0);

I wasn't sure if it was zero one one based index.
Sep 14 '07 #15
arial
109 100+
Hi Plater,

then it is back to original error: specified cast is invalid.

line: int temp=reader.GetInt32(0);

Thanks,
Sep 14 '07 #16
Plater
7,872 Expert 4TB
Ok how SURE are you that your ID column is an int?

Expand|Select|Wrap|Line Numbers
  1. int temp=-1;
  2. try
  3. {
  4. temp=int.Parse(reader.GetValue(0).ToString());
  5. }
  6. catch(Exception ee)
  7. {//if you make it here, your first column is NOT of datatype int
  8. }
  9.  
Sep 14 '07 #17
arial
109 100+
Hi Plater ,

Thank so very much for all your help.

my first column is id and it is numeric datatype in sql database.

with your help I was able to solve my problem.

But Now, I got one more little issue. which is I am trying to do a query with like clause and i think I am not getting write syntax.

Need help with that please.

Select paperid from Allreport where paperid like ' "+DateTime.Now.ToString("yymm")+%" ' ;

my paperid will be for this month. 070901,070902 depending on as many time report was modified by user and I like to get the all paperid for a perticular month.

thank you,
Sep 14 '07 #18
Plater
7,872 Expert 4TB
That SQL statement looked good, what kind of error are you getting?
Sep 14 '07 #19
arial
109 100+
query is in C# page along with other code.

and I am getting Invalid Expression on % sign.
Sep 14 '07 #20
r035198x
13,262 8TB
Hi Plater ,

Thank so very much for all your help.

my first column is id and it is numeric datatype in sql database.

with your help I was able to solve my problem.

But Now, I got one more little issue. which is I am trying to do a query with like clause and i think I am not getting write syntax.

Need help with that please.

Select paperid from Allreport where paperid like ' "+DateTime.Now.ToString("yymm")+%" ' ;

my paperid will be for this month. 070901,070902 depending on as many time report was modified by user and I like to get the all paperid for a perticular month.

thank you,
Try writing it as
Expand|Select|Wrap|Line Numbers
  1. +DateTime.Now.ToString("yymm") " + "%"
Sep 15 '07 #21
arial
109 100+
Hi R,

No , that did not work either.

Thanks,
Sep 17 '07 #22
Plater
7,872 Expert 4TB
Hi R,

No , that did not work either.

Thanks,
What is it telling you now then?
"it doesn't work" is not a very helpful reply

This didn't work?
Expand|Select|Wrap|Line Numbers
  1. string sql="Select paperid from Allreport where paperid like ' "+DateTime.Now.ToString("yymm")+"%' ";
  2.  
Sep 17 '07 #23
arial
109 100+
I have value in id field but the query doesn't return any value.

No error but doesn't give any result.

Thanks,
Sep 17 '07 #24
Plater
7,872 Expert 4TB
Have you tried your query's in the sql query window for your sql server (ie not in code) too make sure they return something?

Expand|Select|Wrap|Line Numbers
  1. Select paperid from Allreport where paperid like '0709%'
  2.  
if that statement in your sql query window returns no results then you don't have matching data.
Sep 17 '07 #25
arial
109 100+
Ok, I saw the problem.

paperid is Numeric datafield in sql table. So, when value hits the database it removes the "0 " from front.

Ex , 070901 hits database it goes for 70901..

that's why query wasn't giving any result.

Any idea on how to have paperid store in database like 070901 and not 70901?

Thanks,
Sep 17 '07 #26
r035198x
13,262 8TB
Ok, I saw the problem.

paperid is Numeric datafield in sql table. So, when value hits the database it removes the "0 " from front.

Ex , 070901 hits database it goes for 70901..

that's why query wasn't giving any result.

Any idea on how to have paperid store in database like 070901 and not 70901?

Thanks,
The table design was wrong then.
Change the table column type to varchar2 whatever type that your database uses for strings.
Sep 18 '07 #27
arial
109 100+
Thanks but then I need to increment id by1 each time the page will modify. If I will change the id to varchar then I need to store that query result some like in array or something where then convert to "Int".

How can I accomplish this? How can I store my query into array?

Thanks,
Sep 18 '07 #28

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: .Net Sports | last post by:
I'm getting a "Specified cast is not valid" error that does not point to the line with the code in error, but instead :An unhandled exception was generated during the execution of the current web...
10
by: Arjen | last post by:
Hello, Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my...
6
by: Biva | last post by:
Hello All, Please help. I get the following error from my webpage: System.InvalidCastException: Specified cast is not valid. at Time.MaintAtRiskProjects.UpdateProject(String Project, String...
2
by: James | last post by:
I'm a newbie to vb.net, and classes were something I never played with in "standard" vb, but... Basically,part of my program comprises of some tcp/ip stuff, and I used the following code to...
0
by: df | last post by:
I'm seeing a strange problem in the Web App config tool: After creating a role and a user, going back to the Security tab home page, I get a "Specified cast is not valid" error. I've set up my...
2
by: Kashiefah | last post by:
Hi, I keep on receiving this error when I click on the edit email link from the datagrid:Specified cast is not valid. Description: An unhandled exception occurred during the execution of the...
1
by: one320b | last post by:
I've read through numerous postings by other users here but was not able to find the solution to my issue. It's probably a simple one, but it's racking me at this point. I have two databases in MS...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.