Show The Data of Respective Columns of a Database in Textbox


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

 

Up Next
    Ebook Download
    View all
    Learn
    View all