Cmd.ExecuteNonQuery value i integer goes -1 from 0

Here I create a table in a database dynamically. The user enters the name as his wish and selects the language of the exchange. So the problem arises after cmd.ExecuteNonQuery is executed, the value i integer goes -1 from 0. And it shows that the table cannot be created, but when I go to the database, it is 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);
        }      
    }
+5
source share
4 answers

Accepts MSDN Comments on SqlCommand.ExecuteNonQuery

UPDATE, INSERT DELETE , . ​​ , , , , . , -1. , -1.

+8

SqlCommand.ExecuteNonQuery ( ):

UPDATE, INSERT DELETE - , . , , , , , . -1. , -1.

UPDATE, INSERT DELETE? . , -1. ​​

, i, -1 "" 0. 0. , ExecuteNonQuery.

+7

Insert a common common function (ByVal SProcedure As String, ByVal () As SqlParameter parameters) As a Boolean Using cnn As New SqlConnection (Emplooyes) Try

            Dim cmd As SqlCommand = New SqlCommand(SProcedure, cnn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddRange(parameters)

            If cnn.State = ConnectionState.Closed Then
                cnn.Open()
            End If

            Dim Ls As Integer
            Ls= cmd.ExecuteNonQuery()
            If Ls = -1 Then
                Return False
            Else
                Return True
            End If

        Catch ex As Exception
            Return False
        Finally
            cnn.Close()
        End Try
    End Using
End Function
-2
source

All Articles