var existingUserCount = db.customers.Count(x =>x.Firstname == Firstname);if (existingUserCount == 0) { // Do your inserttry { Customer newCustomer = new customer(); newCustomer .Firstname = Firstname; newCustomer.Telephone = Telephone; newCustomer.Location = Location;db.Customers.InsertOnSubmit(newCustomer); db.SubmitChanges(); return newCustomer; } catch (Exception) { throw; }}else{// here is the code to get to know the Username is already exist in the databasereturn null;}
Mark This Response as Answer -- Chandu http://www.dotnetfunda.com/images/dnfmvp.gif
If EXISTS ( SELECT UserName FROM tableName WHERE UserName = @PassedUserName )BEGIN SELECT 'Record EXIST'ENDELSEBEGIN INSERT INTO TableName ...END
CREATE PROCEDURE USP_Insert (@UserName VARCHAR(40))AS BEGINIf NOT EXISTS ( SELECT UserName FROM tableName WHERE UserName = @UserName ) BEGIN INSERT INTO TableName VALUES(@UserName) END ELSE BEGIN SELECT 'Record EXIST' END END
Mark as answer if satisfied
var usersCnt = (from u in Users where u.username == "chandu" select u).count()if ( usersCnt != 1){ //Insert code}
bool doesUserExist = myContext.Users.Any(u=>u.Username == "bob" && u.Password=="mypassword");if ( !doesUserExist){ //Insert}
var existingUserCount = publishContext.Users.Count(a => a.username == "chandu"); if (existingUserCount == 0) { // Do your insert }
SAMIR Sr. Software Engineer
Login to post response