PC Review


Reply
Thread Tools Rate Thread

How to contine inserting the rest record when a duplicate occur

 
 
ad
Guest
Posts: n/a
 
      9th Jul 2005
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);
}


 
Reply With Quote
 
 
 
 
Miha Markic [MVP C#]
Guest
Posts: n/a
 
      9th Jul 2005
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);
> }
>
>



 
Reply With Quote
 
ad
Guest
Posts: n/a
 
      9th Jul 2005
Thanks,
But I can't understnad your words.
Can you give me a sample?
Please!


"Miha Markic [MVP C#]" <miha at rthand com> 撰寫於郵件新聞
:#(E-Mail Removed)...
> 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);
> > }
> >
> >

>
>



 
Reply With Quote
 
Miha Markic [MVP C#]
Guest
Posts: n/a
 
      9th Jul 2005
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
// if primary key violation do nothing
// else
throw;
}

"ad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks,
> But I can't understnad your words.
> Can you give me a sample?
> Please!
>
>
> "Miha Markic [MVP C#]" <miha at rthand com> 撰寫於郵件新聞
> :#(E-Mail Removed)...
>> 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);
>> > }
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
duplicate vb projects occur when reopening a sheet Allan Microsoft Excel Programming 9 18th May 2008 12:42 AM
Inserting a duplicate record Michael T Microsoft Access Form Coding 5 28th Jul 2007 09:49 PM
Avoid inserting a duplicate record on page submit? planetthoughtful Microsoft C# .NET 6 30th May 2006 07:38 AM
Duplicate Session Occur Veron Microsoft Windows 2000 Terminal Server Applications 0 29th Apr 2006 09:28 AM
Duplicate Session Occur Veron Microsoft Windows 2000 Terminal Server Applications 0 24th Apr 2006 03:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:31 PM.