Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

this is my function to insert new row

C#
public int addrow(string sqlcommand)
        {
            int res = 0;
            try
            {
                con.Open();
                cmd.CommandText = sqlcommand;
                cmd.Connection = con;
                cmd.ExecuteNonQuery();
                if (cmd.ExecuteNonQuery() == 1)
                    res = 1;
            }
            catch (Exception exp)
            {
                throw exp;
            }

            finally
            {
                con.Close();
            }
            return res;
        }


when i call this function it inserted twice rows
C#
string command = "insert into caseT values(N'" + caseTxt.Text + "')";
            int addRow = db.addrow(command);
            if (addRow == 1)
            {
                MessageBox.Show("inserted");
                caseTxt.Text = "";
                fillCase();
            }

1 case1
2 case2

any help
Posted
Comments
Heba Kamel 2-Jan-15 8:20am    
any id is identity in table
Tejas Vaishnav 2-Jan-15 8:31am    
As per you data shown like 1 case1 and 2 case2 is totally different data, it's not like duplicate row with different id like 1 and 2.

1 solution

Because you call it twice:
C#
cmd.ExecuteNonQuery();
if (cmd.ExecuteNonQuery() == 1)
    res = 1;


Change it to:
C#
int res = cmd.ExecuteNonQuery();
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 2-Jan-15 8:42am    
+5, great answer Maciej. :-)
Maciej Los 2-Jan-15 8:50am    
Thank you ;)
Happy New Year!
Afzaal Ahmad Zeeshan 2-Jan-15 8:54am    
Happy New Year to you too Maciej, :-)
Maciej Los 2-Jan-15 9:01am    
Thank you, Afzaal ;)
[no name] 2-Jan-15 8:48am    
+5. Bruno

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