2013-05-29 40 views
1

我有一個下拉列表。我添加了一個默認值到一個下拉列表框,這個代碼在下拉列表中使用confirmbuttonextender時重複值

<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什麼都沒有發生。然而,當我去點擊列表項值,他們給了我一些錯誤

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(); 

    } 

我該如何解決這個問題?

+3

請您的具體問題http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title –

+0

我@SonerGönül同意更新您的標題。這與標題無關。 – jAC

+0

只是設置'價值=「 - 1」',它會工作 – Rahul

回答

3

COS其預計在默認的情況下,int類型的和值沒有設定值,因此它在選項字段即Select Member Report ID

將其更改爲撿文本:

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

和處理負值適當後面的代碼/ JavaScript的

使用的SelectedValue和非T轉!

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); 
+0

添加負值後,我應該如何在後端代碼中執行此操作? –

+0

umm .. asp.net web窗體不是我的強項,但這裏是一個嘗試..如果(DDLCase.value == - 1){ErrorBox.Show()}即基本檢查下拉列表中設置的x值,如果它是負值在窗體上顯示一個錯誤框..我可能沒有得到正確的語法,但多數民衆贊成在想 – Baz1nga

+0

我認爲你的方法是非常類似於Kamran Ajmal的方法。 –

1

從您的標記中刪除<asp:ListItem Value="">Select Member Report ID</asp:ListItem>這行代碼。 後結合您的DDL只寫這行

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

現在使你的代碼看起來像

INT指數= 1; (索引== 1) DDLCase.Items.Insert(0,new ListItem(「Select Member Report ID」,「-1」));

} 別的 { DDLCase.Items.Add(新的ListItem(DR [ 「DATAID」]的toString(),索引)。); } index ++;

}

+0

對不起。我想我犯了一個錯誤。我沒有在我的DDL上使用任何數據綁定。 –

+0

比第一次當循環輸入你的代碼時比把我發佈在我的答案中的那行代碼比每次循環輸入只添加你的數據庫項目到ddl。 –

+0

int index = 1; (索引== 1) DDLCase.Items。(dr.Read()) {插入(0,新的ListItem(「選擇成員報告ID」,「-1」)); index ++; } else { DDLCase.Items.Add(dr [「policereportid」]。ToString()); } } –

相關問題