Click here to Skip to main content
16,016,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,
I have a Table Inventory with following columns[Invoice Number,Date,Product,[Serial Number],Status, Amount .I have another table Table 1. I want to insert the top(1) row of table inventory into table 1 where the Product=TextBox.Text.How to do it kindly Help.
My code is as follows:-
C#
con.Open();
                string quer = "insert into Table1([Invoice Number],Date,Product,[Serail Number],Status, Amount) Select Top(1) Inventory where Product='" + TextBox10.Text + "'";
                SqlCommand cmdd = new SqlCommand(quer, con);
                cmdd.ExecuteNonQuery();
                con.Close();<pre lang="c#">
Posted

try with
C#
string quer = "insert into Table1([Invoice Number],Date,Product,[Serail Number],Status, Amount) Select Top(1) [Invoice Number],Date,Product,[Serail Number],Status, Amount from Inventory where Product='" + TextBox10.Text + "'";

you need may need to specify the column names and "from" keyword in the second part of the sql select statement. baically syntax would be
SQL
-- Option 1:
INSERT INTO table1 (column names....)
SELECT TOP (1) column names....
FROM table2

-- Option 2:
INSERT TOP (1) INTO table1 (column names....)
SELECT column names....
FROM table2

you better use parameterized sql statement rather than concatenating strings to avoid sql injection attacks.
 
Share this answer
 
v2
SQL
INSERT TOP(1) INTO Table1 
SELECT * FROM Inventory where Product = '" + TextBox10.Text + "'"
 
Share this answer
 
Comments
Member 10578683 11-Dec-14 3:26am    
Thank you

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