Solved

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

Posted on 2010-11-09
8
867 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 7

Expert Comment

by:rogerard
ID: 34101453
col.ColumnName.ToString
0
 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET Textbox input validation 4 63
Access/Visual Basic Question 3 41
Data organization issue 7 36
Survey branching tutorial 11 38
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 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