How to get the ID from DropDownList?

200 views Asked by At

I am trying to save the changes I make when I select a different value from the DropDownList. But I am getting this error

Value cannot be null. Parameter name: String [ArgumentNullException: Value cannot be null. Parameter name: String]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +10722118 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +145 System.Int32.Parse(String s) +23

I think the problem is its not finding the ID of the value in the DropDownList so it catches on this line in the update statement Trailer.UpdateTrailer(int.Parse(TrailerID),

  protected void ddlTrailerLoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlTrailerLoc=sender as DropDownList;
            if (ddlTrailerLoc != null)
            {
                ddlTrailerLoc.SelectedValue.ToString();

                Trailer.UpdateTrailer(int.Parse(TrailerID), Company.Current.CompanyID,txtTrailerReg.Text,ddlTrailerLocation.Text);
            }
        }

How do I get the ID of the value I select from the DropDownList?

Here's the code to populate the DropDownList

protected void PopulateDDLs(DropDownList ddlTrailerLoc)
    {
        DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
        if (dsTrailerLocation.Tables[0].Rows.Count > 0)
        {
            ddlTrailerLoc.DataSource = dsTrailerLocation;
            ddlTrailerLoc.DataValueField = "Description";
            ddlTrailerLoc.DataTextField = "Description";
            ddlTrailerLoc.DataBind();
        }
        else
        {
            ddlTrailerLoc.Items.Insert(0, new ListItem("No Locations Entered", "0"));
        }
    }

I know this ddlTrailerLoc.DataValueField = "Description"; should be set to TrailerID or something but it will only work when it is Description. Changing this makes the DropDownList display the wrong values

2

There are 2 answers

5
Hysteria86 On

Just guessing with the information you've provided, but should this line:

ddlTrailerLoc.SelectedValue.ToString();

actually be:

TrailerID = ddlTrailerLoc.SelectedValue.ToString();

Otherwise not sure from that code where TrailerID is set.

1
pokrak94 On
  protected void ddlTrailerLoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlTrailerLoc=sender as DropDownList;
string value = "".
            if (!String.IsNullOrEmpty(ddlTrailerLoc.SelectedValue.ToString())
            {
                value = ddlTrailerLoc.SelectedValue.ToString();

                Trailer.UpdateTrailer(int.Parse(TrailerID), Company.Current.CompanyID,txtTrailerReg.Text,ddlTrailerLocation.Text);
            }
        }

and then assign value to whenever you need to use it. Like TrailerID. I assume this is what you are trying to achieve. If so, you can assign TrailerID to ddlTrailerLoc.SelectedValue.ToString(); and then declaration for string value is unnecessary. In this case:

protected void ddlTrailerLoc_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlTrailerLoc=sender as DropDownList;
if (!String.IsNullOrEmpty(ddlTrailerLoc.SelectedValue.ToString())
{
TrailerID = ddlTrailerLoc.SelectedValue.ToString();
Trailer.UpdateTrailer(int.Parse(TrailerID), Company.Current.CompanyID,txtTrailerReg.Text,ddlTrailerLocation.Text);
}
}