checking duplicate rows using linq while inserting data.........
-
Hi, I am using LINQ for the first time.I have written a code in LINQ that will insert employees details in to the database. Now the problem is that i do not want to insert duplicate data(employee details) in to the database. How do i check duplicate data using LINQ before new inserting data. I want to check the first name and last name of employee in table before inserting new data.If the record(e.g first name and last name) exist then it will show some message.. The following is the code that i am using to insert data in to the table....
EmployeesDetailsDataContext DetailContext = new EmployeesDetailsDataContext();
Employee tblemp = new Employee();
tblemp.Emp_Code = txtEmpId.Text.Trim();
tblemp.F_Name = txtFname.Text.Trim();
tblemp.L_Name = txtLname.Text.Trim();
tblemp.Country = Convert.ToInt32(ddlCountry.SelectedValue);
tblemp.State = Convert.ToInt32(ddlRegion.SelectedValue);
tblemp.City = Convert.ToInt32(ddlCity.SelectedValue);
tblemp.Designation = txtDes.Text.Trim();
tblemp.Address = txtAdd.Text.Trim();
DetailContext.Employees.InsertOnSubmit(tblemp);
DetailContext.SubmitChanges();any help would be appreciate
Regards, Pranav Dave
-
Hi, I am using LINQ for the first time.I have written a code in LINQ that will insert employees details in to the database. Now the problem is that i do not want to insert duplicate data(employee details) in to the database. How do i check duplicate data using LINQ before new inserting data. I want to check the first name and last name of employee in table before inserting new data.If the record(e.g first name and last name) exist then it will show some message.. The following is the code that i am using to insert data in to the table....
EmployeesDetailsDataContext DetailContext = new EmployeesDetailsDataContext();
Employee tblemp = new Employee();
tblemp.Emp_Code = txtEmpId.Text.Trim();
tblemp.F_Name = txtFname.Text.Trim();
tblemp.L_Name = txtLname.Text.Trim();
tblemp.Country = Convert.ToInt32(ddlCountry.SelectedValue);
tblemp.State = Convert.ToInt32(ddlRegion.SelectedValue);
tblemp.City = Convert.ToInt32(ddlCity.SelectedValue);
tblemp.Designation = txtDes.Text.Trim();
tblemp.Address = txtAdd.Text.Trim();
DetailContext.Employees.InsertOnSubmit(tblemp);
DetailContext.SubmitChanges();any help would be appreciate
Regards, Pranav Dave
Something like this might help:
EmployeesDetailsDataContext DetailContext = new EmployeesDetailsDataContext();
//check if there exists a record with the same First and Last Name
bool exists = DetailContext.Employees.Any ( e => e.F_Name == txtFname.Text.Trim() && e.L_Name == txtLname.Text.Trim() )if (exists)
//display message
else
//insert recordRegards, Syed Mehroz Alam
My Blog My Articles Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein