Solved

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

Posted on 2010-11-09
8
861 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

19 Experts available now in Live!

Get 1:1 Help Now