Delete record from Access database vb.net - vb.net

In this project I use Access database which is displayed in DataGridView. I am trying to delete acces row but i my looking for was not successful.
Code to delete record from DataGridView:
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim index As Integer
index = DataGridView1.CurrentCell.RowIndex
DataGridView1.Rows.RemoveAt(index)
ZoznamBindingSource.RemoveCurrent().
‘Dim da As OledbDataAdapter
‘Dim ds As dataSet
da.Update(ds)
End Sub
The last line of code give me an error: SystemNullReferenceException. I know rhe dataset is problem but i don’t know which code will replace it with.
Any solution?

The whole point of a BindingSource is that it is the one and only point of contact for bound data. You shouldn't have to touch the UI and you shouldn't have to touch the data source.
In your case, you should be calling RemoveCurrent on the BindingSource. That will flag the underlying DataRow as Deleted and then, when you call Update on your data adapter, the corresponding database record will be deleted.

Related

vb.net insert data from listcheckbox to datagridview when an item checked and deleted when unchecked item

When I check a car make the car_model appear from database and when I check the car_model it's added automatically to datagridview1.
My question is when I try to check another car_make to add the new car_model of this car make to the DataGridView all the DataGridView items are deleted due to the (DataGridView1.Rows.Clear()) that I put in the code.
However, when I remove this comand the code duplicates every thing, can you help me solving this problem.
Private Sub acar_modelbox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles car_modelbox.SelectedIndexChanged
Dim itemChecked1 As Object
For Each itemChecked1 In car_makelistbox.CheckedItems
DataGridView1.Rows.Clear()
Next
Dim itemChecked2 As Object
For Each itemChecked2 In car_modelbox.CheckedItems
DataGridView1.Rows.Add(combobox1.SelectedItem("Id").ToString, (combobox1.SelectedItem("carvin").ToString, itemChecked1.item("car_make").ToString, itemChecked2.item("car_model").ToString)
Next
End Sub

How to delete and clear the datagridview rows in vb 2010

I am developing a Point of Sale System in VB and using access as RDBMS. Everything went well however when it came to deletion i got stuck. I want to clear all the rows from the datagridview and want to delete it from database also. The new datagridview will store new items and i am sending that new data to print.I tried the below codes but all in vain.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
For i As Integer = 0 To SProductDataGridView.RowCount
SProductDataGridView.Rows.Remove(SProductDataGridView.Rows(0))
Next
End Sub
2nd Try
For i As Integer = 0 To SProductDataGridView.RowCount -1
ProductTableAdapter.DeleteProduct(IDTextBox.Text)
ProductTableAdapter.Fill(MyDS.product)
ProductTableAdapter.Dispose()
Next
Note : gridview.rows.clear() + gridview.ds = nothing , i tried that butgridview.rows.clear() doesn't work and gridview.ds = nothing just clear the gridview and is not deleting data from access database.
You can use either of below options:
Using DataTable
Using DataGridView
Using BindingSource
The logic of all solutions is removing the rows and then updating the table adapter.
Using DataTable
You can find each rows in the data table and delete it, then update the table adapter:
For Each row As DataRow In Me.TestDBDataSet.TestTable.Rows
row.Delete()
Next
Me.TestTableTableAdapter.Update(TestDBDataSet.TestTable)
Using DataGridView
For Each row As DataGridViewRow In Me.TestTableDataGridView.Rows
DirectCast(row.DataBoundItem, DataRowView).Delete()
Next
Me.TestTableTableAdapter.Update(TestDBDataSet.TestTable)
Using BindingSource
For Each item As Object In TestTableBindingSource
TestTableBindingSource.Remove(item)
Next
Me.TestTableTableAdapter.Update(TestDBDataSet.TestTable)

Simple, but I'm stuck ....Updating a DataSet using code

I have a simple Windows form in VB: textbox bound thru an adapter and a bindingsource to my dataset.
I have a button that on Click I want it to update the database. The form loads and the first data row shows in the textbox, I change the text then click my button but no update happens.
Any ideas what I'm doing wrong, or how I should do this??
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AToolsTableAdapter.Fill(Me.Qedsandb_TroyDataSet.aTools)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AToolsTableAdapter.Update(Qedsandb_TroyDataSet.aTools)
End Sub
End Class
Assuming the click event runs(?), TableAdapters based on a query (a join) do not, by default, have the ability to update the database. The name of your binding source suggests that you are using a query.
MSDN: TableAdapter Overview
The update functionality of a TableAdapter is dependent on how much
information is available based on the main query provided in the
TableAdapter Wizard. For example, TableAdapters that are configured to
fetch values from multiple tables (JOINs), scalar values, views, or
the results of aggregate functions are not initially created with the
ability to send updates back to the underlying database. However, you
can configure the INSERT, UPDATE and DELETE commands manually in the
Properties window.
You don't appear to be moving the data back from the form to the dataset. Try calling EndEdit on your bindingsource.

New added items on the datagridview cannot be seen at runtime

I have a program that inserts data in access database and the user can view the newly added items in a data grid view. After i add new items to the database it cannot be seen in the datagridview while the program is running. I have to stop the program and run it again just to see the changes i made.
Here is how i load the datagridview:
Public Class frmSupplies
Private Sub frmSupplies_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SuppliesDataSet.product_info' table. You can move, or remove it, as needed.
Me.Product_infoTableAdapter.Fill(Me.SuppliesDataSet.product_info)
End Sub
How can i view the newly added items while the program is still running?
I created a module that will call the "refresh" function for the datagridview. I added a new module to my project and added this codes:
Imports System.Data.OleDb
Module Module1
Dim con As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source= database path")
Sub REFRESHDGV()
Dim sql As String
sql = "SELECT * FROM [product info]"
Dim adapter As New OleDbDataAdapter(sql, con)
Dim dt As New DataTable("product info")
adapter.Fill(dt)
Form1.dgv1.DataSource = dt
End Sub
End Module
Hope this helps others!
Try wrapping around beginedit ... end edit
gridview.BeginEdit();
----
gridview.EndEdit();
I am not familiar with access (I use oracle). Oracle database can notify you if something has changes in your database, so you can refresh the datagridview. In your case you can have a button (for refresh the datagridView) or a timer and refresh it every x time (you know the time):
'I imagine that you have a bindingSource
bindingSource1.DataSource = Product_infoTableAdapter.GetData()
bindingSource1.ResetBindings(false)
Maybe you want to take a look in DataGridView DataBinding
When you bind an Access table to a grid on a Windows form and then change the data outside that application, the form or the grid simply do not have a way of knowing that data was changed, unless 1) they receive some kind of notification from Access, or 2) the application keep querying the data source to see if changes were made.
Option 1 there is not applicable with MS Access.
Your only option is to put a timer on the form to regularly check for changes (on a fixed interval) and reload the table and refresh the grid if changes were found.

update requires a valid updatecommand when passed datarow collection with modified rows

Am trying to update the access database with the values in the datagrid by the following command:
Private Sub btnUpdate_Click(ByVal source As Object, ByVal e As EventArgs) Handles btnUpdate.Click
Dim conn As New OleDbConnection(Con)
Dim bsource As BindingSource = New BindingSource()
Dim da As New OleDbDataAdapter
Dim dt As DataTable = ds.Tables("Config_access")
Me.DataGridView1.BindingContext(dt).EndCurrentEdit()
Me.da.Update(dt)
MsgBox("Table Updated")
End Sub
I am facing the error mentioned in the subjectline. Please suggest.
There are a few pieces you have missing. You need a command object and an update command for your data adapter. You'll need to know how to write an update SQL statement. It is suggested you use parameters to identify which row you want to update. Maybe your table has some sort of unique id field (Primary Key)? You can include some of the table information in your question to get feedback on how to do that.
You can start here to address the command error. If you run into other errors, you may want to repost.