1

我有一个下拉列表。我已使用此代码将默认值添加到下拉列表框中

<asp:DropDownList ID="DDLCase" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDLCase_SelectedIndexChanged" AppendDataBoundItems="true" >
<asp:ListItem Value="">Select Member Report ID</asp:ListItem>
</asp:DropDownList>

例如,我有 DB 值,例如 123、1234。

当我运行我的网络表单并单击 123 和 1234 时,什么也没发生。但是,当我去单击 ListItem 值时,他们给了我一些错误

Conversion failed when converting the varchar value 'Select Member Report ID' to data type int.

这是我在页面加载时的 DDL 的后端代码。

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security = SSPI");

            SqlCommand cm = new SqlCommand("Select pr.policereportid from PoliceReport pr, AdminOfficialReport aor, MemberReport mr where pr.memberreportid=mr.memberreportid and mr.caseprogress='settled' and aor.officialreport IS NULL", con);
            con.Open();
            SqlDataReader dr;
            dr = cm.ExecuteReader();
            while (dr.Read())
            {
                DDLCase.Items.Add(dr["policereportid"].ToString());
            }
            DDLCase.Items.Insert(0, new ListItem("Select Member Report ID", "-1"));

            dr.Close();
            con.Close();
        }
    }

这是我的错误发生的地方。它发生在 dr = cm.ExecuteReader(); 周围的区域;

protected void DDLCase_SelectedIndexChanged(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security = SSPI");
        con.Open();
        SqlCommand cm = new SqlCommand("Select lro.fullname, lro.contact, mr.typeofcrime, mr.location,mr.crdatetime, pr.policeid, pr.prdatetime, pr.policereport, pr.image1, mr.citizenreport from MemberReport mr, PoliceReport pr, LoginRegisterOthers lro where pr.policereportid = '" + DDLCase.SelectedItem.Text + "' and mr.memberreportid=pr.memberreportid and lro.username=mr.username and mr.caseprogress='settled'", con);
        SqlDataReader dr;
        dr = cm.ExecuteReader();
        if (dr.Read())
        {
            lblFullName.Text = dr["fullname"].ToString();
            lblContact.Text = dr["contact"].ToString();
            lblTOC.Text = dr["typeofcrime"].ToString();
            lblLocation.Text = dr["location"].ToString();
            lblCRDT.Text = dr["crdatetime"].ToString();
            lblPicture.Text = dr["image1"].ToString();
            lblAssign.Text = dr["policeid"].ToString();
            lblPRDT.Text = dr["prdatetime"].ToString();
            lblCR.Text = dr["citizenreport"].ToString();
            lblPR.Text = dr["policereport"].ToString();
        }

        con.Close();

    }

我该如何解决这个问题?

4

2 回答 2

3

因为它期望一个 int 类型的值,并且在默认情况下没有设置值,因此它正在选择选项字段中的文本,即Select Member Report ID

将其更改为:

<asp:ListItem Value="-1">Select Member Report ID</asp:ListItem>

并在您的代码后面/javascript中适当地处理负值

使用 SelectedValue 而不是 Text!

SqlCommand cm = new SqlCommand("Select lro.fullname, lro.contact, mr.typeofcrime, mr.location,mr.crdatetime, 
pr.policeid, pr.prdatetime, pr.policereport, pr.image1, mr.citizenreport from MemberReport mr, PoliceReport pr, 
LoginRegisterOthers lro where pr.policereportid = '" + DDLCase.SelectedValue + "' and 
mr.memberreportid=pr.memberreportid and lro.username=mr.username and mr.caseprogress='settled'", con);
于 2013-05-29T07:58:39.093 回答
1

从您的标记中删除<asp:ListItem Value="">Select Member Report ID</asp:ListItem>这行代码。绑定你的 DDL 后,只需写下这一行

DDLID.items.insert(0,listboxitem("Select Member Report ID","-1"));

现在让你的代码看起来像

整数索引=1;while (dr.Read()) { if(index == 1) { DDLCase.Items.Insert(0, new ListItem("Select Member Report ID", "-1"));

} else { DDLCase.Items.Add(new ListItem(dr["dataId"].toString(), index)); } 索引++;

}

于 2013-05-29T07:58:26.650 回答