how to remove System.InvalidCastException error?
-
hai all, i am using .net 2005.i am doing my project in asp.net using c#.for my project back end is MS ACCESS 97. the following code is com = new OleDbCommand("select HolderNo,HolderName,DepartmentNo,mid(Appsetdata," + bb + ",2),JobTitle from holderdata where DepartmentNo <> '0000'", con); dr = com.ExecuteReader(); i1 = 0; while (dr.Read()) { holno[i1] = dr.GetString(0); holname[i1] = dr.GetString(1); dept[i1] = dr.GetString(2); appset_holder[i1] = Convert.ToInt16(dr.GetString(3)); jobtitle[i1] = dr.GetString(4); i1++; } dr.Close(); in the above code shown the error in jobtitle[i1] = dr.GetString(4); the following error were display, System.InvalidCastException: Specified cast is not valid. at System.Data.OleDb.ColumnBinding.ValueString() at System.Data.OleDb.OleDbDataReader.GetString(Int32 ordinal) at _Default.emp_iterate() in c:\Documents and Settings\sabari\My Documents\Visual Studio 2005\Projects\styrisact1\Default.aspx.cs:line 182 at _Default.validate_main() in c:\Documents and Settings\sabari\My Documents\Visual Studio 2005\Projects\styrisact1\Default.aspx.cs:line 126 at _Default.SubmitImageButton_Click(Object sender, ImageClickEventArgs e) in c:\Documents and Settings\sabari\My Documents\Visual Studio 2005\Projects\styrisact1\Default.aspx.cs:line 101 how to solve the above error?thanks to all in advance.
-
hai all, i am using .net 2005.i am doing my project in asp.net using c#.for my project back end is MS ACCESS 97. the following code is com = new OleDbCommand("select HolderNo,HolderName,DepartmentNo,mid(Appsetdata," + bb + ",2),JobTitle from holderdata where DepartmentNo <> '0000'", con); dr = com.ExecuteReader(); i1 = 0; while (dr.Read()) { holno[i1] = dr.GetString(0); holname[i1] = dr.GetString(1); dept[i1] = dr.GetString(2); appset_holder[i1] = Convert.ToInt16(dr.GetString(3)); jobtitle[i1] = dr.GetString(4); i1++; } dr.Close(); in the above code shown the error in jobtitle[i1] = dr.GetString(4); the following error were display, System.InvalidCastException: Specified cast is not valid. at System.Data.OleDb.ColumnBinding.ValueString() at System.Data.OleDb.OleDbDataReader.GetString(Int32 ordinal) at _Default.emp_iterate() in c:\Documents and Settings\sabari\My Documents\Visual Studio 2005\Projects\styrisact1\Default.aspx.cs:line 182 at _Default.validate_main() in c:\Documents and Settings\sabari\My Documents\Visual Studio 2005\Projects\styrisact1\Default.aspx.cs:line 126 at _Default.SubmitImageButton_Click(Object sender, ImageClickEventArgs e) in c:\Documents and Settings\sabari\My Documents\Visual Studio 2005\Projects\styrisact1\Default.aspx.cs:line 101 how to solve the above error?thanks to all in advance.
According to the error text, I think there may be a problem with appset_holder[i1] = Convert.ToInt16(dr.GetString(3)) this line of code. Maybe the value returning from database is null or empty and you are trying to convert it to Int16. Check the value in DB. Hope this helps.