2017-02-13 32 views
0

请帮忙。这是我为公司的客户服务团队工作了大约6周的用户界面,他们很想使用它。选择在UI中检查的列时,无法在数据库表中找到'id'值(复选框)

与标题一样,我试图选择与包含复选框的行关联的“ID”值。如果该复选框是'checked',我想要一个select语句运行并从该行获取'ID'值。

protected void UpdateSelectedRecords() 
{ 
    string strPerSignStart = "%"; 
    string strPerSignEnd = "%"; 
    string strDbSearch = strPerSignStart + ddlDatabasesUpdateTCID.Text +  strPerSignEnd; 
    object NewTCID = txtTargetCID.Text; 

    DateTime curtstmp = DateTime.Now; 

    foreach (GridViewRow row in GridView1.Rows) 
    { 
    if (row.RowType == DataControlRowType.DataRow) 
    { 
     chkBox = (row.Cells[0].Controls[0] as CheckBox); 

     if (chkBox != null && Convert.ToBoolean(chkBox.Checked) == true) 
     { 
     OdbcConnection Postgreconnect2 = new OdbcConnection("Driver= {PostgreSQL Unicode};Server=localhost;Port=5433;Database=postgres;Uid=xxx;Pwd=xxx"); 

     Postgreconnect2.Open(); 

     OdbcCommand cmd = new OdbcCommand("SELECT id FROM conceptidmapping WHERE database ILIKE " + "?" + " AND " + (chkBox.Checked == true) + " ORDER BY id ASC", Postgreconnect2); 
     cmd.Parameters.AddWithValue("@database", strDbSearch); 
     cmd.Parameters["@database"].Value = Convert.ToString(strDbSearch); 
     cmd.Connection = Postgreconnect2; 
     string idVal = (string)cmd.ExecuteScalar(); 


     OdbcCommand cmd1 = new OdbcCommand("UPDATE conceptidmapping SET selected = true WHERE database ILIKE " + "?" + " AND id = " + idVal, Postgreconnect2); 
     cmd1.Parameters.AddWithValue("@database", strDbSearch); 
     cmd1.Parameters["@database"].Value = Convert.ToString(strDbSearch); 
     cmd1.Connection = Postgreconnect2; 
     cmd1.ExecuteNonQuery(); 
     } 
     } 
    } 
    } 
+0

我的道歉起初对这个没有提供很多信息。发生的事情是程序进入'For'循环时,它将逐行浏览Gridview(从上到下)。当它看到复选框被选中时,它会输入第二个IF语句(使用ODBC命令)。第一个ODBC命令试图找到该行的“ID”值。一旦它具有'ID'值,它就会在第二个ODBC命令中使用它来将'selected'列从'false'更新为'true'。 – M72

+0

发生了什么事是它从正确的数据库中获取'ID'值,但它从来没有选择复选框。有没有人看到我在这种方法中有任何简单的缺陷?我在'代码隐藏'文件中也是这样做的(C#),因为该过程的第一步是从下拉列表中选择“数据库”值。我可能是错的,但认为在标记中填充的命令中使用这种方法很难。任何建议都非常欢迎! – M72

回答

0

GridViews非常擅长显示和管理数据。您声明您拥有“与”“行关联的”ID“值。但你不知道,你实际上做了一个数据库调用以后检索ID

对我来说这个尖叫“DataKey”。就我个人而言,我会找到一种方法来使您需要的ID的一部分填充GridView的数据集,然后将该ID添加为GridView DataKey。这是您将ID与GridViewRow关联的方式。这可以让你消除选择数据库调用UpdateSelectedRecords()

但是,有时候你没有这个选项,所以这里是你的代码,用这两种解决方案修改和评论一些事情,供您考虑。

protected void UpdateSelectedRecords() 
{ 
    //////////////////////////////////////////// 
    // Since these are all local to the function 
    // 
    string strPerSignStart = "%"; 
    string strPerSignEnd = "%"; 
    string strDbSearch = strPerSignStart + ddlDatabasesUpdateTCID.Text + strPerSignEnd; 
    // 
    ////////////////////////////////////////////// 
    // Consider : 
    string strDbSearch = String.Format("%{0}%", ddlDatabasesUpdateTCID.Text); 


    ////////////////////////////////////////////// 
    // FYI These are never used 
    // 
    object NewTCID = txtTargetCID.Text; 
    DateTime curtstmp = DateTime.Now; 

    ////////////////////////////////////////////// 
    // Instantiate and open a connection once 
    // also consider using the using() statement 
    // it make managing connections much easier 
    // 
    using(OdbcConnection Postgreconnect2 = new OdbcConnection("Driver= {PostgreSQL Unicode};Server=localhost;Port=5433;Database=postgres;Uid=xxx;Pwd=xxx")); 
    { 
    Postgreconnect2.Open(); 

    // Instantiate commands once 
    OdbcCommand cmd = new OdbcCommand("", Postgreconnect2); 
    OdbcCommand cmd1 = new OdbcCommand("", Postgreconnect2); 

    foreach (GridViewRow row in GridView1.Rows) 
    { 
     if (row.RowType == DataControlRowType.DataRow) 
     { 
     chkBox = (row.Cells[0].Controls[0] as CheckBox); 

     ////////////////////////////////// 
     // chkBox.Checked *is* a boolean value, no need to convert to Bool 
     // 
     if (chkBox != null && chkBox.Checked) 
     { 
      ////////////////////////////////// 
      // Since strDbSearch doesn't change 
      // just make it part of the select string you are building 
      // This eliminates the need for parameter passing 
      // 
      // Also, check the string you are building 
      // It doesn't look like it builds a proper SELECT statement 
      // You're missing a field after "AND" 
      cmd.CommandText = 
       "SELECT id FROM conceptidmapping " 
       "WHERE database ILIKE " + strDbSearch + " AND " + 
       (chkBox.Checked == true) + " ORDER BY id ASC"; 

      ////////////////////////////////// 
      // No parameters necessary, but... 
      // This already assigns the value to the parameter 
      // 
      //cmd.Parameters.AddWithValue("@database", strDbSearch); 
      // 
      // So there is no need to do it again here 
      // 
      //cmd.Parameters["@database"].Value = Convert.ToString(strDbSearch); 


      string idVal = (string)cmd.ExecuteScalar(); 

      /////////////////////////////////// 
      // IF YOU ARE ABLE TO MAKE THE ID A DATAKEY 
      // Then the entire cmd for the select call above 
      // can be removed in favor of this: 
      // 
      string idVal = Gridview1.DataKeys(row.RowIndex).Values("id") 

      cmd1.CommandText = "UPDATE conceptidmapping SET selected = true WHERE database ILIKE " + strDBSearch + " AND id = " + idVal ; 

      cmd1.ExecuteNonQuery(); 
     } 
     } 
    } 
    } 
} 

同上面的代码没有注释,并用datakey做

protected void UpdateSelectedRecords() 
{ 
    string strDbSearch = String.Format("%{0}%", ddlDatabasesUpdateTCID.Text); 

    using(OdbcConnection Postgreconnect2 = new OdbcConnection("Driver= {PostgreSQL Unicode};Server=localhost;Port=5433;Database=postgres;Uid=xxx;Pwd=xxx")); 
    { 
    Postgreconnect2.Open(); 
    OdbcCommand cmd1 = new OdbcCommand("", Postgreconnect2); 

    foreach (GridViewRow row in GridView1.Rows) 
    { 
     if (row.RowType == DataControlRowType.DataRow) 
     { 
     chkBox = (row.Cells[0].Controls[0] as CheckBox); 
     if (chkBox != null && chkBox.Checked) 
     { 
      string idVal = Gridview1.DataKeys(row.RowIndex).Values("id"); 

      cmd1.CommandText = String.Format("UPDATE conceptidmapping SET selected = true WHERE database ILIKE {0} AND id = {1}", strDbSearch, idVal) ; 
      cmd1.ExecuteNonQuery(); 
     } 
     } 
    } 
    } 
} 
相关问题