1

I have written a function called on change of a value in a dropdown box.

Here is the function:

protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        IApplicationContext ctx = ContextRegistry.GetContext();
        IServices reg = (IServices)ctx.GetObject("Services");

        if (ddlDistrict.SelectedIndex != 0)
        {
            Int32 DistrictID = Convert.ToInt32(ddlDistrict.SelectedValue);

            ddlTaluka.DataSource = reg.getTalukaList(DistrictID));
            ddlTaluka.DataTextField = "TalukaName";
            ddlTaluka.DataValueField = "TalukaID";
            ddlTaluka.DataBind();
            ddlTaluka.Items.Clear();
            ddlTaluka.Items.Insert(0, new ListItem("-- Select Taluka --", "0"));
        }
        else
        {
            ddlTaluka.Items.Clear();
            ddlTaluka.Items.Insert(0,new ListItem("-- Select Taluka --", "0"));
        }
    }
    catch (Exception ex)
    {


    }
}

On change of a value in district dropdown taluka dropdown should be refilled...but I am getting error

Input string was not in correct format

in this line

Int32 DistrictID = Convert.ToInt32(ddlDistrict.SelectedValue);

I am not understanding how to resolve this error.

4
  • When it breaks in the debugger, what is the value of ddlDistrict.SelectedValue? Hold your mouse over it; it will tell you. Then work your way back until you figure out why it is wrong. Commented Jan 5, 2013 at 5:17
  • @JonathonReinhart hi..i m getting the selected value as 345;2;University from which 345 is the value of dropdown i selected why are the rest values also coming with it..and how should i pick up only the first value. Commented Jan 5, 2013 at 5:22
  • 3
    String.Split using ';' as the delimiter. Commented Jan 5, 2013 at 5:23
  • Check ddlDistrict.SelectedValue ........... Commented Jan 5, 2013 at 5:32

2 Answers 2

1

From looking at your comments, only "345" is in the combobox item you selected.

@JonathonReinhart hi..i m getting the selected value as 345;2;University from which 345 is the value of dropdown i selected why are the rest values also coming with it..and how should i pick up only the first value.

Try:

Int32 DistrictID = int.Parse(ddlDistrict.SelectedItem.ToString());
Sign up to request clarification or add additional context in comments.

4 Comments

hi using ur solution i can the see the value selected "Nashik" but again i get the same error..."Input string not in correct format"
@user1274646 It appears you may have the same problem if the line immediately following the one outlined above. More specifically: ddlTaluka.DataSource = reg.getTalukaList(Convert.ToInt32(ddlDistrict.SelectedValue));
hey updated the question but the control does not go to that line error is in upper line still
@user1274646 is the error still being caused by the original line outlined in your question. It seems like a very odd place to have an error as it's pretty straight forward what the code should do. However, I could see if the error were to happen with "reg.getTalukaList(....)". What is "reg.getTalukaList(...)" expecting as a parameter?
1

Thank you so much everyone with your help i finally found the solution...

    Int32 DistrictID = Convert.ToInt32(ddlDistrict.SelectedValue.Split(";".ToCharArray())[0]);

Special thanks to @JonathanReinhart

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.