Specified cast is not valid - please help

  • Thread starter Thread starter Tyro
  • Start date Start date
T

Tyro

Can someone shed some light on my error here? Thanks!

Specified cast is not valid.

Exception Details: System.InvalidCastException: Specified cast is not
valid.

Source Error:

Stack Trace:

[InvalidCastException: Specified cast is not valid.]
milo.data.Storer.GetString(SqlDataReader reader, Type dataType,
String attributeName) in c:\inetpub\wwwroot\milo.data\storer.cs:572
milo.web.TextBox.SetValue(SqlDataReader reader) in
C:\Inetpub\wwwroot\milo.web\TextBox.cs:33
milo.data.Storer.LoadData(String filter) in
c:\inetpub\wwwroot\milo.data\storer.cs:393
milo.web.Storer.LoadData(String filter) in
c:\inetpub\wwwroot\milo.web\storer.cs:63
cms.user.information.Page_Load(Object sender, EventArgs e) in
C:\cms\user\information.aspx.cs:96
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
 
Your application is making an invalid cast, for instance:
int a =3;
string s;
s=(string)a;

If you want more info, post the code of your milo.data.Storer.GetString()
method in storer.cs.
 
Tyro said:
Can someone shed some light on my error here? Thanks!

Specified cast is not valid.

Exception Details: System.InvalidCastException: Specified cast is not
valid.

Source Error:

Stack Trace:

[InvalidCastException: Specified cast is not valid.]
milo.data.Storer.GetString(SqlDataReader reader, Type dataType,
String attributeName) in c:\inetpub\wwwroot\milo.data\storer.cs:572

Is it possible that the field you're retrieving is NULL?
 
If you want more info, post the code of your milo.data.Storer.GetString()
method in storer.cs.

public static string GetString(SqlDataReader reader, milo.data.Type
dataType, string attributeName)
{
int index = reader.GetOrdinal(attributeName);

if (reader.IsDBNull(index)==false)
{
try
{
if (dataType == milo.data.Type.STRING)
{
return (string)reader[attributeName];
}
else
{
switch((int)dataType)
{
case (int)milo.data.Type.INT_16:
return reader.GetInt16(index).ToString();
case (int)milo.data.Type.INT_32:
return reader.GetInt32(index).ToString();
case (int)milo.data.Type.INT_64:
return reader.GetInt64(index).ToString();
case (int)milo.data.Type.DECIMAL:
return reader.GetDecimal(index).ToString();
case (int)milo.data.Type.DOUBLE:
return reader.GetDouble(index).ToString();
case (int)milo.data.Type.FLOAT:
return reader.GetFloat(index).ToString();
case (int)milo.data.Type.BOOLEAN:
return reader.GetBoolean(index).ToString();
/* case (int)milo.data.Type.CHARS:
return reader.GetChars(index).ToString();*/
case (int)milo.data.Type.BYTE:
return reader.GetByte(index).ToString();
/* case (int)milo.data.Type.BYTES:
return reader.GetBytes(index).ToString();*/
}
}
}
catch(System.Data.SqlTypes.SqlNullValueException)
{
return null;
}
}
else
{
return "";
}

return null;
}
 
Hi Tyro,

There are potentially several lines for your error in your code. To begin
with, if your milo.data.Type is an enum like I suspect, don't cast it to an
int in your switch. Write something like:
switch(dataType)
{
case milo.data.Type.INT_16:
return reader.GetInt16(index).ToString();
case milo.data.Type.INT_32:

The error may come from another line. I suggest that you switch to debug
mode and reach the line where the exception is raised.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top