Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

to insert the values into database in runtime

keerthi23keerthi23 Posts: 7Member
hi,

i doing a project it is a personal manager i need to insert the data into database during runtime using oledbadapter but it shows a error in adapter
Try
Dim ad As New OleDb.OleDbDataAdapter("select * from addbook", cn)
Dim ds As New DataSet
ad.Fill(ds, "addbook")
If txtname.Text.Trim = "" Then
MsgBox("Please enter name")
Exit Sub
End If
If txtadd.Text.Trim = "" Then
MsgBox("Please enter address")
Exit Sub
End If
If txtphone.Text.Trim = "" Then
MsgBox("Please enter phone number")
Exit Sub
End If
If txtemail.Text.Trim = "" Then
MsgBox("Please enter the email address")
Exit Sub
End If
cn = New OleDb.OleDbCommand("insert into addbook values('" & txtname.Text & "','" & txtadd.Text & "','" & txtphone.Text & "','" & txtemail.Text & "')", cn)
Catch ex As Exception
MsgBox(ex.Message)
Finally
cn.Cancel()
End Try
End Sub


End Class

Comments

  • PavlinIIPavlinII Posts: 404Member
    : hi,
    :
    : i doing a project it is a personal manager i need to insert the data into database during runtime using oledbadapter but it shows a error in adapter
    : Try
    : Dim ad As New OleDb.OleDbDataAdapter("select * from addbook", cn)
    : Dim ds As New DataSet
    : ad.Fill(ds, "addbook")
    : If txtname.Text.Trim = "" Then
    : MsgBox("Please enter name")
    : Exit Sub
    : End If
    : If txtadd.Text.Trim = "" Then
    : MsgBox("Please enter address")
    : Exit Sub
    : End If
    : If txtphone.Text.Trim = "" Then
    : MsgBox("Please enter phone number")
    : Exit Sub
    : End If
    : If txtemail.Text.Trim = "" Then
    : MsgBox("Please enter the email address")
    : Exit Sub
    : End If
    : [blue][bold]cn =[/bold][/blue] New OleDb.OleDbCommand("insert into addbook values('" & txtname.Text & "','" & txtadd.Text & "','" & txtphone.Text & "','" & txtemail.Text & "')", cn)
    : Catch ex As Exception
    : MsgBox(ex.Message)
    : Finally
    : cn.Cancel()
    : End Try
    : End Sub
    :
    :
    : End Class
    :
    :
    Hi,
    can you give us ex.ToString value?

    Do you have
    [code]Option Strict On[/code] statement at the beggining of your VB file? If you do not, turn Option Strint On everytime! It will save you a lot of uneccessary problems
    I've put part of your code into blue, this is strange and can not work...
    I guess cn is SqlConnection and your're trying to assing SqlCommand to it.

    Another advices:
    1) Allway write column names into your insert statement.. If you add column into that table and do not update old code, old code will fail.
    [code]cmd = New OleDb.OleDbCommand("insert into addbook [bold](Name, Add, Phone, Email)[/bold] values('" & txtname.Text & "','" & txtadd.Text & "','" & txtphone.Text & "','" & txtemail.Text & "')", cn)

    2) Do not use '" & txtname.Text & "' to send string values into your command.
    If user enters "O'really", apostrophe characters causes your Slq command to throw incorrect syntax.
    And guess what will happen if I enter this name:

    xx','','',''); DELETE FROM addbook; --

    :)

    Hope this helps

    [blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]

    [purple]Don't take life too seriously anyway you won't escape alive from it![/purple]


  • cyberflashcyberflash Posts: 1Member
    What is the exact error that you encounter? In debugging errors, you have to be more specific.


    : hi,
    :
    : i doing a project it is a personal manager i need to insert the data into database during runtime using oledbadapter but it shows a error in adapter
    : Try
    : Dim ad As New OleDb.OleDbDataAdapter("select * from addbook", cn)
    : Dim ds As New DataSet
    : ad.Fill(ds, "addbook")
    : If txtname.Text.Trim = "" Then
    : MsgBox("Please enter name")
    : Exit Sub
    : End If
    : If txtadd.Text.Trim = "" Then
    : MsgBox("Please enter address")
    : Exit Sub
    : End If
    : If txtphone.Text.Trim = "" Then
    : MsgBox("Please enter phone number")
    : Exit Sub
    : End If
    : If txtemail.Text.Trim = "" Then
    : MsgBox("Please enter the email address")
    : Exit Sub
    : End If
    : cn = New OleDb.OleDbCommand("insert into addbook values('" & txtname.Text & "','" & txtadd.Text & "','" & txtphone.Text & "','" & txtemail.Text & "')", cn)
    : Catch ex As Exception
    : MsgBox(ex.Message)
    : Finally
    : cn.Cancel()
    : End Try
    : End Sub
    :
    :
    : End Class
    :
    :

Sign In or Register to comment.