By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,213 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

Specified cast is not valid - please help

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
AW
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.
--
To reply, remove a "l" before the @ sign.

AW - MCT, MCSD.Net, MCAD.Net
Nov 17 '05 #2

P: n/a
"Tyro" <lo******@yahoo.com> wrote in message
news:24*************************@posting.google.co m...
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?
--
John
Nov 17 '05 #3

P: n/a
>
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;
}
Nov 17 '05 #4

P: n/a
AW
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.
--
To reply, remove a "l" before the @ sign.

Arnaud Weil - MCT, MCSD.Net, MCAD.Net
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.