-1

I create a table in the database dynamically. The user enters their name as they wish and selects their language using a RadioButton. The problem is that after executing cmd.ExecuteNonQuery the value of i is changing to -1 from 0. This shows that the table couldnt be created but when I go to the database it has been created successfully. Please let me know where I am going wrong?

protected void btnpaper_Click(object sender, EventArgs e)
    {
        try
        {                
                string conn = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
                SqlConnection con = new SqlConnection(conn);
                con.Open();
                char[] arr = new char[] {'n','g','l','i','s','h'};
                string str = "CREATE TABLE " + Label1.Text.Trim() + 
                             txtpaperset.Text.Trim()+ rbtnEng.Text.TrimEnd(arr) +
                             "(" + "quesNo int NOT NULL PRIMARY KEY, " + 
                             "question varchar(1000) NOT NULL," + 
                             "ansA varchar(500) NOT NULL, " + 
                             "ansB varchar(500) NOT NULL, " + 
                             "ansC varchar(500) NOT NULL, " + 
                             "ansD varchar(500) NOT NULL, " + 
                             "rightAns varchar(50) NOT NULL " + ")";                    
                SqlCommand cmd = new SqlCommand(str, con);
                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    lblerrormsg.Visible = true;
                    con.Close();
                }
                else
                {
                    lblerrormsg.Text = "Table Not Created Please Try with Different Name!";                        
                    con.Close();
                }                

        }
        catch (System.Exception excep)
        {
            MessageBox.Show(excep.Message);
        }      
    }
eggyal
  • 122,705
  • 18
  • 212
  • 237
rushikesh
  • 19
  • 1
  • 5
  • This code is prone to SQL Injection – Curtis Oct 22 '12 at 09:22
  • **Exactly** the same question: [cmd.ExecuteNonQuery value of i integer is going -1 from 0](http://stackoverflow.com/questions/11619919/cmd-executenonquery-value-of-i-integer-is-going-1-from-0) – Adriano Repetti Oct 22 '12 at 09:35

2 Answers2

1

There is no any problem with your code, everything is correct. Please check this Link,

**

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.

**

Community
  • 1
  • 1
andy
  • 5,979
  • 2
  • 27
  • 49
  • Anandkumar is correct, here is another source stating the same thing http://books.google.com.au/books?id=4fYEPmvmCM4C&pg=PA129&lpg=PA129&dq=executenonquery+returns+negative+1+create+table&source=bl&ots=D5usl2nhRz&sig=uwipw3ICU6q_hPfY_O9_rRwX1vE&hl=en&sa=X&ei=3RKFUISdNciZiAeDwYHoDQ&ved=0CCYQ6AEwAQ#v=onepage&q=executenonquery%20returns%20negative%201%20create%20table&f=false – Martin-Brennan Oct 22 '12 at 09:34
-1

use this

SqlCommand cmd = new SqlCommand(str, con); cmd.CommandType = CommandType.Text;

Hitesh S
  • 460
  • 1
  • 5
  • 20