Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
want to insert data of gridview to database, got an error ->"Transaction no has already been decleared" Plz help me with this. Thanx
C#
String query = "insert into Account_Credit(Transaction_No,Account_No,Amount_Credit,Credit_Date,Total_Amount_Credit)values(@Transaction_No,@Account_No,@Amount_Credit,@Credit_Date,@Total_Amount_Credit)";
                cmd = new SqlCommand(query, cn);
               cmd.Prepare();

               int i = dataGridView1.CurrentRow.Index;
               cmd.Parameters.AddWithValue("@Account_No", dataGridView1.Rows[i].Cells[0].Value);
               for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
               {


                   cmd.Parameters.AddWithValue("@Transaction_No", dataGridView1.Rows[i].Cells[1].Value);
                   cmd.Parameters.AddWithValue("@Amount_Credit", dataGridView1.Rows[i].Cells[2].Value);
                   cmd.Parameters.AddWithValue("@Credit_Date", dataGridView1.Rows[i].Cells[3].Value);
                   cmd.Parameters.AddWithValue("@Total_Amount_Credit", dataGridView1.Rows[i].Cells[4].Value);
                   cn.Open();


                   cmd.ExecuteNonQuery();
                   cn.Close();
               }
Posted
Updated 30-Dec-12 0:34am
v2
Comments
Jibesh 30-Dec-12 6:43am    
double check the order of the parameters added to the SQLCmd and the order in query are matched.
The First Parameter Passed to the SqlCmd is AcctionNo but inside the insert query its Transaction_No. Check that
zeshanazam 30-Dec-12 6:47am    
u are wrong
Jibesh 30-Dec-12 6:50am    
can you post the exception stack what you are getting for better understanding of the issue
zeshanazam 30-Dec-12 6:52am    
loop works only for one time and then error occurs Transaction no has already been decleared
Jibesh 30-Dec-12 7:10am    
moment...

what are you doing with query execution inside the loop? that means you are adding the parameters to the sqlCmd for each row. you need to reconsider the logic here.

i.e starting from Transaction_No the parameters are added again and again to the sql Cmd.

AddWithValue adds a parameter with its value to the current command. Since you are executing it in a loop, you need to remove existing copies of the parameters before you add new ones.
There are two things you can do:
1) Put this line at teh top of your loop:
C#
cmd.Parameters.Clear();

2) Add the parameters without values outside the loop, and then set the parameter values only inside the loop.
 
Share this answer
 
Change the query like below
C#
String query = "insert into Account_Credit(Transaction_No,Account_No,Amount_Credit,Credit_Date,Total_Amount_Credit)values(@Transaction_No,@Account_No,@Amount_Credit,@Credit_Date,@Total_Amount_Credit)";
          
int i = dataGridView1.CurrentRow.Index;
              
for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
    cmd = new SqlCommand(query, cn);
    cmd.Prepare();
    cmd.Parameters.AddWithValue("@Account_No", dataGridView1.Rows[i].Cells[0].Value);

    cmd.Parameters.AddWithValue("@Transaction_No", dataGridView1.Rows[i].Cells[1].Value);
    cmd.Parameters.AddWithValue("@Amount_Credit", dataGridView1.Rows[i].Cells[2].Value);
    cmd.Parameters.AddWithValue("@Credit_Date", dataGridView1.Rows[i].Cells[3].Value);
    cmd.Parameters.AddWithValue("@Total_Amount_Credit", dataGridView1.Rows[i].Cells[4].Value);
    cn.Open();

    cmd.ExecuteNonQuery();
    cn.Close();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900