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">
How To Fetch
Record in textbox When Select
particular ID in Drop downlist</td>
</tr>
<tr>
<td class="style3">
<strong>
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>
Name</strong></td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Class</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
</td>
</tr>
</table>
Here we will keep code in Default.aspx.cs asp.net page ....
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 ...
Now Please Try this ...


No comments:
Post a Comment