Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
problem

Drop down list Selected value is select always select the first value
My CODE


C#
public void ddlbind()
       {
           BALBillDetails B = new BALBillDetails();
           DataSet ds = new DataSet();
           B.GetItemNames();
           ddlitems.DataSource = B.GetItemNames();
           ddlitems.DataTextField = "Item_Name";
           ddlitems.DataValueField = "Item_Code";
           ddlitems.DataBind();

       }

protected void ddlitems_SelectedIndexChanged(object sender, EventArgs e)
        {

            BALBillDetails objPrice = new BALBillDetails();
            DataTable dt = new DataTable();
            objPrice.Item_Code = ddlitems.SelectedValue.ToString();
            dt = objPrice.GetItemPrice();
           
            txtPrice.Text = dt.Rows[0]["Sale_Price"].ToString();
     
        }
Posted
Updated 11-Dec-13 0:03am
v2
Comments
Vishal Pand3y 11-Dec-13 6:04am    
means what? can you Explain your requirement
TrushnaK 11-Dec-13 6:10am    
what you want exactly?
Rahul_Pandit 11-Dec-13 6:14am    
there is confusion in ur question?
Kriti Jain 11-Dec-13 6:17am    
You can use ddlitems.SelectedItem.Text or ddlitems.SelectedItem.Value to get the selected value from the dropdown list. Try Once..!!
Harshil_Raval 11-Dec-13 6:32am    
Check that you have call ddlbind() method in page load in
if(!isPostback)
{ddlbind();}

Try this code, I think when you are select drop down list your page is redirect, when page is redirect again fill the values in drop down list.
I hope this will be help you.

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlbind();
        }

    }
    public void ddlbind()
    {
        BALBillDetails B = new BALBillDetails();
        DataSet ds = B.GetItemNames();
        ddlitems.DataSource = ds.Tables[0];
        ddlitems.DataTextField = "Item_Name";
        ddlitems.DataValueField = "Item_Code";
        ddlitems.DataBind();

    }
 
Share this answer
 
Hi Your Error is on ddlitems.DataSource = B.GetItemNames();
check what return it.or
you can Try it too:
1.Click on top right of dropdown -Choose Data source.
2.Select New Data Source.
3.Select Object.
4.Select your BLL class.
5.Configure setting.
Yours Farhad.
 
Share this answer
 
The Dropdown sholud be binded in the following manner if you are connecting to the database using ADO.NET.

C#
public void ddlbind()
       {
           BALBillDetails B = new BALBillDetails();
           DataSet ds = B.GetItemNames();
           ddlitems.DataSource = ds.Tables[0];
           ddlitems.DataTextField = "Item_Name";
           ddlitems.DataValueField = "Item_Code";
           ddlitems.DataBind();
 
       }
 
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