2

I want to use HTML table instead of gridview to fecth the data. Why HTML Table? Because I'll be using the output for email sending so I prefer HTML Table instead of gridview. Also I don't want to use object as the system will run on the server only. It will automatically send an email. Can anyone help me with my problem? Thank you.

Here is what I have so far. On the example below I'm using gridview because I don't know how to do it using HTML Table using Append.

Vb.Net

This is how I call my function

 Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        SendEmail()
 End Sub

This is my email function that i want to be converted into html table using append.

Protects Sub SendEmail()
     For Each dt As System.Data.DataTable In prod3()
            Dim i As Integer = i + 1
            Dim dv As New System.Data.DataView(dt)
            Dim dt2 As System.Data.DataTable = dv.ToTable(False, {"Name", "Product", "Expiry"})
            Dim y As Date = dt.Rows(0)("pdate")
        Dim dte1, dte2, dte3 As String

            Select Case i
                Case 1
                    dte1 = dt.Rows(0)("pdate").ToString
                    dte1 = y.Date.ToString("MM/dd/yyyy")
                    dte1 = y
                    GridView11.DataSource = dt2
                    GridView11.DataBind()
                Case 2
                    dte2 = dt.Rows(0)("pdate").ToString
                    dte2 = y.Date.ToString("MM/dd/yyyy")
                    dte2 = y
                    GridView12.DataSource = dt2
                    GridView12.DataBind()
                Case 3
                    dte2 = dt.Rows(0)("pdate").ToString
                    dte2 = y.Date.ToString("MM/dd/yyyy")
                    dte2 = y
                    GridView13.DataSource = dt2
                    GridView13.DataBind()
            End Select

        Next
End SUb

Public Function prod3() As List(Of DataTable)

        Dim ds As New DataSet
        Dim cmd As New SqlCommand
        Dim ldt As New List(Of DataTable)
        Dim adp As SqlDataAdapter = New SqlDataAdapter
        Dim c As New SqlConnection("myconnection")
        cmd.Connection = c
        cmd.CommandText = "storedprocname"
        cmd.Parameters.AddWithValue("@name", "%")
        cmd.Parameters.AddWithValue("@product", "%")
        cmd.Parameters.AddWithValue("@expiry", "%")
        cmd.Parameters.AddWithValue("@datefrom", DateTime.Today.AddDays(1)) 
        cmd.Parameters.AddWithValue("@dateto", DateTime.Today.AddDays(3)) 
        cmd.Parameters.AddWithValue("@cost", "%")
        cmd.CommandType = CommandType.StoredProcedure
        adp.SelectCommand = cmd
        adp.Fill(ds)
        Dim dv As New DataView(ds.Tables(0))
        Dim dvfilter As DataTable = dv.ToTable(True, {"pdate"})
        For Each dtrow As DataRow In dvfilter.Rows
            Dim dt2 As New DataTable
            dv.RowFilter = "date =#" + dtrow("pdate") + "#"
            dt2 = dv.ToTable(False, {"DATE", "Name", "Product", "Expiry"})
            ldt.Add(dt2)
        Next
        Return ldt
    End Function

The code is working but not the way I want. I don't want to use gridview. I want it to be in html table like :

 Dim builder As New StringBuilder
        builder.Append("<!DOCTYPE html><html>")
        builder.Append("<head>")
        builder.Append("</head>")
        builder.Append("<body>")
        builder.Append("<table>")
        builder.Append("</table>")
        builder.Append("<body>")

Any help would be much appreciated! :) Thank you.

1
  • This is a false opposite. A GridView is a server side control that can render an HTML table. In other words, it can do what you ask without you having to write the HTML. Turn all the fancy options (like pagination) off, give it some column definitions, and voila. Commented Sep 29, 2016 at 5:11

1 Answer 1

1

As an option you can use Run-Time Text Template to create an email template. This way you can simply use a model for generating output using a template. It's like what you can do using ASP.NET MVC and Razor engine, but it's not limited to MVC or even ASP.NET. You can use this idea wherever you need to create a template.

Run-time text template works like a aspx page. For a person who know ASP.NET using t4 templates is really easy. It uses directives and tags and you mix content and code. You use code to make the output dynamic. Then it renders the content when you call its TransformText method.

You can use any type as Model. The model can be one of your business or view model classes or it can be a DataTable.

Example

Add a new class to your project:

Public Class Product
    Public Property Name As String
    Public Property Price As Integer
End Class

Add a new Run-Time Text Template (which is also known as Preprocessed Template) and name it MailTemplate. Then put this content to the file:

<#@ template language="VB" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ parameter type="System.Collections.Generic.List(Of Product)" name="Model"#>
<html>
<head>
    <title>Products</title>
    <style type="text/css">
        body { font-family: Calibri;width:400px;}
        table { text-align:center; }
        .container {width:400px;}
    </style>
</head>
<body>
<div class="container">
<h1 style="text-align:center;">List of Recent Products</h1><hr/>
Here is list of recent products:
<table style="width:100%">
    <tr><th>Index</th><th>Name</th><th>Price</th></tr>
    <# Dim index As Integer = 1
       For Each item in Model 
    #>
    <tr>
        <td><#=index#></td>
        <td><#=item.Name#></td>
        <td><#=item.Price#></td>
    </tr>
    <#     index = index + 1
       Next 
    #>
</table>
<div>
</body>
</html>

Use this code to generate output at runtime:

Dim template As New My.Templates.MailTemplate
template.Session = New Dictionary(Of String, Object)
Dim model = New List(Of Product)()
model.Add(New Product With {.Name = "Product 1", .Price = 100})
model.Add(New Product With {.Name = "Product 2", .Price = 100})
model.Add(New Product With {.Name = "Product 3", .Price = 100})
template.Session("Model") = model
template.Initialize()
Dim output = template.TransformText()

Now you can use output to send an email or write it to response.

The result would be:

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

Sorry forgot to mention that I am not allowed to use object or front end coding. I need to do this all at the back end. Do you have Idea how to achieve it?
It's completely server based coding. Also as I mentioned in the answer, you can use any type of model, including a DataTable. It's a really useful technique which can be applied in any kind of applications to have things like email templates.
sorry but I'm not familiar with that. I Don't know how I going to merge your suggestion with my codes. Can you please give me a sample using my code? Thank you.
I posted an step by step example which you can simply use. Just read the answer carefully and perform steps of the Example and let me know if you have any question about the answer :) - I believe it's a good opportunity for you to learn a new thing which is really simple while it's also really useful ;)
You're welcome and thanks for the feedback. You also have my vote :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.