How to contine inserting the rest record when a duplicate occur

  • 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 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);
}
 
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.
 
Thanks,
But I can't understnad your words.
Can you give me a sample?
Please!


Miha Markic said:
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 said:
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);
}
 
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
// if primary key violation do nothing
// else
throw;
}

ad said:
Thanks,
But I can't understnad your words.
Can you give me a sample?
Please!


Miha Markic said:
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 said:
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);
}
 
Back
Top