Click here to Skip to main content
15,888,158 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a databound DropDownList and I need a code that will only show the DropDownList if there is a value in the Database, if the there is no value/empty DropDownlist it should not be visible.

What I have tried:

if (ddlFruits.Items.Count == 1)
{
ddlFruits.Visible = false;
}
else
{
ddlFruits.Visible = true;
}
Posted
Updated 21-Oct-16 9:23am
Comments
Suvendu Shekhar Giri 21-Oct-16 10:56am    
so what is the issue?
Karthik_Mahalingam 27-Oct-16 1:57am    
if (ddlFruits.Items.Count >0 )
{
ddlFruits.Visible = true;
}
else
{
ddlFruits.Visible = false;
}

So, basically you need to handle the Visible property. When you get the value from database, just check for null and empty.

If not null and not empty, then Visible = true, else, Visible = false.
 
Share this answer
 
Comments
Member 12802669 21-Oct-16 11:13am    
If i its visible = true it Shows the dropdownlist even if there isn't a value and if its visible = false it always hides the dropdownlist even if there is a value in the database....
That's why I told you to check for the value first. Your decision should be taken on the value and nothing else.
Instead of checking the Items from the DropDownList, check for the DataSource that is used by your DropDownList. For example, if you are using a DataTable as your DataSource then you could do something like this at Page_Load event:

C#
private string GetConnectionString(){
       //calling the connection string that was set up from the web config file
        return ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
}

private void BindDropDownList(){
           DataTable dt = new DataTable();
           using (SqlConnection sqlConn = new SqlConnection(GetConnectionString())){
                string sql = "SELECT Field1, Field2 FROM YourTable WHERE Field3 = @Param1";
                using(SqlCommand sqlCmd = new SqlCommand(sql,sqlConn)){
                    sqlCmd.Parameters.AddWithValue("@Param1", "YourFilterValueHere");
                    sqlConn.Open();
                    using(SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCmd))
                    {
                        sqlAdapter.Fill(dt);
                    }
                }
            }

            if (dt.Rows.Count > 0)
            {
                DropDownList1.DataSource =dt;
                DropDownList1.DataTextField = "Field2"; // the items to be displayed in the list items
                DropDownList1.DataValueField = "Field1"; // the id of the items displayed
                DropDownList1.DataBind();
            }
            else
            {
                  DropDownList1.Visible = false;
            }
       
}


protected void Page_Load(object sender, EventArgs e){
        if (!IsPostBack)
            BindDropDownList();
}
 
Share this answer
 
v2

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