Click here to Skip to main content
15,037,928 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Dear All,

My Problem is I was Create One page and runtime bind a DropDown. DropDown values Comes from database Properly, and it shows me Proper Format but when i Click Submit Button at That Time It Become Null and like Bellow Code
C#
protected void Page_Load(object sender, EventArgs e)
{
 if (!Page.IsPostBack)
  {
    LoadCompany();
  }
}

public void LoadCompany()
{
 try
 {
  DataSet ds = Dbcompany.GetAllCompany(ConfigurationManager.AppSettings["ConnectionString"]);
            if (ds != null)
            {
                if (ds.Tables[0] != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            ListItem li = new ListItem();
                            li.Value = ds.Tables[0].Rows[i]["CompanyId"].ToString();
                            li.Text = ds.Tables[0].Rows[i]["CompanyName"].ToString();
                            ddl_company.Items.Add(li);
                            
                        }
                    }
                }
            }
 }
}

protected void btn_add_Click(object sender, EventArgs e)
{
 //When buttin_Click at that time ddl_company DropDown Become Null so How Can Solwed this.. 
}


If I Remove Page.IsPostBack Condition From Page_Load then all Time DropDown was Fill So When Selected Company all Time 1st..


Pleas Help Me, it's My serious Problem...
Posted
Updated 18-Jun-12 1:12am
v5

Try This :

C#
       if (!IsPostBack)
       {
           FillDropDownList();
       }


     private void FillDropDownList()
    {
        DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("Select fileds  FROM tablename", connection object);
        myda.Fill(ds);
        drop_category.DataSource = ds;
        drop_category.DataValueField = "fileds";
        drop_category.DataBind();
        drop_category.Items.Insert(0, new ListItem("Select", "0"));
    }

Hope this will help you. if not please post it.
   
Comments
jasmin_patel 16-Jun-12 8:02am
   
Dear Sir,

Here When I was Select 3rd OR Any Other Option From DropDown And Click Submit Button At That time FillDropDownList Could Not be Call So now DropDown Become Null..

so when submit my page at that time my dropdown value can be null or not Proper Value ...
[no name] 16-Jun-12 8:23am
   
can you give some code which is inside the button click event.
jasmin_patel 16-Jun-12 8:41am
   
protected void btn_add_Click(object sender, EventArgs e)
{
string compnay=ddl_comnapny.SelectedValue;
}
Is the EnableViewState property of the DropDownList set to true?
   
Comments
jasmin_patel 16-Jun-12 8:36am
   
yes, it was True.
Pankaj Nikam 16-Jun-12 8:41am
   
Please post the code like what you handle in the ButtonClick event...
jasmin_patel 18-Jun-12 5:49am
   
protected void btn_add_Click(object sender, EventArgs e)
{
string compnay=ddl_comnapny.SelectedValue;
}
when you call loadcompany set then dropdownlist selected index=0

C#
if (!Page.IsPostBack)
  {
    LoadCompany();
ddl_company.SelectedIndex = 0;
  }

i think after this it will be solved.
   
C#
DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("Select columnvalue,columntext   FROM tablename", connection object);
        myda.Fill(ds);
        drop_category.DataSource = ds;
        drop_category.DataTextField = "columntext";
        drop_category.DataValueField = "columnvalue";
        drop_category.DataBind();
   

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