根据按钮单击复选框从数据库中获取数据

时间:2015-02-12 05:16:18

标签: c# database buttonclick

我的数据库中有包含列(....,....,....,BLOCK)的表。 BLOCK列具有位数据类型(True,False)。 当BLOCK列为False时,应从数据库中提取数据。 当BLOCK列为True时,不应获取数据,从而导致抛出错误。 当我在文本框中输入特定人员的姓名并单击按钮时,必须执行上述操作

我的按钮点击c#编码是......

 protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
{

    string selectsql = "SELECT * FROM UserDetailsTwo";
    using (SqlConnection con = new SqlConnection(@"Data Source=ENTERKEY001;Initial Catalog=ContactManagement;Integrated Security=True"))//DataBase Connection
    {
        SqlCommand selectCommand = new SqlCommand(selectsql, con);
        con.Open();

        SqlDataReader SelectReader = selectCommand.ExecuteReader();
        while (SelectReader.Read())
        {

            Boolean BLOCK = Convert.ToBoolean(SelectReader["BLOCK"]);
            if (BLOCK == false)
            {

                //con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "SearchUser";
                cmd.Parameters.AddWithValue("@NAME", TextBox4.Text.Trim());
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                con.Close();
            }
            else
            {
                Response.Write("Error");

            }
        }
        SelectReader.Close();
    }
}

1 个答案:

答案 0 :(得分:0)

您正在尝试使用像DataAdapter一样的SQLDataReader。

SelectReader = selectCommand.ExecuteReader();
while (SelectReader.Read())
{

          Int64 BLOCK = Convert.ToInt64(SelectReader["BLOCK"]);
          if (BLOCK == false)
          {

              con.Open();
              SqlCommand cmd = con.CreateCommand();
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.CommandText = "SearchUser";
              cmd.Parameters.AddWithValue("@NAME", TextBox4.Text.Trim());
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              DataTable dt = new DataTable();
              da.Fill(dt);
              GridView1.DataBind();
              con.Close();
          }
          else
          {
              Response.Write("Error");

          }
      }

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader%28v=vs.110%29.aspx