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

Top 5 Contributors of the Month
david stephan
Gaurav Pal

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

 Subscribe to Code Snippets

Retrieving data from database into textbox

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

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

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>
    <form id="form1" runat="server">
    <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"

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)
        SqlConnection con = new SqlConnection(connStr);
        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();
    public void idcheck()
        SqlConnection con = new SqlConnection(connStr);
        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";
            lbl1.Text = "Id Does not Exist";


    void clear()
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";


Further Readings:


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