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
27 2892
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
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,
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"
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
which adds an array of objects into an arraylist. Then you have
which won't work because you're now trying to cast an object array to an int.
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,
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[].
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
Since you are only returning a single value, using getValues() is a waste. -
cmd4.CommandText = "select id from dpt" ;
-
cmd4.ExecuteNonQuery();
-
SqlDataReader reader = cmd4.ExecuteReader();
-
while(reader.Read())
-
{
-
int temp=reader.GetInt32(1);
-
arrm.Add(temp);
-
}
-
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;
-
}
-
-
}
-
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.
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);
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)?
I am not sure how to do CSS. so, here is what I have. - cmd4.CommandText = "select id from dpt" ;
- SqlDataReader reader = cmd4.ExecuteReader();
- int temp=reader.GetInt32(1);
- cmcmd4.Connection.Close();
- for (int i = 0;i<arrm.Count;i++)
- int min = (int) arram[i];
I am not sure how to do CSS. so, here is what I have. - cmd4.CommandText = "select id from dpt" ;
- SqlDataReader reader = cmd4.ExecuteReader();
- int temp=reader.GetInt32(1);
- cmcmd4.Connection.Close();
- for (int i = 0;i<arrm.Count;i++)
- int min = (int) arram[i];
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].
Ok. I got it, here it is: - cmd4.CommandText = "select id from dpt" ;
-
cmd4.ExecuteNonQuery();
-
SqlDataReader reader = cmd4.ExecuteReader();
-
while(reader.Read())
-
{
-
int temp=reader.GetInt32(1);
-
arrm.Add(temp);
-
}
-
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,
Try making it:
int temp=reader.GetInt32(0);
I wasn't sure if it was zero one one based index.
Hi Plater,
then it is back to original error: specified cast is invalid.
line: int temp=reader.GetInt32(0);
Thanks,
Ok how SURE are you that your ID column is an int? -
int temp=-1;
-
try
-
{
-
temp=int.Parse(reader.GetValue(0).ToString());
-
}
-
catch(Exception ee)
-
{//if you make it here, your first column is NOT of datatype int
-
}
-
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,
That SQL statement looked good, what kind of error are you getting?
query is in C# page along with other code.
and I am getting Invalid Expression on % sign.
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 - +DateTime.Now.ToString("yymm") " + "%"
Hi R,
No , that did not work either.
Thanks,
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? -
string sql="Select paperid from Allreport where paperid like ' "+DateTime.Now.ToString("yymm")+"%' ";
-
I have value in id field but the query doesn't return any value.
No error but doesn't give any result.
Thanks,
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? -
Select paperid from Allreport where paperid like '0709%'
-
if that statement in your sql query window returns no results then you don't have matching data.
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,
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.
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,
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |