Thursday, 14 June 2012

How To Fetch Record in textbox When Select particular ID in Drop downlist?


Here we will learn how to How To Fetch Record in textbox When Select particular ID in Drop- downlist in asp.net? We can easily develop this application . first we will make a dropdownlist and write code dropdownclick event and use dropdownlist method write code Please try and learn.

Create Database name- Dep
Table name-panku
Field-Id---int(primary Key)
         Name(varchar(50))
         Class(varchar(50)
Here design Default.aspx asp.net page ....



<table class="style1" style="background-color: #454545; color: #FFFFFF">
    <tr>
        <td class="style3" colspan="2">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; How To Fetch
            Record in textbox When Select particular ID in Drop downlist</td>
    </tr>
    <tr>
        <td class="style3">
            <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Select Id</strong></td>
        <td>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="157px">
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td class="style3">
            <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Name</strong></td>
        <td>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style2">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Class</td>
        <td>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td class="style2">
            &nbsp;</td>
        <td>
            &nbsp;</td>
    </tr>
</table>


Here we will keep code in Default.aspx.cs  asp.net page ....

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           
            FillDropDownList1();

          
        }
    }
    private void FillDropDownList1()
    {
       
        conn= new SqlConnection(@"Data Source=PANKAJ-PC;Initial Catalog=dep;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("select [ID] from panku", conn);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        DropDownList1.DataSource = ds;

        DropDownList1.DataTextField = "ID";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("---Select---", "0"));
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ID = DropDownList1.SelectedItem.Value;
       conn = new SqlConnection(@"Data Source=PANKAJ-PC;Initial Catalog=dep;Integrated Security=True");
        string sqlStatement = string.Empty;
        SqlDataReader dr;
        sqlStatement = "select * from panku where [ID]='" + ID + "'";
        conn.Open();
        SqlCommand cmd = new SqlCommand(sqlStatement, conn);
        cmd.Connection = conn;
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        int intRecordCount = 0;
        while (dr.Read())
        {
            intRecordCount = intRecordCount + 1;
            TextBox1.Text = dr.GetString(intRecordCount);
            intRecordCount = intRecordCount + 1;
            TextBox2.Text = dr.GetString(intRecordCount);
           

        }
        dr.NextResult();
    }
}
 Now Please Try this ...

No comments:

Post a Comment

What is ASP.NET? Components of ASP.NET

ASP.Net Definition -It is used for creating web-based applications.ASP.Net is a web development platform provided by Microsoft. ASP.NET i...