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();
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With