I have written some lines of code to add/save data to my database using a save button, but each time I run the program and input my data into the various textboxes, I only see a dialog box "Records successfully saved" but nothing would display in my datagridview, I have check various site for help for non seems to be working ,can some help me and take a look at my lines of code, am still new to vb. I am making use of sql server compact 3.5, my datatable is "treatment_price".
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
Dim Query As String
'Query ="INSERT INTO Treatment_price"
Dim con As SqlServerCe.SqlCeConnection = New SqlServerCe.SqlCeConnection("Data Source= C:\Users\Chinedu\Documents\Visual Studio 2010\Projects\GUU Specialist Hospital 1\GUU Specialist Hospital 1\Treatment_price_unit.sdf")
Dim sql As SqlCeCommand = New SqlCeCommand("Query", con)
Query = " INSERT INTO Treatment_price(Patient_Name, " & "Patient Address, Admitted Date,Patients Date Of Birth,Blood Group_Genotype,Treatment1,Treament2,Treament3,Trement4,Treament5,Treament6,Unit_Price1,Unite_Price2,Unit_Price3,Unite_Price4,Unit_Price5,Unit_Price6,SubTotal,Tax,Total)VALUES('" & Patient_NameTextBox.Text & "','" & Patient_AddressTextBox.Text & "','" & Treatment1TextBox.Text & "','" & Treatment2TextBox.Text & "','" & Treatment4TextBox.Text & "','" & Treatment5TextBox.Text & "','" & Treatment6TextBox.Text & "','" & Unit_Price1TextBox.Text & "','" & Unit_Price2TextBox.Text & "','" & Unit_Price3TextBox.Text & "','" & Unit_Price4TextBox.Text & "','" & Unit_Price5TextBox.Text & "','" & Unit_Price6TextBox.Text & "','" & SubTotalTextBox.Text & "','" & TaxTextBox.Text & "','" & TotalTextBox.Text & "');"
con.Open()
Dim cmd As SqlServerCe.SqlCeCommand = New SqlServerCe.SqlCeCommand(Query, con)
cmd.ExecuteNonQuery()
Catch
If MsgBox("Record is Successfully Inserted") Then
Else
MsgBox("Record Not Succesfully Inserted,Please Check Missing Fields")
End If
con.Close()
End Try
End Sub
Ok I don't know for your error but you're handling exception the wrong way. Here is how you should do it (not to mention the SQL injection here) :
Try
' Your code here
Catch e as Exception
MsgBox.show(e.Message, "An Error occured")
End Try
NB : I post this here because it's too lon g for a comment...
Try this:
' Assumes that connection is a valid SqlConnection object.
'your query
Dim queryString As String = _
"SELECT Patient_Name,Patient_Address,AdmittedDate,PatientsDateOf Birth,Blood Group_Genotype,Treatment1,Treament2,Treament3,Trement4,Treament5,Treament6,Unit_Price1,Unite_Price2,Unit_Price3,Unite_Price4,Unit_Price5,Unit_Price6,SubTotal,Tax,Total FROM dbo.Treatment_price"
Dim adapter As SqlDataAdapter = New SqlDataAdapter( _
queryString, connection)
Dim dsTreatment_price As DataSet = New DataSet
adapter.Fill(dsTreatment_price)
'fill grid
datagridview1.dataSource = dsTreatment_price.tables(0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.