How to insert record in textbox when select value in dropdown list?
Table name(panku)
Id (primary key,int)
Name(nvarchar(max))
Address(nvarchar(max))
Mobile_no(nvarchar(max))
Default2.aspx
<table class="style2"
style="background-color:
#CCFFFF">
<tr>
<td class="style3">
<p>
Id</p>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataTextField="id" DataValueField="id"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" Height="19px"
style="font-style:
italic" Width="155px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style3">
<p>
Name</p>
</td>
<td>
<asp:TextBox ID="t1" runat="server" BackColor="Black" ForeColor="#FF6600"
style="font-style:
italic"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
<p>
address</p>
</td>
<td>
<asp:TextBox ID="t2" runat="server" BackColor="Black" ForeColor="#FF6600"
style="font-style:
italic"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
<p>
Mobile_no</p>
</td>
<td>
<asp:TextBox ID="t3" runat="server" BackColor="Black" ForeColor="#FF6600"
style="font-style:
italic"></asp:TextBox>
</td>
</tr>
</table>
</asp:Content>
Default2.aspx
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.Configuration;
using
System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection
con;
SqlCommand
cmd;
SqlDataReader
dr;
string id= string.Empty;
string s = ConfigurationManager.ConnectionStrings["conn"].ToString();
protected void Page_Load(object
sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
FillDropDownList();
}
}
private void FillDropDownList()
{
SqlConnection
Connection = new SqlConnection(s);
SqlCommand
cmd = new SqlCommand("select [id] from panku", Connection);
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"));
}
private void FillDropDownList1()
{
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs
e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs
e)
{
id = DropDownList1.SelectedItem.Value;
SqlConnection
Connection = new SqlConnection(s);
string
sqlStatement = string.Empty;
SqlDataReader
dr;
sqlStatement = "select
* from panku where [id]='"+id+"'";
Connection.Open();
SqlCommand
cmd = new SqlCommand(sqlStatement,
Connection);
cmd.Connection = Connection;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
int
intRecordCount = 0;
while
(dr.Read())
{
intRecordCount = intRecordCount +
1;
t1.Text =
dr.GetString(intRecordCount);
intRecordCount = intRecordCount +
1;
t2.Text =
dr.GetString(intRecordCount);
intRecordCount = intRecordCount +
1;
t3.Text =
dr.GetString(intRecordCount);
}
dr.NextResult();
}
}
========================================================================================

No comments:
Post a Comment