locked
Incorporating a Gridview code nested inside a SQL Store Procedure via VB.NET RRS feed

  • Question

  • User475570419 posted

    Good Afternoon,

    First I was wondering if the above is possible. I have my code on a search button that does an select from a database.  I want to include a gridview for the results.  But not use the Gridview that is provided from the asp.net tools.

    If anyone has any suggestions, please let me know.


    Thank you,

    Bob

    Tuesday, August 22, 2017 6:13 PM

Answers

  • User347430248 posted

    Hi Nighthawk071...,

    you not need to incorporate the grid view inside store procedure and you also mentioned that you don't want to use the girdview provided by tools.

    so you can try to display the data with in HTML Table.

    you can try to refer example below.

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    
        If Not Me.IsPostBack Then
    
            'Populating a DataTable from database.
    
            Dim dt As DataTable = Me.GetData()
    
     
    
            'Building an HTML string.
    
            Dim html As New StringBuilder()
    
     
    
            'Table start.
    
            html.Append("<table border = '1'>")
    
     
    
            'Building the Header row.
    
            html.Append("<tr>")
    
            For Each column As DataColumn In dt.Columns
    
                html.Append("<th>")
    
                html.Append(column.ColumnName)
    
                html.Append("</th>")
    
            Next
    
            html.Append("</tr>")
    
     
    
            'Building the Data rows.
    
            For Each row As DataRow In dt.Rows
    
                html.Append("<tr>")
    
                For Each column As DataColumn In dt.Columns
    
                    html.Append("<td>")
    
                    html.Append(row(column.ColumnName))
    
                    html.Append("</td>")
    
                Next
    
                html.Append("</tr>")
    
            Next
    
     
    
            'Table end.
    
            html.Append("</table>")
    
     
    
            'Append the HTML string to Placeholder.
    
           PlaceHolder1.Controls.Add(New Literal() With { _
    
              .Text = html.ToString() _
    
            })
    
        End If
    
    End Sub
    
     
    
    Private Function GetData() As DataTable
    
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    
        Using con As New SqlConnection(constr)
    
            Using cmd As New SqlCommand("SELECT CustomerId, Name, Country FROM Customers")
    
                Using sda As New SqlDataAdapter()
    
                    cmd.Connection = con
    
                    sda.SelectCommand = cmd
    
                    Using dt As New DataTable()
    
                        sda.Fill(dt)
    
                        Return dt
    
                    End Using
    
                End Using
    
            End Using
    
        End Using
    
    End Function

    in above example , if you check the GetData function you will find that there is a query to fetch the data.

    at same place you can try to call your procedure. everything else will remain same.

    Output:

    for full  code you can visit link below.

    Display data from database in HTML table in ASP.Net

    Regards

    Deepak

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 23, 2017 6:13 AM

All replies

  • User347430248 posted

    Hi Nighthawk071...,

    you not need to incorporate the grid view inside store procedure and you also mentioned that you don't want to use the girdview provided by tools.

    so you can try to display the data with in HTML Table.

    you can try to refer example below.

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    
        If Not Me.IsPostBack Then
    
            'Populating a DataTable from database.
    
            Dim dt As DataTable = Me.GetData()
    
     
    
            'Building an HTML string.
    
            Dim html As New StringBuilder()
    
     
    
            'Table start.
    
            html.Append("<table border = '1'>")
    
     
    
            'Building the Header row.
    
            html.Append("<tr>")
    
            For Each column As DataColumn In dt.Columns
    
                html.Append("<th>")
    
                html.Append(column.ColumnName)
    
                html.Append("</th>")
    
            Next
    
            html.Append("</tr>")
    
     
    
            'Building the Data rows.
    
            For Each row As DataRow In dt.Rows
    
                html.Append("<tr>")
    
                For Each column As DataColumn In dt.Columns
    
                    html.Append("<td>")
    
                    html.Append(row(column.ColumnName))
    
                    html.Append("</td>")
    
                Next
    
                html.Append("</tr>")
    
            Next
    
     
    
            'Table end.
    
            html.Append("</table>")
    
     
    
            'Append the HTML string to Placeholder.
    
           PlaceHolder1.Controls.Add(New Literal() With { _
    
              .Text = html.ToString() _
    
            })
    
        End If
    
    End Sub
    
     
    
    Private Function GetData() As DataTable
    
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    
        Using con As New SqlConnection(constr)
    
            Using cmd As New SqlCommand("SELECT CustomerId, Name, Country FROM Customers")
    
                Using sda As New SqlDataAdapter()
    
                    cmd.Connection = con
    
                    sda.SelectCommand = cmd
    
                    Using dt As New DataTable()
    
                        sda.Fill(dt)
    
                        Return dt
    
                    End Using
    
                End Using
    
            End Using
    
        End Using
    
    End Function

    in above example , if you check the GetData function you will find that there is a query to fetch the data.

    at same place you can try to call your procedure. everything else will remain same.

    Output:

    for full  code you can visit link below.

    Display data from database in HTML table in ASP.Net

    Regards

    Deepak

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 23, 2017 6:13 AM
  • User475570419 posted

    Thank you Deepak.  Your suggestion worked.  Sorry it took me a bit longer to reply that it worked.

    Wednesday, September 6, 2017 1:01 PM