Data Binding from dataset to HTML table in .Net

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.

Next Recommended Readings