Connecting Tech Pros Worldwide Forums | Help | Site Map

i am new to linq to sql

Member
 
Join Date: May 2007
Posts: 80
#1: Jun 26 '09
Hi friends

i am using Linq to sql
and my code is

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class AccountSettings
  3.     {
  4.        NewCustomerDataContext _dbAccounts = new NewCustomerDataContext();
  5.  
  6.  
  7.        public int UpdateAccountSettigs(int iUserLoginID,string strUserMailID, string strPassword)
  8.        {
  9.  
  10.            var UpdateUser = _dbAccounts.UserLoginInfs.Single(e => e.UserLoginInfID == iUserLoginID);
  11.  
  12.  
  13.  
  14.            UpdateUser.UserMailID = strUserMailID;
  15.            UpdateUser.Password = strPassword;
  16.            _dbAccounts.SubmitChanges();
  17.  
  18.  
  19.  
  20.  
  21.  
  22.        }
  23.     }
  24.  

at the code

var UpdateUser = _dbAccounts.UserLoginInfs.Single(e => e.UserLoginInfID == iUserLoginID);

if no records are there with that criteria i am getting error.
so how do i validate this?

With earlier technology, when no records are found, we used to get 0 or something.

but how do we handle it in this case


also after executing
_dbAccounts.SubmitChanges();
how do i confirm that the update is success?
its return type is void
so how do i capture the result


thank you
Ravindar

insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#2: Jun 26 '09

re: i am new to linq to sql


There are two ways of going about this.
You could use a try/catch:

Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.   var UpdateUser = _dbAccounts.UserLoginInfs.Single(e => e.UserLoginInfID == iUserLoginID);
  4. }
  5. catch (Exception ex) //change this to the exact exception thrown
  6. {
  7.   //exception handling here
  8. }
Or you could do a count before you do your select:
Expand|Select|Wrap|Line Numbers
  1. var UpdateUser;
  2. if(_dbAccounts.UserLoginInfs.Count(e => e.UserLoginInfID == iUserLoginID) > 0)
  3.   UpdateUser = _dbAccounts.UserLoginInfs.Single(e => e.UserLoginInfID == iUserLoginID);
  4. else
  5.   //exception handling here
As to the last question, it will throw an error if unsuccessful. So you should probably catch that error and do something if it fails.
Reply