problem Displaying data in Datagrid using DataReader

this code doesnt display the row of data in to datagrid

Page 1 of 1

2 Replies - 12824 Views - Last Post: 27 March 2008 - 11:34 PM Rate Topic: -----

#1 don_omar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-March 08

problem Displaying data in Datagrid using DataReader

Post icon  Posted 27 March 2008 - 06:04 AM

Hi, My name is Omar, I'm trying to create a datagrid that displays data from a mysql database using a datareader.

Basically I have 5 columns ("name", "sex", "age", "location" & "height" in the table ("message"), I need the datareader loop continuously and pick the rows of data. And the data grid to display the row of data as a link label. All assistance is greatly appreciated.
Thanks Regards
Omar


Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		MySQLConnectionString = My.Settings.MySQLConnectionString
		MyADOConnection.ConnectionString = MySQLConnectionString
		MyADOConnection.Open()
		Call MySqlReadData(MyADOConnection)

	End Sub



Public Sub MySqlReadData(ByVal MyADOConnection As MySqlConnection)

		Dim MySqlCommand As New MySqlCommand("SELECT * FROM message", MyADOConnection)
		Dim MySqlDataReader As MySqlDataReader = MySqlCommand.ExecuteReader(Commandbehavior.CloseConnection)

		Try
			Do
				While (MySqlDataReader.Read())
					MySqlDataGrid.DataSource = MySqlDataReader

				End While
			Loop While MySqlDataReader.NextResult()
			MySqlDataReader.Close()
		Catch MyError As MySqlException
			MessageBox.Show("Error connecting to the database")
		End Try

	End Sub

This post has been edited by don_omar: 27 March 2008 - 07:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem Displaying data in Datagrid using DataReader

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: problem Displaying data in Datagrid using DataReader

Posted 27 March 2008 - 07:41 AM

A data source is usually something in memory, not a stream of any kind.

The standard method for doing this kind of thing is to load all the data into an object and then bind to that object. The standard object for this is a DataSet.

Here's one way to do what you're trying to do:
   Public Sub MySqlReadData(ByVal MyADOConnection As MySqlConnection)
      Dim dataAdapter As New MySqlDataAdapter("SELECT * FROM message", MyADOConnection)
      Dim ds As New DataSet()
      Try
         If (dataAdapter.Fill(ds) > 0) Then
            MySqlDataGrid.DataSource = ds.Tables(0)
         End If
      Catch MyError As MySqlException
         MessageBox.Show("Error connecting to the database")
      End Try
   End Sub



Hope this helps.
Was This Post Helpful? 1

#3 don_omar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-March 08

Re: problem Displaying data in Datagrid using DataReader

Posted 27 March 2008 - 11:34 PM

Hi Baavgai, Thanks very much for your assistance, It works very well.
How do I make the dataset loop so that on load it keeps picking new data over and over?
Do you put the call procedure into a loop such that on load the call procedure keeps displaying the new data?
Or is the put into the private sub?
How do I modify the code to call data constantly form the database?
Or is a timer controlled data set the answer

This post has been edited by don_omar: 28 March 2008 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1