Search code examples
sqlvb.netcursoroffice-interop

SQL get all Rows with Value VB.net


I have developed a word export function within my VB.net application which returns all rows that have a specific value in the column (service_name). However when looping through the function the cells in word always grabs the first service name instead of cycling through each one. I need a way in which I can point to the next value for the specific service_name.

enter image description here

enter image description here

I have given previews of the table I am grabing the value from and the word export knowing that there are 3 rows which match the value but always returning the first one.

Function used for returning SQL data:

Function getdataapplication(ByVal recordnum As Integer, ByVal fieldnum As Integer)
        ds.Reset()
        GC.Collect()

        Dim dbtable
        dbtable = "application_portfolio"

        Dim sql As MySqlCommand

        sql = New MySqlCommand("Select * from application_portfolio where service_name = '" & Wordexport.tbservicename.Text & "' ", dbcon)

        Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter()
        DataAdapter1.SelectCommand = sql


        DataAdapter1.Fill(ds, dbtable)
        dbcon.Close()
        sql.Dispose()
        Return ds.Tables(dbtable).rows(recordnum).Item(fieldnum - 1)
            'Return reader
    End Function

Public Sub Word_Click(sender As Object, e As EventArgs) Handles Word.Click

        Dim sqlrowcount As Integer
        sqlrowcount = CountRecords()
        tbcount.Text = sqlrowcount

        If application_portfolio.Checked = True Then
            'oWord.NewDocument.Application.Equals(Nothing)
            Dim oPara2application As Word.Paragraph
            Dim oTableapplication As Word.Table

            For i As Integer = 1 To sqlrowcount
                j = j + 1
                oTableapplication = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, j, 7)
                oTableapplication.Range.ParagraphFormat.SpaceAfter = 6
                oTableapplication.Rows(1).Range.Font.Bold = True
                oTableapplication.Cell(1, 1).Range.Text = "ID"
                oTableapplication.Cell(1, 2).Range.Text = "Service Name"
                oTableapplication.Cell(1, 3).Range.Text = "Application Name"
                oTableapplication.Cell(1, 4).Range.Text = "Application Type"
                oTableapplication.Cell(1, 5).Range.Text = "Cost"
                oTableapplication.Cell(1, 6).Range.Text = "Year Released"
                oTableapplication.Cell(1, 7).Range.Text = "Version"

                Dim tempdatastore = getdataapplication(0, 1)
                ListBox1.Items.Add(tempdatastore)
                oTableapplication.Cell(j, 1).Range.Text = functions.getdataapplication(0, 1)
                oTableapplication.Cell(j, 2).Range.Text = functions.getdataapplication(0, 2)
                oTableapplication.Cell(j, 3).Range.Text = functions.getdataapplication(0, 3)
                oTableapplication.Cell(j, 4).Range.Text = functions.getdataapplication(0, 4)
                oTableapplication.Cell(j, 5).Range.Text = functions.getdataapplication(0, 5)
                oTableapplication.Cell(j, 6).Range.Text = functions.getdataapplication(0, 6)
                oTableapplication.Cell(j, 7).Range.Text = functions.getdataapplication(0, 7)
                oTableapplication.Rows.Item(1).Range.Font.Italic = True
            Next
        End If
End function

Solution

  • Found the problem of your error. you keep on search for row 0 in your function getdataapplication

    oTableapplication.Cell(j, 1).Range.Text = functions.getdataapplication(0, 1)
    oTableapplication.Cell(j, 2).Range.Text = functions.getdataapplication(0, 2)
    oTableapplication.Cell(j, 3).Range.Text = functions.getdataapplication(0, 3)
    oTableapplication.Cell(j, 4).Range.Text = functions.getdataapplication(0, 4)
    oTableapplication.Cell(j, 5).Range.Text = functions.getdataapplication(0, 5)
    oTableapplication.Cell(j, 6).Range.Text = functions.getdataapplication(0, 6)
    oTableapplication.Cell(j, 7).Range.Text = functions.getdataapplication(0, 7)
    

    What you need to do is as follows

    oTableapplication.Cell(j, 1).Range.Text = functions.getdataapplication(i, 1)
    oTableapplication.Cell(j, 2).Range.Text = functions.getdataapplication(i, 2)
    oTableapplication.Cell(j, 3).Range.Text = functions.getdataapplication(i, 3)
    oTableapplication.Cell(j, 4).Range.Text = functions.getdataapplication(i, 4)
    oTableapplication.Cell(j, 5).Range.Text = functions.getdataapplication(i, 5)
    oTableapplication.Cell(j, 6).Range.Text = functions.getdataapplication(i, 6)
    oTableapplication.Cell(j, 7).Range.Text = functions.getdataapplication(i, 7)