2015-02-12 121 views
-1

我有我的数据库中的表组成列(....,....,.... BLOCK)。 BLOCK列有位数据类型(True,False)。 当BLOCK列有False时,应该从数据库中提取数据。 当BLOCK列为真时,不应该获取数据,导致抛出错误。 当我给一个特定的人在文本框的名称,然后单击按钮,上述操作必须进行从数据库取数据基于复选框上的按钮点击

我的按钮单击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

是什么错误?你调试的代码? – 2015-02-12 05:17:47

+0

它在我的代码的第12行(ststement行)中抛出一个错误,告诉“ERROR CONVERTING DATATYPE NVARCHAR TO BIGINT”。 – 2015-02-12 05:26:01

+0

它告诉我一个错误,告诉“已经有一个打开的DataReader与这个命令关联,必须先关闭它”。在我的第28行(da.Fill(dt);) – 2015-02-12 06:37:40

回答

0

您要使用像一个SqlDataReader的一个DataAdapter。

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

+0

同一行上的同一错误(while(SelectReader.Read())) – 2015-02-12 05:33:46

+0

表中的ID的数据类型是什么?你在哪里定义变量ID? – 2015-02-12 05:42:38

+0

现在我已经在我的代码 – 2015-02-12 06:32:17

相关问题