Hi ,
Check this Example
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = GetData();
DropDownList1.DataValueField = "orderID";
DropDownList1.DataTextField = "OrderAmount";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
{
using (SqlCommand Cmd = new SqlCommand("select * from ODetails where orderID= @orderid", Cn))
{
Cn.Open();
Cmd.Parameters.AddWithValue("@orderid", int.Parse(DropDownList1.SelectedValue));
SqlDataReader Dr = Cmd.ExecuteReader();
if (Dr.HasRows)
{
GridView1.DataSource = Dr;
GridView1.DataBind();
}
Dr.Close();
Cn.Close();
}
}
}
DataTable GetData()
{
DataTable dt = new DataTable();
using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
{
using (SqlCommand Cmd = new SqlCommand("SELECT * FROM Orders ", Cn))
{
Cn.Open();
SqlDataAdapter adpt = new SqlDataAdapter(Cmd);
adpt.Fill(dt);
}
}
return dt;
}
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
Best Regards
M.Mitwalli