Linq to Sql insert fails

B

Bill McCormick

I have a Linq to Sql application where record inserts are not working.
There's no exception or error message and I can't find a reason from SQL
Express logs.

It looks line this:

Trk trk = cmbTruckId.SelectedItem as Trk;
TTare tare = new TTare();

tare.TTareSerial = trk.TrkSerial;
tare.TTareTrkHaulerId = trk.TrkHaulerId;
tare.TTareTrkId = trk.TrkId;
tare.TTareWeight = double.Parse(txtTareWeight.Text.ToString(),
NumberStyles.Float);
tare.TTareTrkMaxCap = trk.TrkMaxCap;
tare.TTareTrkMaxLegal = trk.TrkMaxLegal;
tare.TTareDateTime = System.DateTime.Now;

trk.TTares.Add(tare);

_db.SubmitChanges();

The TTare table has 2 more non-nullable fields that should autogenerate:

TTareSerial
TTareTimeStamp

Any advice?

TIA Bill
 
B

Bill McCormick

I have a Linq to Sql application where record inserts are not working.
There's no exception or error message and I can't find a reason from SQL
Express logs.

It looks line this:

Trk trk = cmbTruckId.SelectedItem as Trk;
TTare tare = new TTare();

tare.TTareSerial = trk.TrkSerial;
tare.TTareTrkHaulerId = trk.TrkHaulerId;
tare.TTareTrkId = trk.TrkId;
tare.TTareWeight = double.Parse(txtTareWeight.Text.ToString(),
NumberStyles.Float);
tare.TTareTrkMaxCap = trk.TrkMaxCap;
tare.TTareTrkMaxLegal = trk.TrkMaxLegal;
tare.TTareDateTime = System.DateTime.Now;

trk.TTares.Add(tare);

_db.SubmitChanges();

The TTare table has 2 more non-nullable fields that should autogenerate:

TTareSerial
TTareTimeStamp

I guess I want to do this:

_db.TTares.InsertOnSubmit(tare);

instead of

trk.TTares.Add(tare);

But then, I can I get the UI to update to see the change?
 
V

vanderghast

Can't you try with MS SQL Sever Studio to append a row by setting the fields
you give. Try the SQL statement:


INSERT INTO tare (TTareSerial, TTareTrkHaulerId, TTareTrkId, TTareWeight,
TTareTrkMaxCap, TTareTrkMaxLegal, TTareDateTime ) VALUES( ... list of your
values here ... )



You may get, there, the explicit reason why the append is not accepted
(duplicated value, DRI not verified, not nullable column without default
value, ... )

Sure, if the account you use in your C# app does not allow appending data
.... that is just yet another reason to not get the data appended.

Also, if you are in a transaction, until you commit it, outside this
transaction, you won't see the data appended, EVEN if it is appended.


One incremental step, if the previous command is executed without problem in
MS SQL Server Studio, is to try the statement, in C#, with a command object,
created from the connection you use. If it fails, then probably, it is your
connection settings.




Vanderghast, Access MVP
 
B

Bill McCormick

Can't you try with MS SQL Sever Studio to append a row by setting the
fields you give. Try the SQL statement:

I got to work; see my previous post.

Now the problem is getting WPF UI TextBlock binding to refresh to see the
change. I'll make another post here in a bit if I can't get it.


Thanks,

Bill
 
B

Bill McCormick

I guess I want to do this:

_db.TTares.InsertOnSubmit(tare);

instead of

trk.TTares.Add(tare);

But then, I can I get the UI to update to see the change?

Hmmm, actually do need to use trk.TTares.Add(tare) in order to get Binding
updates. See final post in thread *WPF Linq to Sql Binding Update*.
 
Top