0

Syntax Error in Insert Into statement. pls give correct code for me.

Imports System.Data
Imports System.Data.OleDb
Partial Class login
    Inherits System.Web.UI.Page
    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim icount As Integer
    Dim str As String
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            cn = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("login.mdb") & ";")
            cn.Open()
            cmd = New OleDb.OleDbCommand("INSERT INTO user VALUES ('" & (TextBox1.Text) & "','" & (TextBox2.Text) & "')", cn)
            cmd.ExecuteNonQuery()
            MsgBox("data Inserted")
            cn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
End Class
5
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by RameshKumar.M
0

>Syntax Error in Insert Into statement. pls give correct code for me.

INSERT INTO TableName (Column1,Column2,Column3,...)  VALUES (@para1,@para2,@para3,...)
0

expanding adatapost solution
----------------------------------------
cmd.Parameters.Add(New SqlParameter("@para1", SqlDbType.VarChar, 10)).Value = TextBox1.Text

the red color is based on your table's field data type

Edited by boonfoo: use cmd instead of cn ( connection) - mistake is regreted

0

Syntax Error in Insert Into statement. pls give correct code for me.

Imports System.Data
Imports System.Data.OleDb
Partial Class login
    Inherits System.Web.UI.Page
    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim icount As Integer
    Dim str As String
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            cn = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("login.mdb") & ";")
            cn.Open()
            cmd = New OleDb.OleDbCommand("INSERT INTO user VALUES ('" & (TextBox1.Text) & "','" & (TextBox2.Text) & "')", cn)
            cmd.ExecuteNonQuery()
            MsgBox("data Inserted")
            cn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
End Class

-----------------------------------

is better to use parameter in the sqlcommand instead of using direct insert statement
like
insert into tablename values(@colname,@colnam2)

0
'Code is in VB.NET
Imports System.Data.OleDb
Public Class RegisterUser
       'Variables Decleration
       Public Con As OleDbConnection
       Public Cmd As OleDbCommand
       Public Dr As OleDbDataReader
       Public Da As OleDbDataAdapter
       Dim Email_id As Object
'TextBox(EmployeeID.Click)
	TBEmpID.Text = Trim(TBEmpID.Text)
'End Sub
'TextBox(EmailName.Click)
	TBEmailName.Text = Trim(TBEmailName.Text)
'End Sub
'Button(BClear.Click)
       TBEmpID.Text.Clear()
       TBEmpName.Text.Clear()
       TBEmailName.Text.Clear()
       TBPass.Text.Clear()
       TBCPass.Text.Clear()
       CBDomain.Text.Clear()
'End Sub
'WindowControlButton(Help.Click) (?)
	HelpForm.Show()
'End Sub


'Button(BRegister.Click)
Try
       Emailid = TBEmailName.Text & “” & CBDomain.Text
       If(TBEmpID.Text = String.Empty or TBEmpName.Text = String.Empty or TBEmailName.Text = String.Empty or TBPass.Text = String.Empty or TBCPass.Text = String.Empty) then
       MsgBox(“You Have miss to fill some fields”, MessageBox.Style = Exclamation)
       Else
		If(CBDomain.Text = String.Empty) then
			MsgBox(“Kindly Select the domain of your email”, MessageBox.Style = Exclamation)
       Else
       		If(TBPass.Text = TBCPass.Text)then
       		Con = New OleDbConnection(“Provider=Microsoft.ACE.OleDb.12.0; Data Source=DataBase\Dashbard.accdb; Presist Info Security = False”)
       		Con.Open()
       		Cmd = New OleDbCommand(“INSERT INTO EmpDatas(EmpID,Name,EmailID,Password) VALUES(‘” & TBEmpID.Text & ”’,’” & TBEmpName.Text & ”’ ,’” & Emailid & ”’ ,’” & TBPass.Text & ”’)”, Con)
       		Cmd.ExecuteNonQuery()
       MsgBox(“Data Saved Successfully”, MessageBox.Style= Information)
       Con.Close()
			Else
			MsgBox(“Password and ConfirmPassword is not matched. Please try again”, MessageBox.Style=Exclamation)
			TBPass.Text.Clear()
       TBCPass.Text.Clear()
			End If
       End If
       End If
Catch ex As Exception
       MsgBox(ex.Message)
End Try
EntryForm.Show()
Me.Close
'End Sub
End Class

Edited by RameshKumar.M: CODE IS IN VB.NET

0

Try this Command[\quote]

cmd = New OleDb.OleDbCommand("INSERT INTO user(DATABASE REQUIRED FIELDS HERE for the textbox1 & textbox2) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "')", cn)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.