DropDownList与DataValueField和DataTextField asp.net C#

时间:2014-04-29 11:15:54

标签: asp.net sql-server-2008

在我的数据库中,我从DropDownList输入CPVComID值(比如 0,1,2,3)并将dataValueFiled值显示为DropDownList之类 (-Select-,Conquest,CBC,Insight Management)。

错误消息:

Exception Details: System.ArgumentOutOfRangeException: 'ddlCardCPVComName' has a SelectedValue which is invalid because itdoes not exist in the list of items. Parameter name: value

ASPX:

<asp:DropDownList ID="ddlCardCPVComName" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlDSCPVCompanyName" DataTextField="CPVComName" 
dataValueFiled="ddlCardCPVComName" Width="205px" DataValueField="CPVComID">
<asp:ListItem Value="0"> -SELECT- </asp:ListItem>
</asp:DropDownList>

<asp:SqlDataSource ID="SqlDSCPVCompanyName" runat="server" 
ConnectionString="<%$ ConnectionStrings:OptimaWebCustomerQueryCon %>"  
SelectCommand="SELECT CPVComID, CPVComName FROM DDCPVCompanyName ORDER BY CPVComID">
</asp:SqlDataSource>

代码背后:

  private void getReceeivedCPV()
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OptimaWebCustomerQueryCon"].ConnectionString))
            {
                conn.Open();
                string str = @"SELECT CardCPVID, CardID, 
        CardCPVComName, 
        CardCPVSentDate, CardCPVStatus, CardCPVRcevDate, CPVRemarks  FROM CC_CardCPV Where CardID LIKE '" + GVCPVReceived.SelectedValue + "'";
                using (SqlCommand com = new SqlCommand(str, conn))
                {
                    using (SqlDataReader dr = com.ExecuteReader())
                    {
                        if (dr.Read())
                        {

                            TextBox1.Text = Convert.ToString(dr[2]);
                            ddlCardCPVComName.SelectedValue = Convert.ToString(dr[2]);
                            txtCardCPVSentDate.Text = Convert.ToDateTime(dr[3]).ToString("dd MMM yyyy");
                            ddlCardCPVStatus.SelectedItem.Value = Convert.ToString(dr[4]);
                            if (dr[5] != DBNull.Value)
                            {
                                txtCardCPVRcevDate.Text = Convert.ToDateTime(dr[5]).ToString("dd MMM yyyy");
                            }
                            else
                            {
                                txtCardCPVRcevDate.Text = "";
                            }
                            txtCPVRemarks.Text = Convert.ToString(dr[6]);
                        }
                    }
                }
                conn.Close();
            }
        }

我该怎么办?请建议我。

3 个答案:

答案 0 :(得分:0)

我可以解释为什么会出现这种异常。在下面的代码行中,您要为下拉列表 ddlCardCPVComName 设置一个值..

ddlCardCPVComName.SelectedValue = Convert.ToString(dr[2]);

此处的例外情况表示下拉列表 ddlCardCPVComName 中没有此类项目。因此,您必须检查何时绑定下拉列表 ddlCardCPVComName 存在您要设置的特定项目..

检查此绑定查询并确保所需的项绑定到数据源..

<asp:SqlDataSource ID="SqlDSCPVCompanyName" runat="server" 
ConnectionString="<%$ ConnectionStrings:OptimaWebCustomerQueryCon %>"  
SelectCommand="SELECT CPVComID, CPVComName FROM DDCPVCompanyName ORDER BY CPVComID">
</asp:SqlDataSource>

答案 1 :(得分:0)

如果没有错,请从您的aspx代码中删除dataValueFiled =“ddlCardCPVComName”(DropDown)并执行。

答案 2 :(得分:0)

我只是更改&#34; dlCardCPVStatus.SelectedItem.Text = Convert.ToString(dr [4]);&#34;并且成功地做到了这一点