AmyLouise
AmyLouise

Reputation: 1

How to 'insert' data into database via VB

Stuggling a bit here. Trying to develop the code to link user input into my database, in the form of a book record. for example the user would be asked to enter their name address etc. But the code I have used does not seem to execute because I continually get the same error.

Line 12:         Dim con As New SqlConnection
Line 13:         Dim inscmd As New SqlCommand
Line 14:         con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Database.My.MySettings.Database1ConnectionString1").ConnectionString
Line 15:         con.Open()
Line 16:         inscmd.CommandText = ("insert into booking values('" + txtfirstname.Text + "', " + txtSurname.Text + "', " + txtAddressline1.Text + "', " + txtAddressline2.Text + "', " + txtPostcode.Text + "', " + txtTime.Text + "', " + txtPeople.Text + "', " + txtDropoff1.Text + "', " + txtDropoff2.Text + "', " + txtDropoffpost.Text + "")

It is line 14 that contains the error but I dont know why. This is my code;

Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles  btnsubmit.Click
    Dim con As New SqlConnection
    Dim inscmd As New SqlCommand
    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Database.My.MySettings.Database1ConnectionString1").ConnectionString
    con.Open()
    inscmd.CommandText = ("insert into booking values('" + txtfirstname.Text + "', " + txtSurname.Text + "', " + txtAddressline1.Text + "', " + txtAddressline2.Text + "', " + txtPostcode.Text + "', " + txtTime.Text + "', " + txtPeople.Text + "', " + txtDropoff1.Text + "', " + txtDropoff2.Text + "', " + txtDropoffpost.Text + "")
    Print(inscmd.CommandText)
    inscmd.Connection = con
    inscmd.ExecuteNonQuery()
    con.Close()
    inscmd.Parameters.Clear()

    MsgBox("Your booking has been successfully")
    con.Close()

End Sub

Upvotes: 0

Views: 4848

Answers (4)

Nishantha Mahawatta
Nishantha Mahawatta

Reputation: 1

strSQL = "INSERT INTO user_account_details" & _
                "(lastname,firstname,middlename,usertype,reg_date_time,status)" & _
                " VALUES ( " & _
                " '" & txtLName.Text & "', " & _
                " '" & txtFName.Text & "' , " & _
                " '" & txtMName.Text & "' , " & _
                " '" & cboUserType.Text & "' , " & _
                " '#" & Now & "#', " & _
                " 'Inactive' " & _
                ")"

Upvotes: 0

Pakk
Pakk

Reputation: 1339

Hopefully this will help you ( insert your code where need to )

        Dim con As New SqlConnection
        Dim myConString As String = getSQLString() ' GET YOUR CON String

' my function looks like this when returned

"Server=ServerExactLocationPath;Database=DataBase;User Id=UserName;Password=Password;"

        Dim objcommand As SqlCommand = New SqlCommand
        'con.ConnectionString = myConString

        With objcommand
            .Connection = con
            Dim cmdText As String = ""
            cmdText = "Insert into SitesStatus (SiteNumber,StatusName,Date,ByUser) values ('" & site & "','" & status & "','" & System.DateTime.Today.ToString("MM/dd/yyyy") & "','" & dbUiInitials & "')"
        'PUT YOUR INSERT ABOVE

         .CommandText = cmdText
        End With
        con.ConnectionString = myConString
        con.Open()
        objcommand.ExecuteNonQuery()
        con.Close()

    Catch ex As Exception
      End Try
 Return Nothing

Upvotes: 1

PatFromCanada
PatFromCanada

Reputation: 2788

You should use the connection string wizard in the Project Settings window. Then try the test connection button, Make sure the type of the setting is ConnectionString You should be able to get the connection string using this syntax if things are set up right.

con.ConnectionString = my.Settings.Database1ConnectionString1

Upvotes: 0

Satya
Satya

Reputation: 8881

insert into booking values('" + txtfirstname.Text + "', " + txtSurname.Text + "', " + txtAddressline1.Text + "', " + txtAddressline2.Text + "', " + txtPostcode.Text + "', " + txtTime.Text + "', " + txtPeople.Text + "', " + txtDropoff1.Text + "', " + txtDropoff2.Text + "', " + txtDropoffpost.Text + "

should be

insert into booking values('" + txtfirstname.Text + "', '" + txtSurname.Text + "', '" + txtAddressline1.Text + "', '" + txtAddressline2.Text + "', '" + txtPostcode.Text + "', " + txtTime.Text + "', '" + txtPeople.Text + "', '" + txtDropoff1.Text + "', '" + txtDropoff2.Text + "', '" + txtDropoffpost.Text + "')"

Upvotes: 0

Related Questions