We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Thursday, January 28, 2016

Created Dynamically and Displaying Data form database in Table in ASP.Net.

Step:1
Go to the SQL server and create database and table i.e database is "MkDB" and table is "tbl_Description".




Step:2
->Go Visual studio and create the asp page


Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
        <h1 align="center">Dynamic Table</h1>
    <table align="center" style="background-color:#46b3ea">
        <tr>
            <td align="center">
                <asp:PlaceHolder ID="placeholder1" runat="server"></asp:PlaceHolder>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>


</html>

Default.aspx.cs

using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con = null;
    SqlDataAdapter da = null;
    DataSet ds = new DataSet();

    StringBuilder htmlTable = new StringBuilder();
    string str = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    private void BindData()
    {

        con = new SqlConnection(@"Data Source=AFF11\SQLEXPRESS;database=MkDB;integrated Security=true;");
        str = "Select * from tbl_Description";
        da = new SqlDataAdapter(str, con);
        da.Fill(ds);
        con.Open();
        htmlTable.Append("<table border='1' align='center'>");
        htmlTable.Append("<tr style='background-color:green;'><th>Code</th><th>DescriptionA</th><th>DescriptionB</th><th>Class</th></tr>");
        if (!object.Equals(ds.Tables[0], null))
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    htmlTable.Append("<tr>");
                    htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Code"] + "</td>");
                    htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["DescriptionA"] + "</td>");
                    htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["DescriptionB"] + "</td>");
                    htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Class"] + "</td>");
                    htmlTable.Append("</tr>");

                }
                htmlTable.Append("</table>");
                placeholder1.Controls.Add(new Literal { Text = htmlTable.ToString() });
            }
            else
            {
                htmlTable.Append("<tr>");
                htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");
                htmlTable.Append("</tr>");
            }
        }
    }

}
Output:

No comments: