473,804 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

specified cast is invalid

109 New Member
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
27 2927
r035198x
13,262 MVP
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.Get Int32(1);
Can you post what you have now(using code=css tags ofcourse)?
Sep 14 '07 #11
arial
109 New Member
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 MVP
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 New Member
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 Recognized Expert Expert
Try making it:
int temp=reader.Get Int32(0);

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

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

line: int temp=reader.Get Int32(0);

Thanks,
Sep 14 '07 #16
Plater
7,872 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
That SQL statement looked good, what kind of error are you getting?
Sep 14 '07 #19
arial
109 New Member
query is in C# page along with other code.

and I am getting Invalid Expression on % sign.
Sep 14 '07 #20

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

Similar topics

4
1313
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 request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. The stack trace is just a lot of system class stuff with no real explanation of where the error is. Wondering the...
10
1795
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 the code. Exception Details: System.InvalidCastException: Specified cast is not valid. Line 176. Source Error:
5
3446
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
3
10521
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 columns is a template column where I'd like to evaluate the data brought back from the db and populate the column with a "Y" or "N" depending on the value in the db. Here is the code that is causing it as well as the error (below). ERROR...
6
6426
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 AtRiskCode, String Comment) Code for UpdateProject(String Project, String AtRiskCode, String Comment) is:
2
1628
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 start off the connection: Public Sub Main() Const PortNumber As Integer = 8022 Dim tcpListener As New TcpListener(PortNumber) tcpListener.Start()
0
1396
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 web application to use SQL Server to store the Membership database using aspnet_regsql.exe and have successfully opened the Web Config Tool. Then, I create a role, and go look in the SQL Server aspnet_roles table, and there is the role. All...
2
3532
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 current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Specified cast is not valid. Source Error: Line 44: while...
1
1397
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 Access with a table "Affiliation". Both contain four columns/fields. I am writing an application to pull some data from Table1's affiliation table and inserting it into Table2's affiliation table. The datatypes are all the same for all four fields...
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9593
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.