I have Used following code to Insert Values in the table:

<%@ Import Namespace ="System.Data" %>

<%@ Import Namespace ="System.Data.SqlClient" %>

<script runat ="server" >

Sub Create(ByVal obj As Object, ByVal e As EventArgs)

Dim ConnString As String
Dim SQLstr As String


ConnString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\ERL.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

SQLstr = "INSERT INTO User(FName,LName,Gender,Bran_ID,Loc_ID,Dept_ID,Join_Dt, UserType_ID,User_ID,password,EmailID) VALUES(@txtFirstName,@txtLastName, @lstGender, @lstBranName, @lstLocName, @lstDeptName, @lstJnYear, @lstUserType, @txtUserID, @Pass1, @txtEmail)"

Dim SQLConn As New SqlConnection(ConnString)
Dim SQLCmd As New SqlCommand(SQLstr, SQLConn)

SQLConn.ConnectionString = ConnString
SQLConn.Open()

SQLCmd.Connection = SQLConn
SQLCmd.CommandText = SQLstr
SQLCmd.ExecuteNonQuery() //Here Visual Studio 2005 is giving error as "Incorrect syntax near the keyword 'User'"

SQLConn.Close()

End Sub

</script>

Pls Help......??

Recommended Answers

All 2 Replies

You got this because the parenthesis following the table name "Users" is too close, or touching, the table name.

Bad: User() Values ()

Good: User () Values ()

However, code update:

<script runat ="server" >

Sub Create(ByVal obj As Object, ByVal e As EventArgs)

Dim ConnString As String
Dim SQLstr As String


ConnString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\ERL.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

SQLstr = "INSERT INTO User (FName,LName,Gender,Bran_ID,Loc_ID,Dept_ID,Join_Dt, UserType_ID,User_ID,password,EmailID) VALUES(@txtFirstName,@txtLastName, @lstGender, @lstBranName, @lstLocName, @lstDeptName, @lstJnYear, @lstUserType, @txtUserID, @Pass1, @txtEmail)"

'You need to choose. This following line or the many after it.
'You're setting the same variables twice.
'This SQLConn, you are creating a new connection with the connection string, ConnString.
Dim SQLConn As New SqlConnection(ConnString)
'You are creating a new command with the following sql under the current connection.
Dim SQLCmd As New SqlCommand(SQLstr, SQLConn)

'Already set this
'SQLConn.ConnectionString = ConnString
SQLConn.Open()

'Already set these two.
'SQLCmd.Connection = SQLConn
'SQLCmd.CommandText = SQLstr
SQLCmd.ExecuteNonQuery()
SQLConn.Close()

End Sub
</script>

Also might be that there is no table called "User". Make sure that is spelled correctly, and verify the cases of the letters.

Dim var1 As Integer=SQLCmd.ExecuteNonQuery()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.