Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have multiple text box and a submit button

on click it take data in text box and insert them to database.

I'm checking if text is not "" then insert textbox.text else nothing here is code sample:

C#
Insert into tablename(col1, col2, col3) values( 

& if not textboxone.text="" 

,'textboxone.text'

else

''

.
.
.
)
Posted
Updated 4-May-12 19:32pm
v2
Comments
R. Giskard Reventlov 4-May-12 18:47pm    
And your question is?
Member 8317792 4-May-12 18:59pm    
it is giving incorrect syntex so how to insert null values if text was not in textbox?
R. Giskard Reventlov 4-May-12 19:12pm    
Check the value first and then only carry on if it is valid. You should be validating your data.
thams 4-May-12 23:14pm    
paste your coding with in code tags.
And which language u r using (vb/c#)?
Your concatenation is confusing...
Rahul Rajat Singh 5-May-12 1:31am    
not clear please show the actual code for clarity

1 solution

Use a parametrized query:
VB
Using con As New SqlConnection(strConnection)
	con.Open()
	Using cmd As New SqlCommand("INSERT INTO MyTable(Id, MyText) VALUES (@ID, @MT)", con)
		cmd.Parameters.AddWithValue("@ID", Id)
		cmd.Parameters.AddWithValue("@MT", If(myTextBox.Text = "", DirectCast(DBNull.Value, Object), DirectCast(myTextBox.Text, Object)))
		cmd.ExecuteNonQuery()
	End Using
End Using
 
Share this answer
 
Comments
Member 8317792 7-May-12 17:17pm    
Does this work with oledb? I have an access database and my code in VB.net 3.5

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