Display (Show) DataSet in View using VB.Net in ASP.Net MVC

luisguillermo965
 
on Sep 29, 2021 11:49 PM
Sample_195134.zip
363 Views

Good afternoon,

I want to be able to fill in my html table that is in a .cshtml file in Vb.net.

I have an sql query that returns a dataset to me.

<tbody>
    <tr>
        <td>ITEM</td>
        <td>VALUE 1</td>
        <td>VALUE 2</td>
        <td>VALUE 3</td>
        <td>VALUE 4</td>
    </tr>
</tbody>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 30, 2021 05:06 AM
on Sep 30, 2021 05:08 AM

Hi luisguillermo...,

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

Controller

Public Class HomeController
    Inherits Controller

    ' GET: Home
    Function Index() As ActionResult
        Return View(GetData())
    End Function

    Private Function GetData() As DataSet
        Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim query As String = "SELECT TOP 5 CustomerID,ContactName,Country FROM Customers"
        Using con As SqlConnection = New SqlConnection(conString)
            Dim cmd As SqlCommand = New SqlCommand(query)
            Using sda As SqlDataAdapter = New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using ds As DataSet = New DataSet()
                    sda.Fill(ds)
                    Return ds
                End Using
            End Using
        End Using
    End Function
End Class

View

@Code
    Layout = Nothing
End Code

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>CustomerId</th>
            <th>Name</th>
            <th>Country</th>
        </tr>
        <tbody>
            @For Each row In Model.Tables(0).Rows
                @<tr>
                    <td>@row("CustomerId")</td>
                    <td>@row("ContactName")</td>
                    <td>@row("Country")</td>
                </tr>
            Next
        </tbody>
    </table>
</body>
</html>

Screenshot