Click here to Skip to main content
15,911,789 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to insert null value of a text box into table. The below query works but when I leave a text box empty it gives me error " data type mismatch in criteria expression"
I have checked validation rule under property sheet is blank and required field is set to no in ms access table.
SQL
"insert into " & ComboBox1.SelectedValue & " (a,b)values ('" & TextBox1.Text & "','" & TextBox2.Text & "')" 



I have tried this , it gives me error - Syntax error in INSERT INTO statement.

SQL
"insert into " & ComboBox1.SelectedValue & " (a,B) DBnull.values ('" & TextBox1.Text & "','" & TextBox2.Text & "')"


can someone tell me how to insert null value of a text box into table?
Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 22-Jan-16 23:40pm    
What is that, "null value of a text box". How it can be null? Do you mean a text box reference equal to null? but it means having no text box object... :-)
—SA
PIEBALDconsult 22-Jan-16 23:45pm    
0) Use parameterized statements
1) To insert a NULL you likely need to use DBNull.Value
Sergey Alexandrovich Kryukov 22-Jan-16 23:59pm    
Or just empty string, because text box is hardly capable of producing null... :-)
—SA
PIEBALDconsult 23-Jan-16 1:15am    
Yes, but that's not the point.
Sergey Alexandrovich Kryukov 23-Jan-16 1:21am    
:-)

1 solution

Please see my comment to the question. As formulated, it does not seems to make any sense.

However, 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)



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