I am trying to save values to database from two lists by applying foreach loop on them but due to use of loop inside loop it is saving duplicate values to database which I don't want.Help me!
I am not able to find any solution so did not tried anything yet
var list1 = obj.getlists1();
var list2 = obj.getlists2();
foreach(var item in list1)
{
foreach(var item1 in list2)
{
SqlConnection con = new SqlConnection(connectionstring);
string sql = "INSERT INTO tbllogs (QBDID,PHPID,CreatedBy) values (" ' +item.QBDID+ " ' , " ' +item1.PHPID+ " ' , " ' + item.CreatedBy + " ')";
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
}
}
I want that the values that are already entered into database will not be able to enter into database i.e. I don't want any duplicate values from the loop to enter to database.