user4370090
user4370090

Reputation:

Error in insert query in visual studio

I'm new in this field. Trying to insert the values from textbox to my database table, but I get an error at

 adapter.InsertCommand.ExecuteNonQuery();

Can anyone help me solve this?

SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter(); 

String sql = "insert into NewName values('" + first_Name.Text + "','" + last_Name.Text + "','" + user.Text + "','" + email.Text + "','" + password.Text + "','" + contact.Text + "')";

command = new SqlCommand(sql,con);

adapter.InsertCommand = new SqlCommand(sql,con); 

// this line here is showing the error
adapter.InsertCommand.ExecuteNonQuery();

command.Dispose();
con.Close();

enter image description here

Upvotes: 0

Views: 601

Answers (3)

user4370090
user4370090

Reputation:

Yes I've done it .I was using "user" in table column which is not allowed .After changing the column name everything works. This is the code

        SqlCommand command;
        SqlDataAdapter adapter = new SqlDataAdapter();

        String sql = "insert into NewName  values('" + first_Name.Text + "','" + last_Name.Text + "','" + user.Text + "','" + email.Text + "','" + password.Text + "','" + contact.Text + "')";

     
        command = new SqlCommand(sql, con);

        adapter.InsertCommand = new SqlCommand(sql, con);

        // this line here is showing the error
        adapter.InsertCommand.ExecuteNonQuery();

        command.Dispose();
        con.Close();

Upvotes: 0

Eric
Eric

Reputation: 3257

You do not list your column name on insert. This means you are also attempting to insert your identity column as well. Always list your column names

insert into NewName (firstname, lastname, username, email, password, contact)
values('" + first_Name.Text + "','" + last_Name.Text + "','" + user.Text + "','" + email.Text + "','" + password.Text + "','" + contact.Text + "')

Upvotes: 0

DavidG
DavidG

Reputation: 118947

Since your table is called table and that is a SQL reserved word, you have two choices:

  1. Change your table name. This is the only option you should be considering but for completeness;
  2. Quote the name of the table:

    insert into [table] values....
    

Upvotes: 3

Related Questions