2015-06-10 57 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