我有一个下拉列表。我已使用此代码
将默认值添加到下拉列表框中<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。
当我运行我的webform并点击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();
}
我如何解决这个问题?
答案 0 :(得分: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);
答案 1 :(得分:1)
从标记中删除<asp:ListItem Value="">Select Member Report ID</asp:ListItem>
这行代码。
绑定你的DDL后,只需写下这一行
DDLID.items.insert(0,listboxitem("Select Member Report ID","-1"));
现在让您的代码看起来像
int index = 1; while(dr.Read()) { if(index == 1) { DDLCase.Items.Insert(0,new ListItem(“Select Member Report ID”,“ - 1”));
} 其他 { DDLCase.Items.Add(new ListItem(dr [“dataId”]。toString(),index)); } 索引++;
}