2
votes

I am getting this runtime exception

Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.String'.

How can I fix my code to get my result without exceptions?

ProductsDataContext db = new ProductsDataContext(); 

var matchedproduct = db.GetTable<product>().SingleOrDefault(p =>p.ProductID==productID);

if (matchedproduct != null)               
   product.ProductName = txtpname.Text;

db.SubmitChanges();
1

1 Answers

3
votes

If you aren't getting a compile time error its because your dbml doesn't accurately depict the column in your database. Your object thinks its a string but its clearly a decimal in the database. You should update it in the dbml editor. Then when you set product name you will have to parse out the decimal value from the Text.