我有一个下拉列表。我已使用此代码将默认值添加到下拉列表框中
<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();
}
我该如何解决这个问题?