Solved

VB .net MySQL - Getting field names and valuse for each row in each field.

Posted on 2010-11-09
8
860 Views
Last Modified: 2012-05-10
Here is the cod I am using to get data from mysql and it does populate the DataGrid.  But how do I get individual row/field information assigned to a variable?  How do I get total rows returned?
 If connected Then
            Try
                'MsgBox("Viewing") ' debugging
                data = New DataTable

                dataAdap = New MySqlDataAdapter("SELECT * FROM events", conn)
                cmdBuild = New MySqlCommandBuilder(dataAdap)

                dataAdap.Fill(data)

            Catch ex As Exception
                MsgBox("Error COnnecting To The Database!" & ex.Message.ToString())
            End Try
            frmMain.dataGrid.DataSource = data

        End If

Open in new window

0
Comment
Question by:keith1001
  • 3
  • 3
  • 2
8 Comments
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 250 total points
ID: 34097101
Hi,

You can do a for loop agains tyou Datagrid like this

        Dim drRow As DataGridViewRow
        For Each drRow In frmMain.dataGrid.Rows
            MsgBox(drRow.Cells(0).Value)
        Next

Where you can change the Cells(0) can be the number of columns of the datagridview. To know the total rows returne use this

frmMain.dataGridRows.Count

Or you can do the same with the Datatable

        Dim drRow As DataRow
        For Each drRow In data.Rows
            MsgBox(drRow(0))
        Next

and fo the total rows returned

data.Rows.Count

0
 
LVL 7

Assisted Solution

by:rogerard
rogerard earned 250 total points
ID: 34097141
msgbox("Rows: " &  data.Rows.Count)

     For Each row As DataRow In tdtAHCS.Rows
                For Each col As DataColumn In tdtAHCS.Columns
                 ...
                Next
              Next


0
 

Author Comment

by:keith1001
ID: 34101246
Thanks guys for your help!  Both of you guys helped me out, so will accept both solutions.  

I have one last question though, how do I get the field names?
0
 
LVL 7

Expert Comment

by:rogerard
ID: 34101453
col.ColumnName.ToString
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 13

Expert Comment

by:gamarrojgq
ID: 34101980
If you are using the Datagridview will be

DataGridView1.Columns(0).DataPropertyName
0
 

Author Comment

by:keith1001
ID: 34102242
I am trying to get the database field names.

I did this, but dont get anything:

Dim col As DataColumn
Dim myfield As String

myfield = col.ColumnName.ToString()
MsgBox("Column Name" & myfield)
0
 
LVL 7

Assisted Solution

by:rogerard
rogerard earned 250 total points
ID: 34102315
You need to do this
If connected Then
            Try
                'MsgBox("Viewing") ' debugging
                data = New DataTable

                dataAdap = New MySqlDataAdapter("SELECT * FROM events", conn)
                cmdBuild = New MySqlCommandBuilder(dataAdap)

                dataAdap.Fill(data)

     For Each col As DataColumn In data.Columns
                 msgbox("Column Name" & col.columnname.tostring)
      Next

            Catch ex As Exception
                MsgBox("Error COnnecting To The Database!" & ex.Message.ToString())
            End Try
            frmMain.dataGrid.DataSource = data

        End If

 
0
 
LVL 13

Assisted Solution

by:gamarrojgq
gamarrojgq earned 250 total points
ID: 34102317
You are getting the right property but your COL object needs to be associated with your datatabe like this (using DATA as your datatable just like your first code example)


Dim col As DataColumn
Dim myfield As String

For Each col In data.Columns
     myfield = col.ColumnName.ToString()
     MsgBox("Column Name" & myfield)
Next
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now