Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hey im trying to insert data into database from vb here. so the first data is from textbox while the other 5 data are from combobox where i've already insert the unbount value into it through vb. so when i try to debug the program, the following stated error occured. anybody can tell whats wrong with my code or maybe i miss something? thanks for your help. :D
VB
Dim rs As New OleDb.OleDbCommand("Insert into table1 (id, control1, control2, control2, control3, control4, control5) values('" & TextBox1.Text & "', '" & Me.ComboBox1 & "', '" & Me.ComboBox2 & "', '" & Me.ComboBox3 & "', '" & Me.ComboBox4 & "', '" & Me.ComboBox5 & "')", con)
Posted
Updated 27-Jun-15 1:26am
v3
Comments
[no name] 27-Jun-15 7:34am    
What is wrong is that you are still using string concatenation instead of parameterized queries, as you have already been told.

ComboBox1 is (a reference to) a Control, while a string is expected there (e.g. ComboBox1.SelectedItem.ToString()).
Of course, you should also follow the advice on using parameterized queries.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Jun-15 8:24am    
Correct, a 5, but the real problem is the query composition which is subject to SQK injection; please see Solution 2.
—SA
Of course combo box cannot be converted to string, so it's strange that you even complain about the error. Probably you mean the text of the combo box selected item.

But your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900