PC Review


Reply
Thread Tools Rate Thread

How a commit a loop of inserting

 
 
ad
Guest
Posts: n/a
 
      8th Jul 2005
I use loop to insert 12 record into a table:
But the for_Loop only loop once and throw an error:
[System.Data.SqlClient.SqlException] = {"The variable name '@PID' has
already been declared. Variable names must be unique within a query batch or
stored procedure."}

How can I do ?

//=========================================================
SqlConnection cnn = DM.cnn;
SqlTransaction trans;
SqlCommand cmd = new SqlCommand();
if (cnn.State == ConnectionState.Closed)
cnn.Open();
trans = cnn.BeginTransaction();
cmd.Connection = cnn;
cmd.Transaction = trans;
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; i < 3; i++)
{
cmd.Parameters.AddWithValue("PID", sPID);
cmd.Parameters.AddWithValue("GradeID", i);
cmd.Parameters.AddWithValue("Sem", j);
cmd.ExecuteNonQuery();
}
trans.Commit();

}
catch (Exception ex)
{
//Trace.Write(ex.Message);
e.Cancel = true;
trans.Rollback();
}


 
Reply With Quote
 
 
 
 
=?Utf-8?B?S2VycnkgTW9vcm1hbg==?=
Guest
Posts: n/a
 
      8th Jul 2005
ad,

In the loop, before adding the parameters:

cmd.Parameters.Clear

Kerry Moorman


"ad" wrote:

> I use loop to insert 12 record into a table:
> But the for_Loop only loop once and throw an error:
> [System.Data.SqlClient.SqlException] = {"The variable name '@PID' has
> already been declared. Variable names must be unique within a query batch or
> stored procedure."}
>
> How can I do ?
>
> //=========================================================
> SqlConnection cnn = DM.cnn;
> SqlTransaction trans;
> SqlCommand cmd = new SqlCommand();
> if (cnn.State == ConnectionState.Closed)
> cnn.Open();
> trans = cnn.BeginTransaction();
> cmd.Connection = cnn;
> cmd.Transaction = trans;
> 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; i < 3; i++)
> {
> cmd.Parameters.AddWithValue("PID", sPID);
> cmd.Parameters.AddWithValue("GradeID", i);
> cmd.Parameters.AddWithValue("Sem", j);
> cmd.ExecuteNonQuery();
> }
> trans.Commit();
>
> }
> catch (Exception ex)
> {
> //Trace.Write(ex.Message);
> e.Cancel = true;
> trans.Rollback();
> }
>
>
>

 
Reply With Quote
 
Jim Hughes
Guest
Posts: n/a
 
      9th Jul 2005

"ad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I use loop to insert 12 record into a table:
> But the for_Loop only loop once and throw an error:
> [System.Data.SqlClient.SqlException] = {"The variable name '@PID' has
> already been declared. Variable names must be unique within a query batch
> or
> stored procedure."}
>
> How can I do ?
>
> //=========================================================
> SqlConnection cnn = DM.cnn;
> SqlTransaction trans;
> SqlCommand cmd = new SqlCommand();
> if (cnn.State == ConnectionState.Closed)
> cnn.Open();
> trans = cnn.BeginTransaction();
> cmd.Connection = cnn;
> cmd.Transaction = trans;
> 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; i < 3; i++)
> {
> cmd.Parameters.AddWithValue("PID", sPID);
> cmd.Parameters.AddWithValue("GradeID", i);
> cmd.Parameters.AddWithValue("Sem", j);
> cmd.ExecuteNonQuery();
> }
> trans.Commit();
>
> }
> catch (Exception ex)
> {
> //Trace.Write(ex.Message);
> e.Cancel = true;
> trans.Rollback();
> }


You only need to add the parameters once, not each time through the loop.

Then just reset the values assigned to each paramter in the loop.

cmd.Parameters.Add("PID");
cmd.Parameters.Add("GradeID");
cmd.Parameters.Add("Sem");
try
{
for (int i = 1; i < 13; i++)
for (int j = 1; i < 3; i++)
{
cmd.Parameters("PID") = sPID;
cmd.Parameters("GradeID") = i;
cmd.Parameters("Sem") = j;
cmd.ExecuteNonQuery();
}
trans.Commit();

}


 
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
Inserting record in a table via loop Jack Microsoft Access Form Coding 0 1st Apr 2008 10:41 PM
loop help with inserting records moonmirage@gmail.com Microsoft Access Form Coding 3 31st Jan 2008 12:41 AM
Way to loop code for inserting Checkboxlist values into DB? wjer Microsoft ADO .NET 0 13th Feb 2004 08:39 PM
Validation? Loop? Commit? Sander Verhagen Microsoft Access ADP SQL Server 23 10th Feb 2004 04:13 PM
Inserting data into tables (COMMIT/ROLLBACK) Dino M. Buljubasic Microsoft ADO .NET 5 18th Jul 2003 07:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:42 PM.