Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
i improved the query as said by the two programmers here.. the new query is as

C#
string query = "insert into ESK_Products(CateogoryID,ProductName,ProductImage,UnitCost,Description) values('" + txtproname.Text + "','" + FileUpload1.FileName + "'," + txtproprice.Text + ",'" + txtprodesc.Text + "') select CategoryID from ESK_Categories where CategoryName='" + DropDownList1.Text + "'";


but now i am getting another error. i think its becuase of categoryid column.. but how can i solve it...some thing we have to specify in for category in sql values..

C#
not insertedSystem.Data.SqlClient.SqlException: There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Admin_Products.btnAdd_Click(Object sender, EventArgs e)    Label
Posted
Updated 1-Sep-11 21:37pm
v5
Comments
Herman<T>.Instance 1-Sep-11 19:05pm    
what happens if you try this?
string query = string.Format("declare @catName varchar(50); select @catName = CategoryID from ESK_Categories where CategoryName = '{0}';", DropDownList1.Text);
string query += string.Format("insert into ESK_Products(CategoryID,ProductName,ProductImage,UnitCost,Description) values( @catName, '{0}', '{1}', '{2}', '{3}' ", txtproname, FileUpload1.FileName, txtproprice, txtprodesc);
codegeekalpha 1-Sep-11 19:09pm    
i get the error

not insertedSystem.Data.SqlClient.SqlException: The name "System.Web.UI.WebControls.TextBox" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Admin_Products.btnAdd_Click(Object sender, EventArgs e)
codegeekalpha 1-Sep-11 19:11pm    
i am not getting u. digimanus
justinonday 2-Sep-11 1:10am    
split query as two query and try?
Herman<T>.Instance 2-Sep-11 3:33am    
no it is 2 commands but with += you create 1 string. the ';' separates the queries for sql but is in 1 call

What happens if you remove the select part - "select CategoryID from ESK_Categories where CategoryName='" + DropDownList1.Text + "'"

Does it insert data then?

I see you have used txtproprice instead of txtproprice.Text.
txtproprice will return "System.Web.UI.WebControls.TextBox" which I can see in your error.
Can you try using txtproprice.Text? Do the same for other text boxes (txtproprice and txtprodesc)
 
Share this answer
 
Dear Friend,

string query = "insert into ESK_Products(CategoryID,ProductName,ProductImage,UnitCost,Description) values('" + txtproname + "','" + FileUpload1.FileName + "','" + txtproprice + "','" + txtprodesc + "') select CategoryID from ESK_Categories where CategoryName='" + DropDownList1.Text + "'";


Replace with txtproprice to txtproprice.text then try the Query


Regards,

Anilkumar.
 
Share this answer
 
I see two errors:
1. probably typo -
SQL
INSERT INTO ESK_Products(CateogoryID...

2. You don't supply value for CategoryID in VALUES part

After new information supplied by OP I've changed query.

C#
string query = 
"DECLARE @categoryID INT; " +
"SET @categoryID = (SELECT CategoryID FROM ESK_Categories where CategoryName='" + DropDownList1.Text + "'); " +
"INSERT INTO ESK_Products " +
"(CategoryID, ProductName, ProductImage, UnitCost, Description) " +
"VALUES " +
"(@categoryID, '" + txtproname.Text + "', '" + FileUpload1.FileName + "', " + txtproprice.Text + ", '" + txtprodesc.Text + "')";
 
Share this answer
 
v2
Comments
codegeekalpha 2-Sep-11 6:50am    
categoryid is the foreign key.. its not autoincrement..
My problem with this and it will also help you is that you should use parameters mate. It helps with security SQL injection etc and in my personal opinion keeps the code a bit neater along the way!
 
Share this answer
 
Comments
Herman<T>.Instance 2-Sep-11 4:59am    
in another post he already stated he would to this in a proper way. He only shows his syntax problem with the query not with the method he uses in real
DanHodgson88 2-Sep-11 7:20am    
cheers for that mate didn't realise that!
hi,

you miss values

string query = "insert into ESK_Products values 

(CategoryID,ProductName,ProductImage,UnitCost,Description) values('" + txtproname + "','" + FileUpload1.FileName + "','" + txtproprice + "','" + txtprodesc + "') select CategoryID from ESK_Categories where CategoryName='" + DropDownList1.Text + "'";
 
Share this answer
 
Comments
codegeekalpha 2-Sep-11 5:57am    
???
Prerak Patel 2-Sep-11 6:46am    
I guess, you missed values in question.

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