Can't get past Specified cast is not valid error

N

.Net Sports

I have a datagrid codebehind script that takes data from sql dbase and
displays it in a footer row as a total. One column has amount_dollars
(which works fine), while another has new sales (which is "newstotal"),
but I'm getting an error (System.InvalidCastException: Specified cast
is not valid) from the newstotal line:

price = (Decimal)(rowData["Amount_Dollars"]);
newstotal = (rowData["News"]);

newstotal is an integer, just wondering what datatype i should use
(whereas 'price' is a money datatype, and I can format it as Decimal,
but I just want the newstotal to be a simple integer. )Using (Integer)
or (short) in front of (rowData["News"]); give me the Specified cast
not valid error.

????
..netsports
 
R

RvGrah

I'm no expert, but have you tried:
newstotal = Convert.ToInt32(rowData["News"]);

Bob Graham
 
D

Derek Harmon

.Net Sports said:
newstotal = (rowData["News"]);
newstotal is an integer, just wondering what datatype i should use

Try asking it and/or printing a message in a debug window,

System.Diagnostics.Debug.WriteLineIf( ( rowData["News"] != null ), rowData["News"].GetType( ).FullName );

If its a string (even if it's a string containing a number, C# is still
strict about these things), then Parse( ) it,

newstotal = int.Parse( rowData[ "News"]);

Note that this'll throw a FormatException if the contents of the
string aren't really a number. If that can happen you could use
Double.TryParse( ) with some flags to be a little more accom-
modating of currency signs or thousands separators appearing
in the string and then cast the double result to an integer.


Derek Harmon
 
B

Bruce Wood

Don't forget that rowData["News"] could be DBNull.Value, if the value
in the database is null, in which none of the solutions here will work.
You should always test for that, first.

After that, I would try Derek's suggestion to display the data type.
That will tell you the best type to which to cast. (Have you tried
long?)
 
B

Bruce Wood

As a further idea, I wrote a method for each data type:

public int DBValueToInt(object rowValue, int defaultValue)
{
if (rowValue.Equals(DBNull.Value))
{
return defaultValue;
}
else
{
return (int)rowValue;
}
}

then I always say:

int newstotal = DataHandler.DBValueToInt(rowData["News"], 0);

which takes care of the DBNull case without writing tons of code. I
have similar methods for decimal, string, boolean, etc.
 
S

Steve Walker

Bruce said:
As a further idea, I wrote a method for each data type:

public int DBValueToInt(object rowValue, int defaultValue)
{
if (rowValue.Equals(DBNull.Value))
{
return defaultValue;
}
else
{
return (int)rowValue;
}
}

then I always say:

int newstotal = DataHandler.DBValueToInt(rowData["News"], 0);

which takes care of the DBNull case without writing tons of code. I
have similar methods for decimal, string, boolean, etc.

Similarly, I have a SafeRowWrapper class.

public Foo(DataRow dr)
{
SafeRowWrapper sr = new SafeRowWrapper(dr);
this.id=sr.GetIntID("ID");
this.title=sr.GetString("Title");
this.description=sr.GetString("Description");
}

It handles DBNulls and also manages certain conventions I use (i.e.
GetInt("Total") will return 0 if total == DBNull, GetIntID("CurrencyID")
will return -1 for CurrencyID == DBNull. The point of this convention is
this:

class Currency
{
......
public static Currency GetInstanceByID(int id)
{
if(id == -1)
{
return Currency.Empty;
}
.....
}
}
 

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

Top