Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to insert dummy data in the view in the vb.net, and my code is as below...

Dim test As String = "INSERT INTO Abc.dbo.Dbe(AccountID,SecurityID,Quantity,OriginalCost,Commitment,AdvisorName,ClientName,ARIMarketValue) vALUES('" & moAccount.AccountID & "," & moSecurity.SecurityID & "," & 0 & "," & 1021 & "," & 0 & "," & "abc" & "," & "cde" & "," & 1021 & "')"


but I am getting the exception error in the above code which is

Incorrect syntax near '.'.

Please Help, where am I making a mistake.

What I have tried:

I have tried to insert a data in the way mentioned above but I am getting exception like...

Incorrect syntax near '.'.
Posted
Updated 25-Apr-16 4:10am

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Fixing that security vulnerability will almost certainly fix your problem at the same time.
VB.NET
Using conn As New SqlConnection("...")
    Using cmd As New SqlCommand("INSERT INTO Abc.dbo.Dbe (AccountID, SecurityID, Quantity, OriginalCost, Commitment, AdvisorName, ClientName, ARIMarketValue) VALUES (@AccountID, @SecurityID, @Quantity, @OriginalCost, @Commitment, @AdvisorName, @ClientName, @ARIMarketValue)", conn)
        cmd.Parameters.AddWithValue("@AccountID", moAccount.AccountID)
        cmd.Parameters.AddWithValue("@SecurityID", moSecurity.SecurityID)
        cmd.Parameters.AddWithValue("@Quantity", 0)
        cmd.Parameters.AddWithValue("@OriginalCost", 1021)
        cmd.Parameters.AddWithValue("@Commitment", 0)
        cmd.Parameters.AddWithValue("@AdvisorName", "abc")
        cmd.Parameters.AddWithValue("@ClientName", "cde")
        cmd.Parameters.AddWithValue("@ARIMarketValue", 1021)
        
        conn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Using



Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
 
Share this answer
 
Your code isn't making a valid SQL statement, look at the content of "test" in the debugger and it'll be like

"... values ('123, 456, 0, 121 .. ')

That's not how you construct values, you put apostrophes around individual field values if they are text, otherwise you don't need them, you have put apostrophes around the whole values data. So you need the sql to look like this

values (123, 456, 0, 1021, 0, 'John', 'Dave' ..

and so on. However you'll hit problems if one of your names has an apostrophe, like "O'Neil".

Rather than dealing with these issues caused by making SQL yourself, use the parameterised queries as already advised. It makes the code simpler to read, and if "abc" etc come from user inputs then your current code leaves you open to sql injection attacks.
 
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