Pass the Selected Value to next Page and display the records in GridView
I have passed the selected value from the dropdownlist to second page using session variable. Using this variable I can able to display the records in Gridview in second page.Below is my code.
FirstPage:
protected void ddlSearch_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString());
try
{
conn.Open();
if (ddlBloodGroup.SelectedIndex != 0)
{
Session["selectedValue"] = ddlBloodGroup.SelectedItem.Text;
adp = new SqlDataAdapter("Select * from userRegister where bloodgroup ='" + ddlBloodGroup.SelectedItem.Text + "' " , conn);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Response.Redirect("~/DisplayResult.aspx");
// Server.Transfer("~/DisplayResult.aspx");
}
}
Second Page:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["selectedValue"] != null)
{
selectedValue = Session["selectedValue"].ToString();
}
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString());
try
{
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter();
adp = new SqlDataAdapter("Select * from userRegister where bloodgroup ='" + selectedValue + "' ", conn);
DataSet ds = new DataSet();
adp.Fill(ds);
GridViewResult.DataSource=ds;
GridViewResult.DataBind();
}
Whether the above is correct method... or is there any other professional way available. Please guide me.