Search code examples
mysqlexcelvb.netbasic

VB.NET How to list all Column in MYSQL into 1 Column in Excel using vb.net


I'm trying to list the return value from MySQL into excel using vb.net but my problem is only the first row is being inserted in Excel

Here is the return query from MySQL

Types of Learners Count
Grade 1 3
Grade 2 4

here is my code in VB.NET

Dim Type_of_Learners As String
        Dim List_TypesOfLearners_for_Today As String = "SELECT survey_at_what_blh as 'Type of Learners', COUNT(survey_at_what_blh) as COUNT
                                                        FROM daily_report 
                                                        GROUP BY survey_at_what_blh 
                                                        ORDER BY count DESC"
        da = New MySqlDataAdapter(List_TypesOfLearners_for_Today, mycon)
        dt = New DataTable()
        da.Fill(dt)
        Type_of_Learners = dt.Rows(0)("Type of Learners")
        xlNewSheet.Cells(66, 8) = Type_of_Learners

Should I use data set?


Solution

  • Dim Type_of_Learners As String
            Dim List_TypesOfLearners_for_Today As String = "SELECT survey_at_what_blh as 'Type of Learners', COUNT(survey_at_what_blh) as COUNT
                                                            FROM daily_report 
                                                            GROUP BY survey_at_what_blh 
                                                            ORDER BY count DESC"
            da = New MySqlDataAdapter(List_TypesOfLearners_for_Today, mycon)
            dt = New DataTable()
            da.Fill(dt)
           
    
    dim i_rowIndex as integer = 66 
    
    for each dr as datarow in dt.rows 
      Type_of_Learners = dr("Type of Learners").tostring.trim
      xlNewSheet.Cells(i_rowIndex , 8) = Type_of_Learners
      i_rowIndex += 1
    next