.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Display Data Using Table Control

Posted By:satyapriyanayak       Posted Date: March 11, 2013    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Display Data Using Table Control
 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Display_Data_Using_Table_Control._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1" runat="server"
            Text="Display Records Through table control" onclick="Button1_Click" />
    <asp:Table ID="Table1" runat="server" BackColor="#FFFF80" BorderColor="#FF8080"
        BorderStyle="Solid" BorderWidth="5px" ForeColor="Red" GridLines="Both">
    </asp:Table><br />
    </div>
    </form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Display_Data_Using_Table_Control
{
    public partial class _Default : System.Web.UI.Page
    {
        string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string str;
        SqlCommand com;
        SqlDataAdapter sqlda;
        DataSet ds;
        DataTable dt;
        int i;
        int j;

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(strConnString);
            try
            {
                con.Open();
                str = "select * from student ";
                com = new SqlCommand(str, con);
                ds = new DataSet();
                sqlda = new SqlDataAdapter(com);
                sqlda.Fill(ds, "student");
                dt = ds.Tables["student"];

                for (i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    TableRow tr = new TableRow();

                    for (j = 0; j <= dt.Columns.Count - 1; j++)
                    {
                        TableCell tc = new TableCell();
                        tc.Text = dt.Rows[i].ItemArray[j].ToString();
                        tr.Cells.Add(tc);
                        Table1.Rows.Add(tr);
                    }
                }
                con.Close();

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }
    }
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend