Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

there is run time exception
Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.String'.



how can i correct below code to get my result?
C#
ProductsDataContext db = new ProductsDataContext(); 
var matchedproduct = db.GetTable<product>().SingleOrDefault(p =>p.ProductID==productID);
 if (matchedproduct != null)  
{             
        products product=new products();
product.ProductName = txtpname.Text;
db.SubmitChanges()};
Posted
Updated 12-May-12 23:52pm
v8
Comments
wizardzz 7-May-12 14:51pm    
What is the actual problem, do you get an error?
itbehrooz 11-May-12 6:10am    
no error, but not updating
Cagri Kaynar 13-May-12 6:44am    
what is your goal?
adding a new product or update

Your data passed to your data context is a file on your local machine. Even if this works, you will end up with issues once you move it off your machine.

Typically there is a connection string created in your app.config or your web.config when you build your linq to sql context. You need to find the name of this connection string then reference THAT name in the creation of your data context.

So if your configuration name is "MyDatabase" then that is what you will pass in the constructor.
 
Share this answer
 
you get the product named matchedproduct
but you are trying to update product
why dont you try
C#
matchedproduct.ProductName = txtpname.Text;
db.SubmitChanges(); 
 
Share this answer
 
sql compact is not supported for linq to sql.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900