|
-
May 13th, 2011, 01:54 PM
#1
Thread Starter
Addicted Member
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
-
May 13th, 2011, 01:58 PM
#2
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
-
May 13th, 2011, 02:01 PM
#3
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
-
May 13th, 2011, 02:14 PM
#4
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|