write HTML file from VB.NET

Hi ,

I have my VB.NET writing the output in form of CSV file. Below is the code. It works fine and it gives me 20 records. Now I have to write the same content in form of HTML file with one thing extra. All the 20 rows will have one column "RecordIDFor1880" for which I have to give link. If user clicks it, it will open up some other page for which I know the path.
How can I achieve it?


Public Sub GenerateCSV()

            Dim OutputFileMSA As String = Application.StartupPath & "\CSVFiles\" & FirstLastName & ".csv"
            Dim myFileStream As New System.IO.FileStream(OutputFileMSA, _
           FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)
            'Create the stream writer
            Dim OutputFNMSA As New System.IO.StreamWriter(myFileStream)

            OutputFNMSA.WriteLine("Counter" & "," & "CountFromBlockedSet" & "," & "RecordIDFor1880" & "," & "FIRSTNAME" & "," & "LASTNAME" & "," & "Total Score")
            Dim connstr = "server=CSDAMAP1;uid=namematch;pwd=namematch;database=NameSearch;"
            Dim sqlConn4 As New SqlConnection(connstr)
            Dim rsSort_sqlcomm As New SqlCommand("SELECT * from ScoreTableNoQuotes order by TotalScore Desc", sqlConn4)
            Dim rsSortDR As SqlDataReader = rsSort_sqlcomm.ExecuteReader()
            Dim SortRecCount As Integer
            SortRecCount = 1

            While rsSortDR.Read()
                If SortRecCount <= 20 Then                  
                    OutputFNMSA.WriteLine(SortRecCount & "," & rsSortDR("CountFromBlockedSet") & "," & rsSortDR("RecordIDFor1880") & "," & rsSortDR("frstname") & "," & rsSortDR("lastname") & "," & rsSortDR("totalscore") & "," )
                    SortRecCount = SortRecCount + 1
                    Exit While
                End If
            End While

            OutputFNMSA = Nothing
            rsSortDR = Nothing
            sqlConn4 = Nothing
        Catch ex As Exception          
            MessageBox.Show(Err.Description, vbCritical, "JaroWeight")
        End Try
    End Sub
Who is Participating?
Bind your datareader to a datagrid and then construct the columns like so where edit is the column you want to open with the link.

<asp:datagrid id="dg" runat="server" AllowPaging="True" GridLines="Horizontal" CellPadding="5"
                              AutoGenerateColumns="False" BorderColor="DarkGray" HorizontalAlign="Left" AllowSorting="True">
                              <AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
                              <ItemStyle BackColor="WhiteSmoke"></ItemStyle>
                              <HeaderStyle Font-Bold="True"></HeaderStyle>
                                    <asp:TemplateColumn Visible="False" HeaderText="JobNumberID">
                                          <HeaderStyle Width="50px"></HeaderStyle>
                                                <%# DataBinder.Eval(Container.DataItem, "JobNumberID") %>
                                    <asp:TemplateColumn HeaderText="JobNumber">
                                          <HeaderStyle Width="50px"></HeaderStyle>
                                                <%# DataBinder.Eval(Container.DataItem, "JobNumber") %>
                                    <asp:TemplateColumn HeaderText="CustomerID">
                                          <HeaderStyle Width="50px"></HeaderStyle>
                                                <%# DataBinder.Eval(Container.DataItem, "CustomerID") %>
                                    <asp:TemplateColumn HeaderText="StartDate">
                                          <HeaderStyle Width="100px"></HeaderStyle>
                                                <%# DataBinder.Eval(Container.DataItem, "StartDate", "{0:MM-dd-yy}") %>
                                    <asp:TemplateColumn HeaderText="EndDate">
                                          <HeaderStyle Width="100px"></HeaderStyle>
                                                <%# DataBinder.Eval(Container.DataItem, "EndDate", "{0:MM-dd-yy}") %>
                                    <asp:TemplateColumn HeaderText="AssignedStaff">
                                          <HeaderStyle Width="150px"></HeaderStyle>
                                                <%# DataBinder.Eval(Container.DataItem, "AssignedStaff") %>
                                    <asp:TemplateColumn HeaderText="Description">
                                          <HeaderStyle Width="450px"></HeaderStyle>
                                                <%# DataBinder.Eval(Container.DataItem, "Description") %>
                                          <ItemStyle HorizontalAlign="Right"></ItemStyle>
                                                <asp:LinkButton id="EditItem" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                              <PagerStyle PageButtonCount="20" Mode="NumericPages"></PagerStyle>

And then in the codebehind:

Private Sub dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.ItemCommand
        If e.CommandName = "Edit" Then
            Response.Redirect("EditJob.aspx?jnid=" & CStr(dg.DataKeys(e.Item.ItemIndex)))
        End If
    End Sub

The CStr(dg.Datakeys(e.Item.ItemIndex))) will ensure your row selection


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.