Ceejay
Ceejay

Reputation: 7267

How to get the dataset values to custom table in c#

here i am trying to get the values retrieved from database to my custom table format. right now i am storing values to dataset.

 protected void Page_Load(object sender, EventArgs e)
    {
        String userID = Convert.ToString(Session["user_id"]);        
        if (string.IsNullOrEmpty(userID) == true)
        {            
            Response.Redirect("login.aspx");
        }

        try { 
            string scon="SERVER=localhost;DATABASE=bmtc;UID=root;";
            MySqlConnection con = new MySqlConnection(scon);
            String s = "select * from application";
            MySqlDataAdapter dat = new MySqlDataAdapter(s, con);
            DataSet ds = new DataSet();
            dat.Fill(ds,"tbl");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }

        catch(Exception ex){
           // Label1.Text = ex.ToString();
    }
}

Asp code:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
        GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

I need to save each individual values in table. like

<table class="custom-table" id="preview-table">
<tr><td>Name</td><td><% value1 %></td></tr>
<tr><td>Address</td><td><% value2 %></td></tr>
<tr><td>Phone</td><td><% value3 %></td></tr>
....
</table>

how can i do this?

Upvotes: 0

Views: 166

Answers (2)

Govinda Rajbhar
Govinda Rajbhar

Reputation: 3034

You can use listview

<asp:ListView ID="listview1" runat="server" ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
    <table class="custom-table" id="preview-table">
        <tr>
            <th>
                Header1
            </th>
            <th>
                Header2
            </th>
            <th>
                Header3
            </th>
        </tr>
        <tr><asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder></tr>            
    </table>
</LayoutTemplate>

<ItemTemplate>
    <td>
        <%# Eval("Column1") %>
    </td>
    <td>
        <%# Eval("Column2") %>
    </td>
    <td>
        <%# Eval("Column3") %>
    </td>
</ItemTemplate>
</asp:ListView>

At Server side code :

protected void Page_Load(object sender, EventArgs e)
    {
        String userID = Convert.ToString(Session["user_id"]);        
        if (string.IsNullOrEmpty(userID) == true)
        {            
            Response.Redirect("login.aspx");
        }

        try { 
            string scon="SERVER=localhost;DATABASE=bmtc;UID=root;";
            MySqlConnection con = new MySqlConnection(scon);
            String s = "select * from application";
            MySqlDataAdapter dat = new MySqlDataAdapter(s, con);
            DataSet ds = new DataSet();
            dat.Fill(ds,"tbl");
            listview1.DataSource = ds.Tables[0];
            listview1.DataBind();
        }

        catch(Exception ex){
           // Label1.Text = ex.ToString();
    }
}

For More information follow below links

listView Example, listView Example 2

Upvotes: 1

Rahul Singh
Rahul Singh

Reputation: 21805

As mentioned in my comment, an ASP.NET Gridview control is anyways rendered as Html table in browser, so their is no need to define a custom table separately. Now, as it stands by default the AutoGenerateColumns property of gridview is true so if you simply bind the data programatically (as you are doing now), it will render a table.

If you don't wish to display any specific column then either don't fetch it from DB or else set the AutoGenerateColumns property to false and add BoundField for each column.

Although, there are some issues with your current code, first you should only be binding the data on initial get request i.e don't bind the data after every postback:-

protected void Page_Load(object sender, EventArgs e)
    {
       //code to get user_id
       If(!PostBack)
       {
           //code to bind gridview.
       }
  }

Apart from this, you should store the connection string in Web.Config file. You should consider using the using statement to dispose expensive db related objects.

Upvotes: 0

Related Questions