Retrieving data from database into textbox

In this blog we will know how to retrieve the related data from database into textbox's when we provide one data in the textbox and press submit button.

 

<%@ 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="REGDNO" Width="100px"></asp:Label>

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

   

    <asp:Label ID="Label2" runat="server" Text="NAME" Width="100px"></asp:Label>

    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />

   

    <asp:Label ID="Label3" runat="server" Text="BRANCH" Width="100px"></asp:Label>

    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />

   

    <asp:Label ID="Label4" runat="server" Text="ADDRESS" Width="100px"></asp:Label>

    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />

   

    <asp:Label ID="Label5" runat="server" Text="PHNO" Width="100px"></asp:Label>

    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />

   

    <asp:Label ID="Label6" runat="server" Text="STATE" Width="100px"></asp:Label>

    <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox><br />

   

    <asp:Button ID="Button1" runat="server" Text="Retrive data"

        onclick="Button1_Click" />

    <asp:Label ID="lbl1" runat="server" BackColor="Black" Font-Bold="True"

        ForeColor="Red"></asp:Label>

    </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;

    string str;

  

    protected void Button1_Click(object sender, EventArgs e)

    {

        clear();

        idcheck();

        SqlConnection con = new SqlConnection(connStr);

        con.Open();

        str = "select * from std where REGDNO='" + TextBox1.Text.Trim() + "'";

        com = new SqlCommand(str, con);

        SqlDataReader reader = com.ExecuteReader();

        if (reader.Read())

        {

            TextBox2.Text =reader["NAME"].ToString();

            TextBox3.Text = reader["BRANCH"].ToString();

            TextBox4.Text = reader["ADDRESS"].ToString();

            TextBox5.Text = reader["PHNO"].ToString();

            TextBox6.Text = reader["STATE"].ToString();

            reader.Close();

            con.Close();

        }

       

    }

    public void idcheck()

    {

        SqlConnection con = new SqlConnection(connStr);

        con.Open();

        str = "select count(*)from std where REGDNO='" + TextBox1.Text + "'";

        com = new SqlCommand(str, con);

        int count = Convert.ToInt32(com.ExecuteScalar());

        if (count > 0)

        {

            lbl1.Text = "Id Exist";

        }

        else

        {

            lbl1.Text = "Id Does not Exist";

        }

 

    }

 

    void clear()

    {

       

        TextBox2.Text = "";

        TextBox3.Text = "";

        TextBox4.Text = "";

        TextBox5.Text = "";

        TextBox6.Text = "";

    }

}