Hi,
You should trap and "eat" the exception when such an even occurs.
You have to embed ExecuteNonQuery into a such try/catch block (you are doing
similar thing however not in the right place).
Be careful not to "eat" other exceptions though.
--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog:
http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group
www.codezone-si.info
"ad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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);
> }
>
>