user1274646
user1274646

Reputation: 921

C#.net Input string was not in correct format

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.

Upvotes: 1

Views: 1530

Answers (2)

user1274646
user1274646

Reputation: 921

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

Upvotes: 1

Inisheer
Inisheer

Reputation: 20794

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());

Upvotes: 1

Related Questions