You must Sign In to post a response.
  • Category: ASP.NET

    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.
  • #767919
    Hai Gopi,
    Session is not good in the case where you need to send some small value to the next page. The best way is to use QueryString where you can pass the value which is selected from the Dropdown list. You can modify the code as below:
    FirstPage:

    protected void ddlSearch_Click(object sender, EventArgs e)
    {
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString());
    try
    {
    conn.Open();
    if (ddlBloodGroup.SelectedIndex != 0)
    {
    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?selectedValue='"+ ddlBloodGroup.SelectedItem.Text +"'");
    }
    }
    }

    Second Page:

    protected void Page_Load(object sender, EventArgs e)
    {
    if (Request.QueryString["selectedValue"] != null)
    {
    selectedValue = Request.QueryString["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();
    }
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #767928
    Hi,

    Yes, I agree with pawan as i seen your code you are passing single variable using session, that not yet all correct, if you want to send large data then only we can go for session, but in your case you want to send one dropdown value so query string is more than enough for you.

    use below piece of code while redirecting time


    Response.Redirect("~/DisplayResult.aspx?SelectedValue='"+ ddlBloodGroup.SelectedItem.Text+"'");


    and in second page get that selected value as like below


    string SelectedValue=string.Empty;
    if(Request.QueryString["SelectedValue"]!=null)
    SelectedValue=Request.QueryString["SelectedValue"].ToString();

    //pass that SelectedValue while fetching data from database and bind the same to your gridview control


    Hope this helps you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments