Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Friday, March 23, 2012

problem with INSERT on ExecuteNonQuery

I'm trying to INSERT a new record into a SQL database using the command object. It works if I use an UPDATE, but every time I try an INSERT, I get "ExecuteNonQuery: Connection property has not been initialized". The query has been checked in enterprise manager direct and works fine. Has anybody got any ideas, please?


Dim cmdSql As New SqlCommand("INSERT INTO table(blah) VALUES ('blah'), ConUsers")
ConUsers.Open()
cmdSql.ExecuteNonQuery()
ConUsers.Close()
What are you using for your connection/connection string? is it exactly the same as the sub that works for the update statement?|||> is it exactly the same as the sub that works for the update statement?

yes, it is. The only thing that changes is the query itself and I've checked that on the database direct. I've even checked that servername/ASPNET has INSERT permission on that table. So I'm a bit stumped.|||Is that code copied and pasted? If so, you have a misplaced double quote. Should be like this:

 Dim cmdSql As New SqlCommand("INSERT INTO table(blah) VALUES ('blah')", ConUsers)

Terri

Problem with INSERT

I've made customcontrol with 3 textboxes, dropdownlist and button.

here is action for button:

protected void Button1_Click(object sender, EventArgs e) { SqlDataSource1.InsertCommand ="insert INTO P ( Numer, Opis, Koszt, id_kli) VALUES('" + nr.Text +"'" +"," +"'" + opisTB.Text +"'" +"," +"'" + kosztTB.Text +"'" +"," +"'" + DropDownList1.SelectedValue +"');"; SqlDataSource1.Insert(); Button1.Visible =false; }
 
the problem is it insert 5 row at once
what should i change.
my db looks like:  P(nr_id(PK), Numer, Opis, Koszt, id_kli);
K(id_kli(PK), Name); 
 
PS
sorry for my english... 

baran:

I've made customcontrol with 3 textboxes, dropdownlist and button.

here is action for button:

protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertCommand ="insert INTO P ( Numer, Opis, Koszt, id_kli) VALUES('" + nr.Text +"'" +"," +"'" + opisTB.Text +"'" +"," +"'" + kosztTB.Text +"'" +"," +"'" + DropDownList1.SelectedValue +"');";
SqlDataSource1.Insert();
Button1.Visible =false;
}
 
the problem is it insert 5 row at once
what should i change.
my db looks like:  P(nr_id(PK), Numer, Opis, Koszt, id_kli);
K(id_kli(PK), Name); 

First of all, you should be using parameterized SQL statements. The code you have there, with concatenating UI-supplied data to a SQL string to be executed, is quite insecure. It opens you up to a possibleSQL Injection attack.

If you need to insert 5 rows, wouldn't you create a loop in your code, executing the SQL statement 5 times, each time with different parameter values?

|||

i don't need to insert 5 rows it makes it and i don't know why and how...

i'm noob just getting started

|||

Hi baran,

In this case, I suspect that this event has been fired several times. Please set a breakpoint to see if it has been fired 5 times. Each time should insert 1 record.