SIGN UP MEMBER LOGIN:    
ARTICLE

Show The Data of Respective Columns of a Database in Textbox

Posted by Satyapriya Nayak Articles | ASP.NET Programming January 12, 2012
This article explains how to show the data of respective columns of a database to the corresponding textboxes present in a web page.
Reader Level:

This article explains how to show the data of respective columns of a database to the corresponding textboxes present in a web page. Here we can also navigate the records which are present inside the textboxes with the help of a button control. For this requirement we take 5 textboxes and 4 buttons and also a GridView to show the records to the user.

Table creation

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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:Label ID="Label1" runat="server" Text="ID" Width="80"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label2" runat="server" Text="Name" Width="80"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label3" runat="server" Text="Address" Width="80"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label4" runat="server" Text="Marks" Width="80"></asp:Label>
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label5" runat="server" Text="Year" Width="80"></asp:Label>
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnfirst" runat="server" Text="First" Width="60px"
            onclick="btnfirst_Click" />
        <asp:Button ID="btnprevious" runat="server" Text="Previous" Width="60px"
            onclick="btnprevious_Click" />
        <asp:Button ID="btnnext" runat="server" Text="Next"  Width="60px"
            onclick="btnnext_Click"/>
        <asp:Button ID="btnlast" runat="server" Text="Last"  Width="60px"
            onclick="btnlast_Click"/><br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>

</
body>
</
html>
 
using System;
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;
public
partial class _Default : System.Web.UI.Page
{
    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlCommand com;
    SqlDataAdapter sqlda;
    string str;
    static DataSet ds;
    static DataTable dt;
    static int r1;
    static int r2;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(connStr);
        try
        {
            if (!IsPostBack)
            {
                con.Open();
                str = "select * from student";
                com = new SqlCommand(str, con);
                sqlda = new SqlDataAdapter(com);
                ds = new DataSet();
                sqlda.Fill(ds, "student");
                dt = ds.Tables["student"];
                r1 = dt.Rows.Count;
                if (r1 > 0)
                {
                    showfirst();
                }
                r2 = 0;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        con.Close();
        bindgrid(); 
    }
    public void bindgrid()
    {
        SqlConnection con = new SqlConnection(connStr);
        con.Open();
        str = "select * from student";
        com = new SqlCommand(str, con);
        sqlda = new SqlDataAdapter(com);
        ds = new DataSet();
        sqlda.Fill(ds, "student");
        GridView1.DataSource = ds;
        GridView1.DataMember = "student";
        GridView1.DataBind();
        con.Close();
    }
    public void showfirst()
    {
        TextBox1.Text = dt.Rows[0].ItemArray[0].ToString();
        TextBox2.Text = dt.Rows[0].ItemArray[1].ToString();
        TextBox3.Text = dt.Rows[0].ItemArray[2].ToString();
        TextBox4.Text = dt.Rows[0].ItemArray[3].ToString();
        TextBox5.Text = dt.Rows[0].ItemArray[4].ToString();
    } 
    protected void btnfirst_Click(object sender, EventArgs e)
    {
        showfirst();
    }
    protected void btnprevious_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(connStr);
        try
        {
            if (r2 > 0)
            {
                r2 -= 1;
                TextBox1.Text = dt.Rows[r2].ItemArray[0].ToString();
                TextBox2.Text = dt.Rows[r2].ItemArray[1].ToString();
                TextBox3.Text = dt.Rows[r2].ItemArray[2].ToString();
                TextBox4.Text = dt.Rows[r2].ItemArray[3].ToString();
                TextBox5.Text = dt.Rows[r2].ItemArray[4].ToString();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
        }
 
    }
    protected void btnnext_Click(object sender, EventArgs e)
    {
        try
        {
            if (r2 < r1)
            {
                r2 += 1;
                TextBox1.Text = dt.Rows[r2].ItemArray[0].ToString();
                TextBox2.Text = dt.Rows[r2].ItemArray[1].ToString();
                TextBox3.Text = dt.Rows[r2].ItemArray[2].ToString();
                TextBox4.Text = dt.Rows[r2].ItemArray[3].ToString();
                TextBox5.Text = dt.Rows[r2].ItemArray[4].ToString();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
 
    }
    protected void btnlast_Click(object sender, EventArgs e)
    {
        try
        {
            TextBox1.Text = ds.Tables["student"].Rows[r1 - 1].ItemArray[0].ToString();
            TextBox2.Text = ds.Tables["student"].Rows[r1 - 1].ItemArray[1].ToString();
            TextBox3.Text = ds.Tables["student"].Rows[r1 - 1].ItemArray[2].ToString();
            TextBox4.Text = ds.Tables["student"].Rows[r1 - 1].ItemArray[3].ToString();
            TextBox5.Text = ds.Tables["student"].Rows[r1 - 1].ItemArray[4].ToString();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
}

Output

 

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks

Posted by Satyapriya Nayak Jan 13, 2012

Hi Satyapriya. Its a very useful article and I will surely try this.

Posted by Adora Krause Jan 13, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    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.
Become a Sponsor