How to refresh Database Connections
-
Hello, I would like to ask how can I show what I have recently updated in my database I just use textboxe's to show my data but my problem is that I does not show it yet until I restart my program.Not like my delete command that it immediately affect what changes I have made in the database. Problem Conclusion: How to immediately show changes made after updating the database For more details below here are my codes: My Declarations:
Dim con As New OleDb.OleDbConnection
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
Dim sSQL As String
Dim maxRows As Integer
Dim inc As IntegerMy sub to display my database records to textbox:
Private Sub setRecords()
Try
'set the specific records
txtProduct_ID.Text = dt.Rows(inc).Item("productID")
txtProductName.Text = dt.Rows(inc).Item("productName")
txtProductDescription.Text = dt.Rows(inc).Item("productDescription")
txtProductCategory.Text = dt.Rows(inc).Item("productCategory")
txtProductStatus.Text = dt.Rows(inc).Item("productStatus")
txtProductPrice.Text = dt.Rows(inc).Item("productPrice")
txtProductQuantity.Text = dt.Rows(inc).Item("productQuantity")
datePicker.Text = dt.Rows(inc).Item("productDateAdded")
'disable editing when form in load
txtProduct_ID.Enabled = False
txtProductName.Enabled = False
txtProductDescription.Enabled = False
txtProductCategory.Enabled = False
txtProductStatus.Enabled = False
txtProductPrice.Enabled = False
txtProductQuantity.Enabled = False
datePicker.Enabled = False
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End SubMY code in update:
con.Close()
Try con.Open() Try 'add a new row Dim Rows As DataRow Rows = dt.NewRow Rows.Item("productID") = Val(txtProduct\_ID.Text) Rows.Item("productName") = txtProductName.Text Rows.Item("productCategory") = txtProductCategory.Text Rows.Item("productDescription") = txtProductDescription.Text Rows.Item("productStatus") = txtProductStatus.Text Rows.Item("productPrice") = Val(txtProductPrice.Text) Rows.Item("productQuantit
-
Hello, I would like to ask how can I show what I have recently updated in my database I just use textboxe's to show my data but my problem is that I does not show it yet until I restart my program.Not like my delete command that it immediately affect what changes I have made in the database. Problem Conclusion: How to immediately show changes made after updating the database For more details below here are my codes: My Declarations:
Dim con As New OleDb.OleDbConnection
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
Dim sSQL As String
Dim maxRows As Integer
Dim inc As IntegerMy sub to display my database records to textbox:
Private Sub setRecords()
Try
'set the specific records
txtProduct_ID.Text = dt.Rows(inc).Item("productID")
txtProductName.Text = dt.Rows(inc).Item("productName")
txtProductDescription.Text = dt.Rows(inc).Item("productDescription")
txtProductCategory.Text = dt.Rows(inc).Item("productCategory")
txtProductStatus.Text = dt.Rows(inc).Item("productStatus")
txtProductPrice.Text = dt.Rows(inc).Item("productPrice")
txtProductQuantity.Text = dt.Rows(inc).Item("productQuantity")
datePicker.Text = dt.Rows(inc).Item("productDateAdded")
'disable editing when form in load
txtProduct_ID.Enabled = False
txtProductName.Enabled = False
txtProductDescription.Enabled = False
txtProductCategory.Enabled = False
txtProductStatus.Enabled = False
txtProductPrice.Enabled = False
txtProductQuantity.Enabled = False
datePicker.Enabled = False
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End SubMY code in update:
con.Close()
Try con.Open() Try 'add a new row Dim Rows As DataRow Rows = dt.NewRow Rows.Item("productID") = Val(txtProduct\_ID.Text) Rows.Item("productName") = txtProductName.Text Rows.Item("productCategory") = txtProductCategory.Text Rows.Item("productDescription") = txtProductDescription.Text Rows.Item("productStatus") = txtProductStatus.Text Rows.Item("productPrice") = Val(txtProductPrice.Text) Rows.Item("productQuantit
All in all, you have Delete, Insert, Update in a class, so you can declare an Event (Modified), and subscribe to it. When it fires, call setRecords sub. In C# it looks as follows:
public event EventHandler DatabaseDataModified;
protected virtual void OnDatabaseDataModified(EventArgs args)
{
if(DatabaseDataModified != null)
DatabaseDataModified.Invoke(this, args);
}Call
OnDatabaseDataModified
method everytime a record is updated in the database. Subscribe toDatabaseDataModified
event and call setRecords sub, once the event handler fires. For a more detailed review check publish/subscribe model.