5 Replies - 186919 Views - Last Post: 22 October 2009 - 08:50 AM Rate Topic: -----

#1 rida  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-October 08

how to add, delete and update database using datagridview

Posted 23 October 2008 - 09:04 PM

Hi all,
I am Rida, i have connected my access database to vb.net and program is capable to show the database in the datagridview. but what i want is when i change the data in the datagridview and click onto update button,changes i made should be saved in database so when i execute the application again i should be able to see the updated database in the datagridview.
guide me to add data as well as to delete data too
pls help me !!

here is the code..
i have three buttons on the form else then the datagridview.
button1=update
button2=add
button3=delete
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Windows.Forms.DataGridView





Public Class Form1
	Dim row As Integer
	Dim col As Integer
	Dim currentTime As System.DateTime = System.DateTime.Now
	Dim DataSet1 As New DataSet()
	'Dim com As New OleDb.OleDbCommandBuilder(OleDbDataAdapter1)
	Dim SQLString As String = "SELECT * FROM Table1"
	Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\members.accdb;User ID=admin"
	Dim OleDBConn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(ConnString)
	Dim OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString, OleDBConn1)
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		OleDBConn1.Open()
		OleDbDataAdapter1.Fill(DataSet1, "Table1")
		DataGridView1.DataSource = DataSet1.Tables("Table1")
		TextBox1.Text = currentTime
	End Sub



	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		DataSet1.Tables("Table1").AcceptChanges()
		OleDbDataAdapter1.Update(DataSet1, "table1")
		Dim item As New DataGridViewRow
		DataGridView1.AllowUserToAddRows = True
		item.CreateCells(DataGridView1)


		
		
	End Sub


	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		OleDbDataAdapter1.AcceptChangesDuringUpdate = True
		'OleDbDataAdapter1.UpdateCommand("table1")
		Dim j As Integer
		j = 0
		DataSet1.Tables("Table1").Rows(j).Item(1) = TextBox2.Text


	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		Dim i As Integer
		i = 0
		DataSet1.Tables("Table1").Rows(i).Delete()

	End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: how to add, delete and update database using datagridview

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: how to add, delete and update database using datagridview

Posted 24 October 2008 - 09:14 AM

Moved to VB.NET.
Was This Post Helpful? 0
  • +
  • -

#3 gymratz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 18-October 07

Re: how to add, delete and update database using datagridview

Posted 24 October 2008 - 09:51 AM

This may not be the answer you are looking for; however, I ran into the same problem a while back.
I used to populate my gridviews using vb.net and I have since changed to using the wizard when you add a GridView to your project. If you are pulling only from a single table it does all the code for update/delete for you. If you are pulling from multiple tables using joins (custom query instead of point and click) then it still prompts you for the code to delete and update.
Was This Post Helpful? 0
  • +
  • -

#4 ktkarunadasa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 02-March 09

Re: how to add, delete and update database using datagridview

Posted 02 March 2009 - 12:28 PM

YOU CAN UPDATE DATABASE THROUGH DATAGRIDVIEW. CONNECT TO YOUR DATABASE. ADD A DATAGRIDVIEW. USE FILL METHOD TO DISPLAY DATA.
CREATE A DATATABLE. CREATE A BINDING SOURCE. ATTACH THE DATA TO THE DATATABLE FROM DATAGRIDVIEW THROUGH BINDINGSOURCE.

THEN USE UPDATE METHOD OF DATAADAPTER TO UPDATE DATABASE.

VISIT THE FOLLOWING WEB SITE FOR DETAILS.


http://www.articlescloud.com
Was This Post Helpful? 1
  • +
  • -

#5 Damage  Icon User is online

  • Lord of Schwing
  • member icon

Reputation: 35
  • View blog
  • Posts: 1,105
  • Joined: 05-June 08

Re: how to add, delete and update database using datagridview

Posted 02 March 2009 - 01:56 PM

normally you'd have to insert the values from your datagrid view into the database table(something like below)

There are plenty of tutorials on this site, that can point you in the right direction

	Try
			Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Tables\Employees.accdb")
			conn.Open()

			Dim addUser As String = "INSERT INTO tblEmployee ( EmpID, EmpName, EmpPassword ) VALUES (" + txtEmpID.Text + ",'" + txtEmpName.Text + "','" +		 

txtEmpPassword.Text + "')"

			Dim cmd As OleDbCommand = New OleDbCommand(addUser, conn)
			
			cmd.ExecuteNonQuery()

			MsgBox("User added successfully")

			conn.Close()

		Catch ex As Exception
			MsgBox(ex.Message.ToString, , "Addition Error")

		End Try


Was This Post Helpful? 0
  • +
  • -

#6 stevejagerhorn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-October 09

Re: how to add, delete and update database using datagridview

Posted 22 October 2009 - 08:50 AM

Hi Rida,
I'm trying to do the same thing, can came across a note that stated that you shouldn't make the
DataSet1.Tables("Table1").AcceptChanges()
call before the update call because it resets all of the rows that were changed, to show no changes. So when you call the .Update command, it doesn't see any changes.

As I haven't resolved the issue either, I would take this information with a grain of salt. :)

Good Luck.
Steve

View Postrida, on 23 Oct, 2008 - 08:04 PM, said:

Hi all,
I am Rida, i have connected my access database to vb.net and program is capable to show the database in the datagridview. but what i want is when i change the data in the datagridview and click onto update button,changes i made should be saved in database so when i execute the application again i should be able to see the updated database in the datagridview.
guide me to add data as well as to delete data too
pls help me !!

here is the code..
i have three buttons on the form else then the datagridview.
button1=update
button2=add
button3=delete
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Windows.Forms.DataGridView





Public Class Form1
	Dim row As Integer
	Dim col As Integer
	Dim currentTime As System.DateTime = System.DateTime.Now
	Dim DataSet1 As New DataSet()
	'Dim com As New OleDb.OleDbCommandBuilder(OleDbDataAdapter1)
	Dim SQLString As String = "SELECT * FROM Table1"
	Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\members.accdb;User ID=admin"
	Dim OleDBConn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(ConnString)
	Dim OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString, OleDBConn1)
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		OleDBConn1.Open()
		OleDbDataAdapter1.Fill(DataSet1, "Table1")
		DataGridView1.DataSource = DataSet1.Tables("Table1")
		TextBox1.Text = currentTime
	End Sub



	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		DataSet1.Tables("Table1").AcceptChanges()
		OleDbDataAdapter1.Update(DataSet1, "table1")
		Dim item As New DataGridViewRow
		DataGridView1.AllowUserToAddRows = True
		item.CreateCells(DataGridView1)


		
		
	End Sub


	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		OleDbDataAdapter1.AcceptChangesDuringUpdate = True
		'OleDbDataAdapter1.UpdateCommand("table1")
		Dim j As Integer
		j = 0
		DataSet1.Tables("Table1").Rows(j).Item(1) = TextBox2.Text


	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		Dim i As Integer
		i = 0
		DataSet1.Tables("Table1").Rows(i).Delete()

	End Sub

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1