A
ad
I use a loop to insert about 12 record into table:
Mybe there are already some records exist in the table,
When the duplicate primary key exception thow, my program will show an error
page and the loop terminate.
I hope that when an duplicate primary key exception thow, my application
will keep silence,
and continue to work the rest record until the loop is complete.
How can I do that?
//--------------------------------------------------------------------------
---------
SqlConnection cnn = new SqlConnection(....);
SqlCommand cmd = new SqlCommand();
cnn.Open();
cmd.Connection = cnn;
cmd.CommandType = CommandType.Text;
string sSql = "Insert into Sight (PID, GradeID, Sem) values (@PID,
@GradeID, @Sem)";
cmd.CommandText = sSql;
string sPID = Request.QueryString["PID"].ToString();
try
{
for (int i = 1; i < 13; i++)
for (int j = 1; j < 3; j++)
{
cmd.Parameters.AddWithValue("PID", sPID);
cmd.Parameters.AddWithValue("GradeID", i);
cmd.Parameters.AddWithValue("Sem", j);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
catch (Exception ex)
{
iErr++;
Trace.Write(ex.Message);
}
Mybe there are already some records exist in the table,
When the duplicate primary key exception thow, my program will show an error
page and the loop terminate.
I hope that when an duplicate primary key exception thow, my application
will keep silence,
and continue to work the rest record until the loop is complete.
How can I do that?
//--------------------------------------------------------------------------
---------
SqlConnection cnn = new SqlConnection(....);
SqlCommand cmd = new SqlCommand();
cnn.Open();
cmd.Connection = cnn;
cmd.CommandType = CommandType.Text;
string sSql = "Insert into Sight (PID, GradeID, Sem) values (@PID,
@GradeID, @Sem)";
cmd.CommandText = sSql;
string sPID = Request.QueryString["PID"].ToString();
try
{
for (int i = 1; i < 13; i++)
for (int j = 1; j < 3; j++)
{
cmd.Parameters.AddWithValue("PID", sPID);
cmd.Parameters.AddWithValue("GradeID", i);
cmd.Parameters.AddWithValue("Sem", j);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
catch (Exception ex)
{
iErr++;
Trace.Write(ex.Message);
}