0

i had problem in inserting data into a table in access database.i am using visual studio2005 for the projects.here is my code

Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" _
       & "c:\Users\BELLCONSULT\Documents\LIBRARY2.mdb "
        Dim cn As OleDb.OleDbConnection = New OleDb.OleDbConnection(strConnection)
Dim cmd As New OleDb.OleDbCommand
        With cmd
            .CommandText = "INSERT INTO PRACTISE(ID,NAMES,AGE,SEX,DATE_BIRTH,LEVEL) VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & ")"
            .Connection = cn
            .Connection.Open()
            .ExecuteNonQuery()
            .Connection.Close()
        End With
3
Contributors
5
Replies
12
Views
7 Years
Discussion Span
Last Post by pritesh2010
0

why are you writing so much of cmd it's simple for adding the data in access database see the code

Imports SYstem.Oledb

dim cn as OledbConnection
Dim cmd As OleDbCommand
dim sqlstr as string,ans as Boolean

private sub btn_click

Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & "c:\Users\BELLCONSULT\Documents\LIBRARY2.mdb "

cn  = New OleDbConnection(strConnection)
cn.open()
sqlstr=""INSERT INTO PRACTISE(ID,NAMES,AGE,SEX,DATE_BIRTH,LEVEL) VALUES('" & Trim(TextBox1.Text) & "','" & trim(TextBox2.Text) & "','" & trim(TextBox3.Text) & "','" & trim(TextBox4.Text) & "','" & trim(TextBox5.Text) & "','" & trim(TextBox6.Text) & "')"

''trim use for removing space
cmd=new OleDbCommand(sqlstr,cn)
ans=cmd.ExequteNonquery()
if (ans=true) then
''message added
else
'' error
End if
cmd=nothing  ''Disposing the object from memory
cn.close

end sub

try it will work

0

why are you writing so much of cmd it's simple for adding the data in access database see the code

Imports SYstem.Oledb

dim cn as OledbConnection
Dim cmd As OleDbCommand
dim sqlstr as string,ans as Boolean

private sub btn_click

Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & "c:\Users\BELLCONSULT\Documents\LIBRARY2.mdb "

cn  = New OleDbConnection(strConnection)
cn.open()
sqlstr=""INSERT INTO PRACTISE(ID,NAMES,AGE,SEX,DATE_BIRTH,LEVEL) VALUES('" & Trim(TextBox1.Text) & "','" & trim(TextBox2.Text) & "','" & trim(TextBox3.Text) & "','" & trim(TextBox4.Text) & "','" & trim(TextBox5.Text) & "','" & trim(TextBox6.Text) & "')"

''trim use for removing space
cmd=new OleDbCommand(sqlstr,cn)
ans=cmd.ExequteNonquery()
if (ans=true) then
''message added
else
'' error
End if
cmd=nothing  ''Disposing the object from memory
cn.close

end sub

try it will work

0

sorry just mistak happen write like this

sqlstr="INSERT INTO PRACTISE(ID,NAMES,AGE,SEX,DATE_BIRTH,LEVEL) VALUES('" & Trim(TextBox1.Text) & "','" & trim(TextBox2.Text) & "','" & trim(TextBox3.Text) & "','" & trim(TextBox4.Text) & "','" & trim(TextBox5.Text) & "','" & trim(TextBox6.Text) & "')"
0

Hi Guys im having some trouble using this code im having a few errors on it
Its about the undeclared variables, end of statement expected, name not declared
how do I fix those things

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.