Results 1 to 4 of 4

Thread: Unable to insert data into sql database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    175

    Unable to insert data into sql database

    I'm sure that I'm missing something really basic here, but I'm just not seeing it. I have the following code:

    Code:
        Public Sub InsertRow()
            Dim employeenumber As String = employeeListBox.SelectedValue
            Dim headsetnumber As String = headsetnumberText.Text
            Dim amplifiernumber As String = amplifiernumberText.Text
            Dim returnreason As String = returnreasonDropDown.SelectedValue
            Dim notes As String = notesText.Text
    
            Dim myConnectionString As String = "Initial Catalog=xxxxx;Data Source=xxxxx;uid=xxxxx;password=xxxxx;"
            Dim myConnection As New SqlConnection(myConnectionString)
            Dim insertcommandText As String = _
            "insert into equipment (employeenumber, headsetnumber, amplifiernumber, returnreason, notes) " & _
            "values(employeenumber, headsetnumber, amplifiernumber, returnreason, notes)"
    
            Using connection As New SqlConnection(myConnectionString)
                Dim command As New SqlCommand(insertcommandText, connection)
    
                Try
                    connection.Open()
                    command.ExecuteNonQuery()
                    thankyouLabel.Text = "Your data has been submitted."
    
                Catch ex As Exception
                    If myerrormessageLabel.Text = ex.Message Then
                        thankyouLabel.Text = ""
                    End If
                End Try
            End Using
    
        End Sub
    and when I put a break point on the command.ExecuteNonQuery line, and then step through that, I'm never displayed the thankyouLabel.Text nor is my data being inserted into the database. Can someone point out what I may be missing?

    Thank you

    Doug

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Unable to insert data into sql database

    this line is wrong:

    insert into equipment (employeenumber, headsetnumber, amplifiernumber, returnreason, notes) " & _
    "values(employeenumber, headsetnumber, amplifiernumber, returnreason, notes)"


    Is should be:

    insert into equipment (employeenumber, headsetnumber, amplifiernumber, returnreason, notes) " & _
    "values(" & employeenumber & "," & headsetnumber & "," & amplifiernumber & "," & returnreason & "," & notes & ")"

    if Notes is text then you need to enclose in single qoutes
    same for any of these things actually
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Unable to insert data into sql database

    you should be getting an exception. try this.
    Code:
        Public Sub InsertRow()
            Dim employeenumber As String = employeeListBox.SelectedValue
            Dim headsetnumber As String = headsetnumberText.Text
            Dim amplifiernumber As String = amplifiernumberText.Text
            Dim returnreason As String = returnreasonDropDown.SelectedValue
            Dim notes As String = notesText.Text
    
            Dim myConnectionString As String = "Initial Catalog=xxxxx;Data Source=xxxxx;uid=xxxxx;password=xxxxx;"
            Dim myConnection As New SqlConnection(myConnectionString)
            Dim insertcommandText As String = _
            "insert into equipment (employeenumber, headsetnumber, amplifiernumber, returnreason, notes) " & _
            "values(@employeenumber, @headsetnumber, @amplifiernumber, @returnreason, @notes)"
    
            Using connection As New SqlConnection(myConnectionString)
                Dim command As New SqlCommand(insertcommandText, connection)
                command.parameters.addwithvalue("@employeenumber",employeenumber)
                command.parameters.addwithvalue("@headsetnumber",headsetnumber)
                command.parameters.addwithvalue("@amplifiernumber",amplifiernumber)
                command.parameters.addwithvalue("@returnreason",returnreason)
                command.parameters.addwithvalue("@notes",notes)
    
    
    
                Try
                    connection.Open()
                    command.ExecuteNonQuery()
                    thankyouLabel.Text = "Your data has been submitted."
    
                Catch ex As Exception
                    If myerrormessageLabel.Text = ex.Message Then
                        thankyouLabel.Text = ""
                    End If
                End Try
            End Using
    
        End Sub
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    175

    Re: Unable to insert data into sql database

    Wild Bill,

    thanks ... that did it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width