1

Sorry in advance if this question seems strange or obvious but I am a very new to c#.

I'm making a web application with C# in visual studio, and I have an html table. I'd like to populate it with a table from sql server.

Here is my HTML Table:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs"  Inherits="MRAApplication.test" %>
<!DOCTYPE html>
<body>
<form runat="server">
<asp:Button ID="populateButton" runat="server" Text="Table" onClick="populateButton_Click" />
</form>
 <table id="table1">
    <thead>
        <tr>
            <th>AlertID</th>
            <th>AccountID</th>
            <th>Alert</th>
            <th>Active</th>
            <th>IsShow</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
</body>

And when I click the button I'd like the table to be filled with a datatable that I'm creating here:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace MRAApplication
{
public partial class test : System.Web.UI.Page
{
    protected void populateButton_Click(object sender, EventArgs e)
    {
        GetDataTable();
    }
    public static System.Data.DataTable GetDataTable()
    {
        string queryString = "Select * FROM Alerts";
        string conn =     ConfigurationManager.ConnectionStrings["UATConnectionString"].ToString();
        SqlDataAdapter adapter = new SqlDataAdapter(queryString, conn);
        System.Data.DataTable dt = new System.Data.DataTable();
        dt.TableName = "Table";
        using (System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(queryString, conn))
        {
            da.Fill(dt);
        }
        return dt;
    }
}
}

The UATConnectionString is working (I'm almost positive), so now I just need to get the data from the SQLtable I'm connecting to into the HTMLtable.

If anybody could help me I would appreciate it. If there is any more information needed I will be glad to help.

Ganesh Gaxy
  • 657
  • 5
  • 11
JaGo
  • 257
  • 1
  • 4
  • 12
  • http://stackoverflow.com/questions/19682996/datatable-to-html-table?rq=1 – Nick Aug 19 '14 at 16:10
  • @NicholasV. Thanks, I ran into that earlier, but what would be the best way to call the ConvertDataTableToHTML() function with our datatable? I tried putting "ConvertDataTableToHTML(dt);" after the sqlDataAdapter and before "return dt;" and it still isn't doing anything when I click the button. – JaGo Aug 19 '14 at 16:17

1 Answers1

2

You should use Repeater Control for this. You can see a very good example here: http://msdn.microsoft.com/en-us/library/zzx23804(v=vs.85).aspx

Georgi Bilyukov
  • 645
  • 4
  • 11
  • That does look like it would accomplish what I'm looking for. I'm not totally sure if I understand it but I'll try to make one using the example as a guide, thank you. +1 – JaGo Aug 19 '14 at 16:29