Click here to Skip to main content
15,880,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
syntax error, Incorrect syntax near 'SELECT'

Table Name abc
Feilds:
col DataType

Date Datetime
a navarchar(max)

What I have tried:

This is my code. Please do needfull


 Dim regDate As Date = Date.Now
        Dim strDate As String = regDate.ToString("MM/dd/yyyy")
        sqlconn = New SqlConnection(str)
        sqlconn.Open()
        Try
            cmd = New SqlCommand("INSERT INTO abc (Date,a) values('" & regDate.ToString("MM/dd/yyyy") & "',SELECT sum(StockValue) from StockValue)", sqlconn)
            cmd.ExecuteNonQuery()
            MsgBox("Your Data Saved Succesfully")


        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        sqlconn.Close()
Posted
Updated 29-Jul-18 19:12pm

1 solution

Based on the posted code, I think the SELECT ... approach is more appropriate since the application is expecting a value from the table. See below.

VB
cmd = New SqlCommand("INSERT INTO abc (Date,a) SELECT '" & regDate.ToString("MM/dd/yyyy") & "', sum(StockValue) from StockValue", sqlconn)


The posted code might not subject to SQL injection, but you should try using parameterized query SqlCommand.Parameters Property (System.Data.SqlClient)[^]
 
Share this answer
 
Comments
Member 13313252 31-Jul-18 2:17am    
Sir But Date is not in StockValue Table. I want do Save System Date and a from StockValue Table
Bryian Tan 31-Jul-18 9:58am    
have you try the query?the date was being passed into the query not pulling out from the table.

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