Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant update table from text using linq2sql

Using linq2sql I'm trying to take the string in txtOilChange and update the oilChange integer in the car table of the white fusion.

I know my code below is wrong but what do I need to change?

using (DataClasses1DataContext db = new DataClasses1DataContext())
{
var o = (from c in db.cars
where c.carDesc == "White Fusion"
select c).First();

txtOilChange.Text = o.oilChange.ToString();

db.SubmitChanges();
}
like image 385
Sealer_05 Avatar asked Jun 18 '26 23:06

Sealer_05


1 Answers

If you're trying to update the record it looks like the assignment statement is reversed.

This:

txtOilChange.Text = o.oilChange.ToString();

Should be:

o.oilChange = int.Parse(txtOilChange.Text);

For better error handling consider using the TryParse method:

int oilChangeValue;
if (int.TryParse(txtOilChange.Text, out oilChangeValue))
{
    o.oilChange = oilChangeValue;
    db.SubmitChanges();
} 
else
{
    // invalid value
}
like image 183
Ahmad Mageed Avatar answered Jun 20 '26 13:06

Ahmad Mageed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!