SIGN UP MEMBER LOGIN:    
ARTICLE

Data Binding from dataset to HTML table in .Net

Posted by Farooque Ali Articles | ADO.NET in C# September 27, 2010
In this article we are going to discuss Databinding from a Dataset to a HTML table during RUNTIME, if the table contains more than one detail for an order.
Reader Level:

Introduction:

In this article we are going to discuss Databinding from a Dataset to a HTML table during RUNTIME, if the table contains more than one detail for an order.

Below I have explained the things.

Create any table (I have mentioned my table below) in SQL database and bind the values into the new dataset. From the DataSet we need to bind the values into the table.

Table: Sample
  • Title Varchar
  • Qty int
  • Downloadable Varchar
  • Shipped Varchar
  • Price int
  • Shipping int
  • Tax int
  • Total int
Below code(Written in CodeBehind) is used to Bind the values in to the HTML table during RUNTIME:

public void BindData(int iOrderId)
{
    DataSet ds = new DataSet();
    ds = objorders.GetOrderStatus(iOrderId);
    if (ds.Tables.Count > 0)
    {
        Response.Write("<table border='1' cellpadding='2'  WIDTH='75%' ");
        Response.Write("<tr><th>Title</th><th>Qty</th><th>Downloadable?</th><th>Shipped?</th><th>Price</th><th>Shipping</th><th>Tax</th><th>Total</th>");
        Response.Write("</tr>");
        if (ds.Tables.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Response.Write("<tr>");
                Response.Write("<td> " + ds.Tables[0].Rows[i]["product_name"].ToString() + " </td>");
                Response.Write("<td> " + ds.Tables[0].Rows[i]["Product_Qty"].ToString() + " </td>");
                Response.Write("<td> " + "NO" + "  </td>");
                Response.Write("<td> " + "YES" + "  </td>");
                Response.Write("<td> " + ds.Tables[0].Rows[i]["Product_Cost"].ToString() + " </td>");
                Response.Write("<td> " + 1.00 + " </td>");
                Response.Write("<td> " + 1.00 + "  </td>");
                Response.Write("<td> " + ds.Tables[0].Rows[i]["store_product_final_cost_total"].ToString() + " </td>");
                Response.Write("</tr>");
            }
            Response.Write("<tr>");
            Response.Write("<td colspan=5> Order-Level shiping cost() </td>");
            Response.Write("<td> " + 1.00 + "  </td>");
            Response.Write("<td> " + 1.00 + "  </td>");
            Response.Write("<td> " + ds.Tables[0].Rows[0]["store_product_final_cost_total"].ToString() + " </td>");
            Response.Write("</tr>");
            Response.Write("<tr>");
            Response.Write("<td colspan=7> Total on Discover xxxxxxxxxxcc9124 </td>");
            Response.Write("<td> " + ds.Tables[0].Rows[0]["store_order_final_cost_total"].ToString() + " </td>");
            Response.Write("</tr>");
            Response.Write("</table>");
        }
    }
    else
    {
        //lblinform.Text = "No data found.";
    }
}

In the above code I have added the Stored procedure (Which is joined with Multiple table and Aggregate functions) with the new Dataset. You can able to create this by using Single table also.

Output:

The output will be shown like this

1.gif

Try this and let me know the feedbacks.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor