IndianaTech
asked on
System.InvalidCastException: Specified cast is not valid, Microsoft Visual Studio, C#
I am writing a program which runs a stored procedure to return the data, then output the content to a CSV file. When running my stored procedure, I get all the values back. However, when I run the program, it stops what iit's doing and gives me the following error:
************** Exception Text **************
System.InvalidCastExceptio n: Specified cast is not valid.
I took a look at the CSV file and it populated 23 out of the 57 lines before it gives me this error message. After doing some testing, I took out the following line and it worked successfully:
Decimal decDiscount = (Decimal)rowA["Discount"];
Can someone please help me solve this issue? I'm not sure what i'm doing wrong with the "Discount" column of the Stored Procedure. The "Total" column has no problems, but its only the "Discount" column. Both columns are the same data type of "money".
************** Exception Text **************
System.InvalidCastExceptio
I took a look at the CSV file and it populated 23 out of the 57 lines before it gives me this error message. After doing some testing, I took out the following line and it worked successfully:
Decimal decDiscount = (Decimal)rowA["Discount"];
Can someone please help me solve this issue? I'm not sure what i'm doing wrong with the "Discount" column of the Stored Procedure. The "Total" column has no problems, but its only the "Discount" column. Both columns are the same data type of "money".
this.spTableAdapter.Fill(this.twOLTPDDataSet.spSproc);
DataView data = new DataView(twOLTPDDataSet.spSproc);
//Begin each row cycle
foreach (DataRowView rowA in currMinorData)
{
string strCustomer = (string)rowA["Customer"];
string strName = (string)rowA["Name"];
Decimal decTotal = (Decimal)rowA["Total"];
string strTotal = decTotal.ToString();
Decimal decDiscount = (Decimal)rowA["Discount"];
string strDiscount = decDiscount.ToString();
Write Comma Delimiter to the file
CorporateSpending.WriteLine(strCustomer + "," + strName + "," + strTotal + "," + strDiscount);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
add what Tech Suggested with the
if(rowA["Discount"] != DbNull.Value)
strDiscount = Convert.ToDecimal(rowA["Di scount"]). ToString() ;
wait why are you converting to decimal and then to string?
what about
String strDiscount = String.Format("{0}:c", rowA["Discount"]);
if(rowA["Discount"] != DbNull.Value)
strDiscount = Convert.ToDecimal(rowA["Di
wait why are you converting to decimal and then to string?
what about
String strDiscount = String.Format("{0}:c", rowA["Discount"]);
sorry that would be...
String strDiscount = String.Format("{1:c}", rowA["Discount"]);
String strDiscount = String.Format("{1:c}", rowA["Discount"]);
ASKER
@p_davis: Have a problem with the DbNull.Value.
When building it, i got an error that says:
"The name 'DbNull' doe snot exist in the current context."
When building it, i got an error that says:
"The name 'DbNull' doe snot exist in the current context."
sorry
DBNull
DBNull
ASKER
and I am getting a different error back this time:
************** Exception Text **************
System.InvalidCastExceptio
at System.DBNull.System.IConv
at System.Convert.ToDecimal(O
************** Exception Text **************
I looked at the results of the CSV file and it has 22 lines in the file. I looked at Query Analyzer and the 23rd row doesn't have any Null values in it. I'm not too sure why it would be telling me it cannot cast from DBNull to other types.
Open in new window