473,387 Members | 1,542 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,387 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 3491
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...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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,...
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.