Click here to Skip to main content
15,916,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I bind to dropdownlist, and am trying to use the datavalue as the parameter in my following query to bind to gridview. any help would be appreciated. Code below.

What I have tried:

C#
public partial class ConsolidatedReport : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {
           BindCustomersDDL();
       }

       private void BindCustomersDDL()
       {
           DataTable dt = new DataTable();
           using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AMTConnectionString"].ConnectionString))
           {
               con.Open();
               SqlCommand cmd = new SqlCommand("Select * from dbo.customer order by 2", con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(dt);
           }
           ddlCustomer.DataSource = dt;
           ddlCustomer.DataTextField = "cname";//Which you want to display inthe dropdownlist
           ddlCustomer.DataValueField = "customerid";
           ddlCustomer.DataBind();


       }

       protected void btnExecute_Click(object sender, EventArgs e)
       {
           BindBacteriaGrid();

       }

       private void BindBacteriaGrid()
       {
           DataTable dt = new DataTable();
           try
           {
               SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AMTConnectionString"].ConnectionString);
               SqlDataAdapter adp = new SqlDataAdapter("SELECT [CustomerID], WellID, SRB_RISK, APB_RISK  from [AMT].[dbo].bacteria where customerid = @cust order by 1", con);
               adp.SelectCommand.Parameters.AddWithValue("@cust", ddlCustomer.Text);
               adp.Fill(dt);

               if (dt.Rows.Count > 0)
               {
                   BacteriaGridview.DataSource = dt;
                   BacteriaGridview.DataBind();
               }
               else
               {
                   BacteriaGridview.DataSource = null;
                   BacteriaGridview.DataBind();
               }
           }
           catch (Exception ex)
           {
               Response.Write("Error Occured: " + ex.ToString());
           }
           finally
           {
               dt.Clear();
               dt.Dispose();
           }
       }
Posted
Updated 6-May-16 18:04pm
v2

1 solution

Page.IsPostback is missing
C#
protected void Page_Load(object sender, EventArgs e)
      {
          if (Page.IsPostBack) return;
          BindCustomersDDL();

      }

for reading the dropdown selected item, use
C#
var text = ddlCustomer.SelectedItem.Text;
var value = ddlCustomer.SelectedItem.Value;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900