2012-04-27 129 views
-1
cmd1.CommandText = "SELECT distinct MbrBtch from Member where MbrStrm='"+DrpDwnStrm .SelectedItem .Text +"'"; 
      cmd1.Connection = con; 
      DataTable Table1; 
      Table1 = new DataTable("mbr"); 
      DataRow Row1; 
      DataColumn MbrBatch = new DataColumn("MbrBatch"); 
      MbrBatch.DataType = System.Type.GetType("System.Int32"); 
      Table1.Columns.Add(MbrBatch); 
      try 
      { 
       con.Open(); 
       SqlDataReader RdrMbr = cmd1.ExecuteReader(); 
       while (RdrMbr.Read()) 
       { 

        Row1 = Table1.NewRow(); 
        Row1["MbrBatch"] = Convert.ToInt32(RdrMbr.GetInt32(0)); 
        Table1.Rows.Add(Row1); 
       } 
       RdrMbr.Close(); 
      } 

      finally 
      { 
       con.Close(); 
      } 
      DrpDwnBtch.DataSource = Table1; 
      this.DrpDwnBtch.DataTextField = "MbrBatch"; 
      DrpDwnBtch.DataBind(); 

//here MbrBtch is numeric type attribute of sql server. 
+1

哪裏?你有什麼嘗試?等 – Keith 2012-04-27 17:06:45

+4

哪一行錯誤? – 2012-04-27 17:06:57

+5

您的查詢很容易受到SQL注入的影響。使用參數化查詢。 – 2012-04-27 17:07:50

回答

1

我的猜測是下面的一行會給你一個錯誤。

更改線路

Row1["MbrBatch"] = Convert.ToInt32(RdrMbr.GetInt32(0)); 

int mbr = 0; 
if (Int32.TryParse(RdrMbr[0], out mbr)) 
    Row1["MbrBatch"] = mbr; 
0

是否有可能有在MbrBtch列空值?如果是這樣,你需要像這樣檢查空值:

if (!RdrMbr.IsDBNull(0)) 
    Row1["MbrBatch"] = Convert.ToInt32(RdrMbr.GetInt32(0)); 
else 
    // Set value to what it should be if null, perhaps a -1 or 0 
相關問題