Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
want to insert data of gridview to database, got an error ->"Transaction no has already been decleared" Plz help me with this. Thanx
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 30-Dec-12 0:22am
Edited 30-Dec-12 0:34am
v2
Comments
jibesh at 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 at 30-Dec-12 6:47am
   
u are wrong
jibesh at 30-Dec-12 6:50am
   
can you post the exception stack what you are getting for better understanding of the issue
zeshanazam at 30-Dec-12 6:52am
   
loop works only for one time and then error occurs Transaction no has already been decleared
jibesh at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
cmd.Parameters.Clear();
2) Add the parameters without values outside the loop, and then set the parameter values only inside the loop.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Change the query like below
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();
}
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100