tags:

views:

719

answers:

2

Hi,

I have some code like this:

Function GetTypeFromTableName(ByVal _TableName As String, ByVal _DataContext As DataContext)

            Dim Mytype As Type = (From t In _DataContext.Mapping.GetTables Where t.TableName = "dbo." + _TableName Select t.RowType.Type).SingleOrDefault
            Return Mytype

    End Function

 Dim DBA As New LINQDataContext
_TBLName="City"
 TableType = GetTypeFromTableName(_TBLName, DBA)

                CallByName(obj, "Code", CallType.Set,1)

                Dim Equery = From T In DBA.GetTable(TableType) Select T

                Equery = Equery.Where(Function(Oj1) Oj1 Is obj)

                Dim oopp = From t In Equery Select  CallByName(t, "CName", CallType.Get, Nothing)
                oopp.ToList.Item(0) = Txt_Name.Text
                DBA.SubmitChanges()

SubmitChanges do not work. What is wrong?

+2  A: 

It looks like to me that you are selecting out a list of anonymous types, then changing the value in the list of anonymous types not in the matching object in the table. Since you haven't updated the table object itself, SubmitChanges has nothing to do.

Try changing it to the following

  Dim matchingObj = Equery.Where( Function(Oj1) Oj1 Is obj )
                          .SingleOrDefault();

Then set the property value.

  CallByName( matchingObj, "CName", CallType.Set, Txt_Name.Text );

Then submit your changes.

Note: I read VB ok, but I don't write it so well. You may need to fix up my syntax. You might also want to make sure that there is a matching object before you attempt to set it's property.

tvanfosson
thanks a lot about your answer.
This just solved my problem, thanks!
Nick Gotch
A: 

This is old but thought i will come and clear this up.

Whenever your update or Submitchanges fails try puttin InsertOnSubmit() statment. This statment will give you detailed error.

in my case I had very simple code.

        var single = dataContext.MySysTables.FirstOrDefault();
        single.DispatchIdIndex ++;
        single.ModifyDate = DateTime.Now;
        dataContext.SubmitChanges();

This was not being updated. When I put a InsertOnSubmit(single) line than it gave me error that "data can not be inserted because table does not have Primary key". This was my test table to i didnt have primary key. Once I added a new column and made a primary key than everything worked fine.

peace

Ved