2015-06-10 59 views
-1

我想保存從DropDownList中選擇不同值時所做的更改。但我得到這個錯誤如何從DropDownList獲取ID?

值不能爲空。參數名稱:String [ArgumentNullException: 值不能爲空。參數名:字符串]
System.Number.StringToNumber(字符串str的NumberStyles選項, NumberBuffer &數的NumberFormatInfo信息,布爾parseDecimal) 10722118 System.Number.ParseInt32(字符串s的NumberStyles風格的NumberFormatInfo信息)+145 System.Int32.Parse(String s)將+23

我認爲問題是它沒有找到值的ID在DropDownList所以抓住這條線的更新語句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); 
      } 
     } 

我該如何? et我從DropDownList中選擇的值的ID?

下面的代碼來填充的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")); 
     } 
    } 

我知道這ddlTrailerLoc.DataValueField = "Description";應設置爲TrailerID或東西但是當它被描述它只會工作。改變這使得DropDownList中顯示錯誤的值

+4

從您的示例中不清楚TrailerID變量的初始化位置。 您可能需要在事件處理程序中顯式初始化它,如下所示:TrailerID =((Trailer)ddlTrailerLoc.SelectedValue).TrailerID,其中Trailer是DropDownList項目的類 –

+1

將DropDownList Value和Text Field設置爲遵循ddlTrailerLoc。 DataValueField =「Trailer_ID」; //你想要的ID字段 ddlTrailerLoc.DataTextField =「Trailer_Name」; –

回答

0

與您所提供的信息只是猜測,但應該這行:

ddlTrailerLoc.SelectedValue.ToString(); 

實際上是:

TrailerID = ddlTrailerLoc.SelectedValue.ToString(); 

否則不會從確定代碼TrailerID設置。

+0

是的,我已經嘗試過,但它會導致錯誤'輸入字符串不是正確的格式。因爲它設置拖車位置是一個字符串的id是int。 TrailerID從DropDownList變成「測試」,但它應該是一個數字 – user2026041

+0

那麼爲什麼它應該是一個數字「測試」?你如何填充DropDownList? –

+0

我已將代碼添加到我的問題 – user2026041

0
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); 
      } 
     } 

然後在您需要使用它時分配值。像TrailerID一樣。我認爲這是你想要達到的目標。如果是這樣,您可以將TrailerID分配給ddlTrailerLoc.SelectedValue.ToString();然後聲明字符串值是不必要的。在這種情況下:

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); 
} 
} 
+0

設置'TrailerID = ddlTrailerLoc.SelectedValue.ToString();'導致錯誤輸入字符串格式不正確。因爲它將拖放位置是一個字符串設置爲int的id。 TrailerID從DropDownList變成「測試」,但它應該是一個數字 – user2026041