Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do I use insert Statement with where clause ..

I have two tables
1. Company
2. Product
In Company Table there is ID(PK) and Company Name
In product Table there is ProductID(PK), Price, QTy, and CompanyID(FK)

when user select company name from comboBox and enter ProductID , Price and Qty it should be save in the table of the particular Product Table.

What I have tried:

insert into T1 (SR_NO,AH,Model,Price,Qty,ID) Values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "', '" + textBox5.Text + "',1 where ID = '1')
Posted
Updated 15-Sep-17 20:02pm
Comments
[no name] 20-Nov-16 9:44am    
Using a proper parameterized query might make whatever error you see on your screen that we can't see, go away all by itself.

First, your closing parenthesis is in the wrong place.

SQL
insert into T1 (SR_NO, AH, Model, Price, Qty, ID) 
Values('" + textBox1.Text + 
       "', '" + textBox2.Text + 
       "', '" + textBox3.Text + 
       "', '" + textBox4.Text + 
       "', '" + textBox5.Text + 
       "', " 1) where ID = 1 


Second, you should be using parameterized queries, which would change your query to look like this:

SQL
insert into T1 (SR_NO, AH, Model, Price, Qty, ID) 
Values(@srNo, @ah, @model, @price, @qty, @id) where ID = 1 


Third, you don't need a where clause in an insert statement. Why are you assigning an ID yourself? You can make the database do that for you when a record is inserted. This (along with parameterized queries) would make your query look like this:

SQL
insert into T1 (SR_NO, AH, Model, Price, Qty) 
Values(@srNo, @ah, @model, @price, @qty)


Finally, follow industry practice when writing SQL queries. It will generally help you read the query better.

SQL
INSERT INTO [database_name].[dbo].[T1] ([SR_NO], [AH], [Model], [Price], [Qty]) 
VALUES (@srNo, @ah, @model, @price, @qty)
 
Share this answer
 
If you are inserting, why you need the Pk of Product? You can have that column auto incremented.

Then your insert statement should look like below.
SQL
INSERT INTO Product (ColumnNames) VALUES (AllValues) WHERE CompanyId = ComboBoxSelectedCompanyId
 
Share this answer
 
Comments
Mahrukh Razi 20-Nov-16 2:36am    
It gives error near where clause
What is the error?
It gives an error.
Incorrect syntax near the keyword 'where'
My code is..
string query= "Insert Into Course(PostNum,CourseName)values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem + "')where StID'"+Label1.Text+"'";
SqlCommand cmd = new SqlCommand(query, con);
 
Share this answer
 
Comments
Patrice T 16-Sep-17 3:33am    
This is not a solution to this question.
Open a new question for your problem and delete this.

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