0

Command executed successfully look at here,..

string strConn = "Data Source=USER\\SQLExpress; Initial Catalog=empdata; Integrated Security=true";
        SqlConnection conn = new SqlConnection(strConn);
        conn.Open();

        SqlCommand sqlCmd = new SqlCommand("alter table empreg ADD '"+ this.comboBox1.Text +"' varchar(50)", conn);

        int res=sqlCmd.ExecuteNonQuery();
        if (res == 1)
        { MessageBox.Show("New column created"); }
        else
        { MessageBox.Show("sorry unable to create"); }

But it goes to 2nd part of if "sorry unable to create" result and the thing is that in SQL studio we can see that column created. How is it possible ???

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ARJUN
  • 399
  • 1
  • 3
  • 15
  • return of ExecuteNonQuery is number of affected rows, maybe adding column is interpreted as no changed rows – Milan Halada Apr 28 '14 at 12:04
  • @ARJUN from [MSDN](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx): _For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. ... For all other types of statements, the return value is -1_ – dkozl Apr 28 '14 at 12:06
  • sorry, i didn't get you.. well, if no changed then it should return 0 (zero) right... – ARJUN Apr 28 '14 at 12:08
  • 1
    As you can see with all these answers.. [MSDN Reference](http://msdn.microsoft.com/library) is a great resource to start when you'll have a doubt in the future, it'll save you some time.. – Andre Figueiredo Apr 28 '14 at 12:09
  • yes, André Figueiredo you are right,,, – ARJUN Apr 28 '14 at 12:13

5 Answers5

1

your result is the amount of affected rows, 1 doesn't mean you got an error.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery%28v=vs.110%29.aspx

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

Andrew
  • 3,648
  • 1
  • 15
  • 29
1

From the MSDN page:

"For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1."

You need to handle res ==-1, not res ==1

Jay
  • 2,077
  • 5
  • 24
  • 39
1

from http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

Milan Halada
  • 1,943
  • 18
  • 28
0

SqlCommand.ExecuteNonQuery => "Executes a Transact-SQL statement against the connection and returns the number of rows affected."

It will always return number of rows altered or not

Read more SqlCommand.ExecuteNonQuery

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
-1

qlCommand sqlCmd = new SqlCommand("alter table empreg ADD '"+ this.comboBox1.Text +"' varchar(50)", conn);

correct

qlCommand sqlCmd = new SqlCommand("alter table empreg ADD "+this.comboBox1.Text+" varchar(50)", conn);

and dont need

    if (res == 1)
    { MessageBox.Show("New column created"); }
    else
    { MessageBox.Show("sorry unable to create"); }