I have a SQL Server CE database with a table name records and 22 columns and in my form in vb.net I have 19 textboxes and 3 datetimepickers for each of the respective textboxes and also a bound dgv, which I dragged from the query of my tables.
When I run the program and input data into the textboxes and click save it save with the msg "record saved" as programmed but nothing displays in my dgv
Here's my code:
Imports System
Imports System.Data
Imports System.Data.SqlServerCe
Public Class Form1
Dim Cmd As SqlCeCommand
Dim con As SqlCeConnection
Dim affector As Integer
Dim ConnectionString As String = ("Data Source=C:\Users\chinedu\documents\visual studio 2010\Projects\GUU SPECIALIST HOSPITAL 2\GUU SPECIALIST HOSPITAL 2\Database1.sdf")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Database1DataSet.PATIENT_RECORD' table. You can move, or remove it, as needed.'
Dim strconnection As String = ("Data Source=C:\Users\chinedu\documents\visual studio 2010\Projects\GUU SPECIALIST HOSPITAL 2\GUU SPECIALIST HOSPITAL 2\Database1.sdf")
Try
con = New SqlServerCe.SqlCeConnection("strconnection")
Dim sql As SqlServerCe.SqlCeCommand = New SqlServerCe.SqlCeCommand("Query", con)
con.Open()
If con.State = ConnectionState.Closed Then
MessageBox.Show("Database has failed to connect. System will now terminate.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
End
Else
Exit Sub
End If
Catch
MsgBox("Connection Established")
End Try
End Sub
'save button'
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
Try
con = New SqlCeConnection(ConnectionString)
con.Open()
sCommand = New SqlCeCommand("INSERT INTO RECORD([Patient_name],[Patient_Address],[Patient_Date_Of_Birth],[Patient_Phone Number],[Patient_CardNO],[Blood_Group],[Date_Admitted],[Date_Discharged],[Treatment1],[Treatment2],[Treatment3],[Treatment4],[Treatment5],[Treatment6],[Price_Unit1],[Price_Unit2],[Price_Unit3],[Price_Unit4],[Price_Unit5],[Price_Unit6],[Sub_total],[VAT],[Prev_bal],[Net_Total]) values ('" + Patient_AddressTextBox.Text + "','" + Patient_AddressTextBox.Text + "','" + Patient_Date_Of_BirthDateTimePicker.Value.Date + "','" + Phone_NumberTextBox.Text + "','" + Hospital_NumberTextBox.Text + "','" + Blood_Group_TextBox.Text + "','" + Date_AdmittedDateTimePicker.Value.Date + "','" + Date_DischargedDateTimePicker.Value.Date + "','" + Treatment1TextBox.Text + "','" + Treatment2TextBox.Text + "','" + Treatment3TextBox.Text + "','" + Treatment4TextBox.Text + "','" + Treatment5TextBox.Text + "','" + Treatment6TextBox.Text + "','" + Sub_TotalTextBox.Text + "','" + VATTextBox.Text + "','" + Previous_BalTextBox.Text + "','" + Net_TotalTextBox.Text + "',)", con)
affector = sCommand.ExecuteNonQuery
Catch
MessageBox.Show("New record has been saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
Private Sub BtnRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRecord.Click
strcon = ("Data Source= C:\Users\chinedu\documents\visual studio 2010\Projects\GUU SPECIALIST HOSPITAL 2\GUU SPECIALIST HOSPITAL 2\Database1.sdf")
con = New SqlCeConnection(strcon)
Dim query As String = " SELECT * from Records"
Dim adpt As New SqlCeDataAdapter(query, con)
Dim ds As New Database1DataSet()
adpt.Fill(ds, "Records")
RecordsDataGridView.DataSource = ds.Tables(0)
You need to DataBind()
your GridView
In Your BtnRecord_Click
event should be like this
RecordsDataGridView.DataSource = ds.Tables(0)
RecordsDataGridView.DataBind() `This is your missing line.
Also you can make it to load automatically after a new record is added. Just add those 2 lines to your Button4_Click
event
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.