473,289 Members | 1,952 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,289 software developers and data experts.

Specified Cast Invalid Exception

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 (myReader.Read())
Line 45: {
Line 46: myEmail.id = myReader.GetInt32(0);
Line 47: myEmail.Title = myReader.GetString(1);
Line 48: myEmail.emailType = myReader.GetInt32(2);


Source File: c:\inetpub\wwwroot\cibt\datalayer\clsemail.cs Line: 46

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
System.Data.SqlClient.SqlDataReader.GetSqlInt32(In t32 i)
System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
CIBT.dataLayer.clsEmail.getDetails() in c:\inetpub\wwwroot\cibt\datalayer\clsemail.cs:46
CIBT.Administration.EditEmail.populateEmail() in c:\inetpub\wwwroot\cibt\administration\editemail.a spx.cs:65
CIBT.Administration.EditEmail.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\cibt\administration\editemail.a spx.cs:54
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()


Here is the code where the error occurs:public CIBT.businessLayer.clsEmail getDetails()
{
SqlConnection dbCon = new SqlConnection(System.Configuration.ConfigurationSe ttings.AppSettings["dbCon"]);
SqlCommand dbComm = new SqlCommand("sp_get_email_details",dbCon);
dbComm.CommandType = CommandType.StoredProcedure ;

dbComm.Parameters.Add("@id",SqlDbType.Int);

dbComm.Parameters[0].Value = myEmail.id;

//procedure sp_get_company_details
//@id int

dbCon.Open();
SqlDataReader myReader = dbComm.ExecuteReader(CommandBehavior.CloseConnecti on);

while (myReader.Read())
{
myEmail.id = myReader.GetInt32(0); myEmail.Title = myReader.GetString(1);
myEmail.emailType = myReader.GetInt32(2);
myEmail.Subject = myReader.GetString(3);
myEmail.Body = myReader.GetString(4);
myEmail.Notes = myReader.GetString(5);

}

return myEmail;
}
Oct 19 '06 #1
2 3487
r035198x
13,262 8TB
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 (myReader.Read())
Line 45: {
Line 46: myEmail.id = myReader.GetInt32(0);
Line 47: myEmail.Title = myReader.GetString(1);
Line 48: myEmail.emailType = myReader.GetInt32(2);


Source File: c:\inetpub\wwwroot\cibt\datalayer\clsemail.cs Line: 46

Stack Trace:


[InvalidCastException: Specified cast is not valid.]
System.Data.SqlClient.SqlDataReader.GetSqlInt32(In t32 i)
System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
CIBT.dataLayer.clsEmail.getDetails() in c:\inetpub\wwwroot\cibt\datalayer\clsemail.cs:46
CIBT.Administration.EditEmail.populateEmail() in c:\inetpub\wwwroot\cibt\administration\editemail.a spx.cs:65
CIBT.Administration.EditEmail.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\cibt\administration\editemail.a spx.cs:54
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()


Here is the code where the error occurs:public CIBT.businessLayer.clsEmail getDetails()
{
SqlConnection dbCon = new SqlConnection(System.Configuration.ConfigurationSe ttings.AppSettings["dbCon"]);
SqlCommand dbComm = new SqlCommand("sp_get_email_details",dbCon);
dbComm.CommandType = CommandType.StoredProcedure ;

dbComm.Parameters.Add("@id",SqlDbType.Int);

dbComm.Parameters[0].Value = myEmail.id;

//procedure sp_get_company_details
//@id int

dbCon.Open();
SqlDataReader myReader = dbComm.ExecuteReader(CommandBehavior.CloseConnecti on);

while (myReader.Read())
{
myEmail.id = myReader.GetInt32(0); myEmail.Title = myReader.GetString(1);
myEmail.emailType = myReader.GetInt32(2);
myEmail.Subject = myReader.GetString(3);
myEmail.Body = myReader.GetString(4);
myEmail.Notes = myReader.GetString(5);

}

return myEmail;
}
Apparently the email Id is not of type Int32.
Oct 19 '06 #2
at the datareader use getvalue instead of get(typeofvalue) and check if the values are empty.
Oct 26 '10 #3

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

Similar topics

0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
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: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
1
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm...
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
6
by: John-Arne Lillebø | last post by:
Hi. I run into this problem and i could need some help to solve it. The project is an ASP.NET Web project. Including code sample of the problem. Any idea what is causing the error message ?...
27
by: arial | last post by:
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.