Click here to Skip to main content
15,891,733 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SqlCeConnection stock_in = new SqlCeConnection(@"Data Source=C:\\Users\\Prime Computer\\Documents\\Visual Studio 2010\\Projects\\STOCK MANAGEMENT\\STOCK MANAGEMENT\\StockManagement.sdf");

           stock_in.Open();
           SqlCeCommand stock_in_command = new SqlCeCommand("INSERT INTO stock_in(name,code,type,category,price,packing,power,quantity)VALUES('" + comboBox1.SelectedText + "','" + comboBox2.SelectedText + "','" + comboBox3.SelectedText + "','" + comboBox4.SelectedText + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "',)",stock_in);
           stock_in_command.ExecuteNonQuery();
           stock_in.Close();
           MessageBox.Show("RECORD IS SAVED");


What I have tried:

what is wrong with my code i have also tried to use combobox.text property but still it is giving the same error
Posted
Updated 5-Jun-17 9:48am
Comments

Remove the comma before the closing parenthesis, it means there is another value and there is not;

"','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "',)",stock_in);


Also please look at using parameterised queries rather than concatenating strings, leaving yourself open to SQL injection and it is harder to find problems when they occur.
 
Share this answer
 
Comments
Member 13241999 5-Jun-17 16:06pm    
thanks for pointing out comma mistake but after removing the comma it show the following error now "The specified table does not exist. [ stock_in ]" even the table is there
Michael_Davies 5-Jun-17 16:33pm    
You'll need to show the schema for anyone to be able to answer that.
Member 13241999 5-Jun-17 16:39pm    
hmmm you are right but unfortunately print screen button of my laptop is not working can i have your whatsapp/imo/facebook so i can send you table schema picture/ video
Member 13241999 5-Jun-17 16:40pm    
Reply Modify the comment. Delete the comment.
i want to insert data in table say "stock_in" i am building an app for medical store in which i have mutliple forms and each form is having using database which is in my case "medicalstoremanagement.sdf" which include talbes like " product,customer,company,orders,stock in, stock out, etc the form/table for which i am using the above stated query is stock_in the form link with it is using values from two tables product and customer i have link the comboboxes with combobox1 which is using value from a datasource name addproductbindingsource.
Not directly a solution to your question, but another problem you have.
Never build an SQL query by concatenating with user inputs, it is a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]

Secondary problem of the way you build the query:
You are given a very precise error message, but since we have no way to know what is your exact query, we can't use the error message.
C#
SqlCeCommand stock_in_command = new SqlCeCommand("INSERT INTO stock_in(name,code,type,category,price,packing,power,quantity)VALUES('" + comboBox1.SelectedText + "','" + comboBox2.SelectedText + "','" + comboBox3.SelectedText + "','" + comboBox4.SelectedText + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "',)",stock_in);

But sometimes one can get lucky when the error is so huge. Have a mppl at the end of query and remove the comma.
 
Share this answer
 
Comments
Member 13241999 5-Jun-17 16:10pm    
thanks for pointing out comma mistake but after removing the comma it show the following error now "The specified table does not exist. [ stock_in ]" even the table is there
Patrice T 5-Jun-17 17:00pm    
I fear I can't help you on this.
Member 13241999 5-Jun-17 16:32pm    
i want to insert data in table say "stock_in" i am building an app for medical store in which i have mutliple forms and each form is having using database which is in my case "medicalstoremanagement.sdf" which include talbes like " product,customer,company,orders,stock in, stock out, etc the form/table for which i am using the above stated query is stock_in the form link with it is using values from two tables product and customer i have link the comboboxes with combobox1 which is using value from a datasource name addproductbindingsource.
Michael_Davies 5-Jun-17 16:56pm    
In you above comment you name the table stock in not stock_in as in your query, if it is the stock in then you need to use square brackets as you are using a space in a name; [stock in]
Member 13241999 6-Jun-17 11:10am    
thank you very much for your report i am sorry it was misspelled by me it is stock_in and not stock in there is no space in my data table it is as exact as in my query

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