Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
i try to insert a value into table called student in sqlserver but i get error in line cmd.ExecuteNonQuery;

C#
public partial class additem : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
    protected void addButton_Click(object sender, EventArgs e)
    {
       SqlConnection cn = new SqlConnection(@"Server=.\SQLEXPRESS; DataBase=first; Integrated Security=true;");
       SqlCommand cmd;
        cmd = new SqlCommand("Insert into student (id) values ("+ idTextBox.Text +")", cn);
        try
        {
            cn.Open();
            cmd.ExecuteNonQuery;
            resultLabel.Text = "added successfully";
        }
        catch (SqlException ex)
        {
            resultLabel.Text = ("error" + ex.Message);
        }
        finally
        {
            cn.Close();
        }
    }
Posted
Comments
MayurDighe 14-Jun-14 17:46pm    
Always Try to Debug the program using Built-in facilities (e.g. The Menu Bar -> Debug Menu).
Soumitra Mithu 14-Jun-14 23:39pm    
what is the error it says??

i found the error

it just should be
cmd.ExecuteNonQuery();
 
Share this answer
 
you have done 2 mistake

1. SqlCommand values does not have '
2. Eecute non query should like this cmd.ExecuteNonQuery()

C#
protected void addButton_Click(object sender, EventArgs e)
    {
       SqlConnection cn = new SqlConnection(@"Server=.\SQLEXPRESS; DataBase=first; Integrated Security=true;");
       SqlCommand cmd;
        cmd = new SqlCommand("Insert into student (id) values ('"+ idTextBox.Text +"')", cn);
        try
        {
            cn.Open();
            cmd.ExecuteNonQuery();
            resultLabel.Text = "added successfully";
        }
        catch (SqlException ex)
        {
            resultLabel.Text = ("error" + ex.Message);
        }
        finally
        {
            cn.Close();
        }
    }
 
Share this answer
 
Comments
ZurdoDev 18-Jun-14 7:55am    
1. This is open to sql injection. Not a good solution.
2. You did answer the question though.
Use parameterized query, check this out[^]
 
Share this answer
 
USE CODE AS BELOW


protected void addButton_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(@"Server=.\SQLEXPRESS; DataBase=first; Integrated Security=true;");
Cn.open();
SqlCommand cmd;
cmd = new SqlCommand("Insert into student (id) values (‘"+ idTextBox.Text +"’)", cn);

cmd.ExecuteNonQuery;

resultLabel.Text = "added successfully";
cn.close();

}
 
Share this answer
 
Comments
[no name] 14-Jun-14 21:34pm    
If you had been reading, the OP solved his own issue. And like the OPs original code, your code will not compile either.
ZurdoDev 18-Jun-14 7:54am    
This is open to SQL injection. Not a good suggestion.
Try this.

From:
C#
SqlCommand cmd;
cmd = new SqlCommand("Insert into student (id) values ("+ idTextBox.Text +")", cn);

Change to :
C#
SqlCommand cmd = new SqlCommand("Insert into student (id) values ("+ idTextBox.Text +")", cn);


From
C#
try
        {
            cn.Open();
            cmd.ExecuteNonQuery;
            resultLabel.Text = "added successfully";
        }

Change to:
C#
try
        {
            cmd.cn.Open();
            cmd.ExecuteNonQuery;
            resultLabel.Text = "added successfully";
        }


Hope can help.
 
Share this answer
 
Comments
ZurdoDev 18-Jun-14 7:54am    
1. This won't compile.
2. You are open to SQL injection. Not a good suggestion.
As seen your above code -

First you check that id column of student is primary key or identity or not. If id column is primary key then you can not insert the record.

If id column is not p.k or identity then you post the exact error.
 
Share this answer
 
ExecuteNonQuery is a function. You can't use it like property. Please apply paranthesis () just after the ExecuteNonQuery to execute it as function as shown below

C#
cmd.ExecuteNonQuery();


Hope, this'll help you.
 
Share this answer
 
Use cmd.ExecuteNonQuery(); instead of cmd.ExecuteNonQuery
 
Share this answer
 

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