How to keep a an exception silence

  • Thread starter Thread starter ad
  • Start date Start date
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 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);
}
 
Robbe Morris said:
try
{
cmd.Parameters.AddWithValue("PID", sPID);
cmd.Parameters.AddWithValue("GradeID", i);
cmd.Parameters.AddWithValue("Sem", j);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();

}
catch { }

Though I strongly dis-recommend throwing away exceptions without at least
logging them to Console.Error (I know, that's what the original poster
wanted). Still, this can cost you hours when searching for bugs.

Regards,
Martin
 
Robbe Morris said:
Its the answer he was looking for.

Yes, of course, I even noted that in my answer. I just wanted to point
out (more him then to you) that this might be dangerous.

Regards,
Martin
 
Back
Top