Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmd.ExecuteNonQuery value of i integer is going -1 from 0

Tags:

c#

asp.net

sqlcmd

Here i create table in database dynamically. User enters name as his wish and selects language radiobutton. So problem is after executing cmd.ExecuteNonQuery value of i integer is going -1 from 0. And shows that table couldnt be created but when i go to database its already created successfully. Please let me know where i am doing wrong. Thanx in Advance !!

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);
        }      
    }
like image 423
Rushikesh Korgaonkar Avatar asked Jul 23 '12 20:07

Rushikesh Korgaonkar


People also ask

What value does ExecuteNonQuery return?

Although the ExecuteNonQuery returns no rows, any output parameters or return values mapped to parameters are populated with data. 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.

What is use of ExecuteNonQuery () method?

ExecuteNonQuery: Use this operation to execute any arbitrary SQL statements in SQL Server if you do not want any result set to be returned. You can use this operation to create database objects or change data in a database by executing UPDATE, INSERT, or DELETE statements.

How do I know if ExecuteNonQuery is successful C#?

int a=NewCmd. ExecuteNonQuery(); if(a==0) //Not updated. else //Updated. ExecuteNonQuery() -> This function return integer value.

What is ExecuteNonQuery ado net?

ExecuteNonQuery method is used to execute SQL Command or the storeprocedure performs, INSERT, UPDATE or Delete operations. It doesn't return any data from the database. Instead, it returns an integer specifying the number of rows inserted, updated or deleted.


2 Answers

Takes from MSDN Remarks on SqlCommand.ExecuteNonQuery

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.

like image 57
HatSoft Avatar answered Oct 10 '22 18:10

HatSoft


From the docs for SqlCommand.ExecuteNonQuery (emphasis mine):

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.

Is your statement an UPDATE, INSERT or DELETE statement? Nope. Hence you're getting -1.

It's not clear what you mean by the value of i going to -1 "from" 0. It's never 0. It doesn't have a value until it's assigned one from the result of ExecuteNonQuery.

like image 7
Jon Skeet Avatar answered Oct 10 '22 17:10

Jon Skeet