Listview To Database

Should be quite easy :P

Page 1 of 1

3 Replies - 3699 Views - Last Post: 22 June 2009 - 10:30 AM Rate Topic: -----

#1 matto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 18-April 09

Listview To Database

Post icon  Posted 21 June 2009 - 12:13 PM

Hi,

I have a list view which is populated with an MSACCESS database using ADO. I know how to import data from the database, and I know how to create new rows in the database. However, is it possible to save the contents of the listview to the databaes? Here's my code so far:

Adding A Row:
Dim cb As New OleDb.OleDbCommandBuilder(da)
			Dim dsNewRow As DataRow

			dsNewRow = ds.Tables("Accounts").NewRow()

			dsNewRow.Item("UserID") = txtUserReg.Text
			dsNewRow.Item("PassID") = txtPassReg.Text

			ds.Tables("Accounts").Rows.Add(dsNewRow)

			da.Update(ds, "Accounts")

			MessageBox.Show("Account added successfully", "Account Created", MessageBoxButtons.OK, MessageBoxIcon.Information)

			LoadDataBaseToListview()



Populate Listview With Database:
		imgIcons.Images.Add("account", My.Resources.user) 'Add user icon to imagelist
		' Load the data.
		' Open the database.
		Dim conn As OleDbConnection = GetDbConnection()

		' Select records.
		Dim cmd As New OleDbCommand("SELECT * FROM Accounts", conn) 'Select the Accounts table
		Dim data_reader As OleDbDataReader = cmd.ExecuteReader()
		Do While data_reader.Read()
			Dim account As New ListViewItem(data_reader.Item("UserID").ToString) 'Sets the username
			account.SubItems.Add(data_reader.Item("PassID").ToString) 'Sets the password
			lstAccounts.Items.Add(account)
			Me.lstAccounts.Items.Item(lstAccounts.Items.Count - 1).ImageKey = "account" 'Adds the image key "account" to the item "Username"
		Loop
		' Close the connection.
		conn.Close()


Is This A Good Question/Topic? 0
  • +

Replies To: Listview To Database

#2 AlienCoder  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 05-May 09

Re: Listview To Database

Posted 22 June 2009 - 04:54 AM

Whats in the listview that you want to write to the database, how many columns and so on, which ones do you want to write? Do you want to write to the same table ("Accounts")?
Was This Post Helpful? 0
  • +
  • -

#3 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Listview To Database

Posted 22 June 2009 - 05:04 AM

Idea: You can do it by a Loop such as For i as Integer = 0 to Listview1.Items.Count-1
Was This Post Helpful? 0
  • +
  • -

#4 matto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 18-April 09

Re: Listview To Database

Posted 22 June 2009 - 10:30 AM

Quote

Whats in the listview that you want to write to the database, how many columns and so on, which ones do you want to write? Do you want to write to the same table ("Accounts")?


2 columns - An item with 1 subitem. I want to write all of the items and subitems. I would like to write them to the same table (Accounts)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1