3
Answers

C# not update table correctly

Ask a question
Sie Ste

Sie Ste

12y
1.3k
1
I am having a problem using linq to sql in a C# desktop application that connects to  sql server 2008. Basically if the CustId already exists in the Trans table, I do not want to insert a new row into the table. However the code listed below does not work.
**Note the  CustId is not the primary key of the table. there is an identity key that is the primary key of the table.  
eDataContext rptData = new eDataContext();
                        var eupdate = (from a in rptData.Tran
                                          where a.Cust_ID == CustId
                                          select a).SingleOrDefault();
                        if (eupdate != null)
                        {
                            log.Info(CustId + "already exists in the Trans table");
                        }
                        else
                        {
                            Trans subCustid = null;
                            subCustid = new Trans();
                            subCustid.Package_ID = pkgid;
                            rptData.Trans.InsertOnSubmit(subCustid);
                            rptData.SubmitChanges();
                           }
Thus can you tell me what I can do to not allow a row to be inserted into the Trans table when the CustId already exists in the table?

Answers (3)