问题描述
执行当前Web请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息:System.ArgumentOutOfRangeException:“ddlcity”有一个无效SelectedValue,因为它不在项目列表中。参数名:value源错误:执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。堆栈跟踪:[ArgumentOutOfRangeException:“ddlcity”有一个无效SelectedValue,因为它不在项目列表中。参数名:value]System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerabledataSource)+977System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgse)+97System.Web.UI.WebControls.ListControl.PerformSelect()+21System.Web.UI.WebControls.BaseDataBoundControl.DataBind()+71System.Web.UI.Control.DataBindChildren()+207System.Web.UI.Control.DataBind(BooleanraiseOnDataBinding)+149System.Web.UI.Control.DataBind()+15System.Web.UI.Control.DataBindChildren()+207System.Web.UI.Control.DataBind(BooleanraiseOnDataBinding)+149System.Web.UI.Control.DataBind()+15System.Web.UI.WebControls.GridView.CreateRow(Int32rowIndex,Int32dataSourceIndex,DataControlRowTyperowType,DataControlRowStaterowState,BooleandataBind,ObjectdataItem,DataControlField[]fields,TableRowCollectionrows,PagedDataSourcepagedDataSource)+156System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerabledataSource,BooleandataBinding)+2444System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerabledata)+58System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerabledata)+15System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerabledata)+104System.Web.UI.DataSourceView.Select(DataSourceSelectArgumentsarguments,DataSourceViewSelectCallbackcallback)+27System.Web.UI.WebControls.DataBoundControl.PerformSelect()+133System.Web.UI.WebControls.BaseDataBoundControl.DataBind()+71System.Web.UI.WebControls.GridView.DataBind()+5System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()+61System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgse)+22System.Web.UI.WebControls.GridView.OnPreRender(EventArgse)+18System.Web.UI.Control.PreRenderRecursiveInternal()+91System.Web.UI.Control.PreRenderRecursiveInternal()+180System.Web.UI.Control.PreRenderRecursiveInternal()+180System.Web.UI.Page.ProcessRequestMain(BooleanincludeStagesBeforeAsyncPoint,BooleanincludeStagesAfterAsyncPoint)+3873代码如下.那位帮忙看看<%@PageLanguage="C#"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headid="Head1"runat="server"><title>Show</title></head><body><formid="form1"runat="server"><div><asp:GridViewID="grdMovies"DataSourceID="SqlDataSource1"DataKeyNames="ID"AutoGenerateColumns="False"AutoGenerateEditButton="True"Runat="server"><Columns><asp:TemplateFieldHeaderText="UserName"><ItemTemplate><%#Eval("UserName")%></ItemTemplate><EditItemTemplate><asp:TextBoxID="txtUserName"Text='<%#Bind("UserName")%>'runat="server"></asp:TextBox><asp:RequiredFieldValidatorid="valUserName"ControlToValidate="txtUserName"Text="(required)"runat="server"></asp:RequiredFieldValidator></EditItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="city"><ItemTemplate><%#Eval("city")%></ItemTemplate><EditItemTemplate><asp:DropDownListid="ddlcity"DataSourceID="SqlDataSource2"DataTextField="city"DataValueField="ID"SelectedValue='<%#Bind("cityID")%>'runat="server"></asp:DropDownList></EditItemTemplate></asp:TemplateField></Columns></asp:GridView><asp:SqlDataSourceid="SqlDataSource1"ConnectionString="server=;uid=sa;password=;database="SelectCommand="SELECTtb_User.ID,tb_User.UserName,tb_User.cityID,tb_User.cityFROMtb_UserJOINCitysONCitys.ID=tb_User.cityID"UpdateCommand="UPDATEtb_UserSETUserName=@UserName,cityID=@cityidWHERE(ID=@ID)"runat="server"ProviderName="System.Data.SqlClient"><UpdateParameters><asp:ParameterName="UserName"/><asp:ParameterName="cityid"/><asp:ParameterName="ID"/></UpdateParameters></asp:SqlDataSource><asp:SqlDataSourceid="SqlDataSource2"ConnectionString="server=;uid=sa;password=;database="SelectCommand="SelectID,Citys.cityfromCitys"runat="server"></asp:SqlDataSource></div></form></body></html>
解决方案
解决方案二:
猜测一下是不是你绑定的时候有的CITYID值是重复的?
解决方案三:
不是
解决方案四:
你绑定的值是NUll,或者是string.Empty
解决方案五:
查看下数据库
解决方案六:
ConnectionString="server=;uid=sa;password=;database="这里能对吗!
解决方案七:
<asp:DropDownListid="ddlcity"DataSourceID="SqlDataSource2"DataTextField="city"DataValueField="ID"SelectedValue='<%#Bind("cityID")%>'runat="server"></asp:DropDownList>SelectID,Citys.cityfromCitys这里根本对不上,查询的字段没有cityID。
解决方案八:
正解...忘记了...现在还有个问题就是在我更新数据的时候,点击更新后CITYID自动变成NULL,从而使这列数据消失,这是什么原因?
解决方案九:
引用6楼net_xiaojian的回复:
<asp:DropDownListid="ddlcity"DataSourceID="SqlDataSource2"DataTextField="city"DataValueField="ID"SelectedValue='<%#Bind("cityID")%>'runat="server"></asp:DropDownList>SelectID,Citys.cityfromCitys这里根本对不上,查询的字段没有cityID。
好像也没错..这个CITYID用的是SQLDATASOURCE1里的