Click here to Skip to main content
15,906,296 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
VB
Dim str1 As String = " Insert into ticket details values('" & Text1.Text & "'," & Val(Text7.Text) & "," & Val(Text2.Text) & "," & Val(Text3.Text) & ",'" & Text4.Text & "'," & Val(Text5.Text) & "," & Val(Text6.Text) & ")"


what is the syntax error in this line
Posted
Updated 22-Mar-11 22:52pm
v2

So many things:
1) It is a good idea to keep SQL command keywords as all upper case: it helps you identify the various parts of the statement.
2) It is a very good idea to give sensible names to your textboxes: that way you don't get them mixed up. "tbUserName" is much easier to read in code than "Text6" - you get an immediate sense of "is this right" when you read it. Intellisense will "fill in the blanks" so it is almost no extra typing...
3) It is a good idea to delimit your table names if they contain spaces - or preferably use CamelCase table names so you do not need spaces at all...
4) It is a very good idea to use parametrized queries: your way leaves you wide open to an accidental or deliberate SQL Injection attack. Since this could destroy your database, most people try to avoid it...
5) When entering SQL queries, it is recommended that you follow the syntax, and incluyde teh field bnames in your staemnet, so that SQL knows where to put each element...
INSERT INTO mtTable (myColumn, myOtherColumn), VALUES (@MC, @MOC)


Try this:

Dim str1 As String = "INSERT INTO [ticket details] (myColumn, myOtherColumn) VALUES (@MC, @MOC)"
SqlCommand com = new SQLCOmmand(str1, con)
com.Parameters.AddWithValue("@MC", Text1.Text)
com.Parameters.AddWithValue("@MOC", Val(Text7.Text))
 
Share this answer
 
Look at example at this site: system.data.sqlclient.sqlcommand.parameters.aspx[^]

It may save you a lot of troubles
 
Share this answer
 
Always use string.Format instead of multiple "&". To concatenate in cycle, always use System.Text.StringBuilder, never "&". Remember: strings are immutable. Based in this information, can you understand why "&" is such a big performance leak?

—SA
 
Share this answer
 
hi,
try this

Dim str1 As String = (" Insert into ticket details values('" + Text1.Text & "'," & Convert.ToInt32(Text7.Text) & "," & Convert.ToInt32(Text2.Text) & "," & Convert.ToInt32(Text3.Text) & ",'") + Text4.Text & "'," & Convert.ToInt32(Text5.Text) & "," & Convert.ToInt32(Text6.Text) & ")"
 
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