【问题标题】:Cannot locate 'id' value in database table when selecting column checked in UI (checkbox)选择 UI 中选中的列时无法在数据库表中找到“id”值(复选框)
【发布时间】:2017-02-16 02:46:28
【问题描述】:

请帮忙。这是我为我公司的客户服务团队工作了大约 6 周的 UI,他们渴望使用它。

如标题所示,我正在尝试选择与包含复选框的行关联的“ID”值。如果该复选框被“选中”,我希望运行一个选择语句并从该行中获取“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();
        }
      }
    }
  }

【问题讨论】:

  • 我很抱歉最初没有提供太多这方面的信息。发生的事情是当程序进入“For”循环时,它通过 Gridview(从上到下)逐行导航。当它看到复选框被选中时,它进入第二个 IF 语句(使用 ODBC 命令)。第一个 ODBC 命令尝试定位该行的“ID”值。一旦获得了“ID”值,它将在第二个 ODBC 命令中使用该值将“选定”列从“假”更新为“真”。
  • 发生的事情是它确实从正确的数据库中获取了一个“ID”值,但它从来不是我选中复选框的那个。有没有人看到我在这种方法中有任何简单的缺陷?我也在“代码隐藏”文件(C#)中完成这一切,因为该过程的第一步是从下拉列表中选择一个“数据库”值。我可能错了,但认为在标记中填充的命令中使用这种方法会很困难。非常欢迎任何建议!

标签: gridview checkbox


【解决方案1】:

GridView 非常擅长显示和管理数据。您声明您有“与行关联的‘ID’值”。但你没有,你实际上是在稍后进行数据库调用以检索 ID

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

但是,有时您没有该选项,因此这里是您的代码,已使用两种解决方案进行了修改,并添加了一些注释供您考虑。

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

上面的相同代码没有 cmets 并使用 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();
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    相关资源
    最近更新 更多