2013-09-21 94 views
0

我试图从数据库中检索信息。用户输入他正在寻找的人的身份证文本框的ID,而不是按下显示按钮。网格视图应显示结果。但是当按下按钮时,没有任何反应。任何人都可以帮忙或告诉我我该检查什么? 代码按钮:单击按钮 - 无动作

protected void btnDisplay_Click(object sender, EventArgs e) 
     { 
      SqlConnection conn = new SqlConnection("Data Source="Name";Initial Catalog="Name";Integrated Security=True"); 
      SqlCommand cmd = new SqlCommand("displayData", conn); 
      conn.Open(); 
      cmd.Parameters.Add("@ID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(txtID.Text); 
      cmd.CommandType = CommandType.StoredProcedure; 
      SqlDataReader rd = cmd.ExecuteReader(); 
      grvResults.DataSource = rd; 
      grvResults.DataBind(); 
     } 

在这里被存储的过程:

USE ["Name"] 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER procedure [dbo].[displayData] (@ID int) 
as 
begin 
SELECT * FROM Customers WHERE ID = @ID 
end 

这里是显示数据的方法:

public List<Customer> displayData() 
     { 
      List<Customer> lst = new List<Customer>(); 
      SqlConnection conn = new SqlConnection("Data Source="Name";Initial Catalog="Name";Integrated Security=True"); 
      SqlCommand cmd = new SqlCommand("Select * From Customers", conn); 
      conn.Open(); 
      SqlDataReader rd = cmd.ExecuteReader(); 
      while (rd.Read()) 
      { 
       lst.Add(new Customer() 
       { 
        ID = rd.GetInt32(0), 
        FName = rd.GetString(1), 
        LName = rd.GetString(2) 

       }); 
      } 
      return lst; 
     } 

ASPX为按钮:

<asp:Button ID="btnDisplay" runat="server" Text="Display" OnClick="btnDisplay_Click" /> 
+0

你也可以显示按钮的ASPX标记吗? –

+0

当然,刚添加。 – Eugene

+0

除非我错过了一些东西 - 它看起来没问题。你有什么错误吗?如果没有,你确定SP返回数据吗?你可以尝试使用相同的参数从SSMS自行运行它吗? –

回答

0

您是否尝试在按钮单击中放置断点?它是否达到断点?如果不是那么我会说删除

OnClick="btnDisplay_Click" 

从您的aspx页面。然后通过选择控件及其事件来添加.cs页面中的默认点击事件。他们尝试逐行调试。

+0

我检查了onclick事件,我好像我有VS错误。当我再次重写了一切,它开始工作 – Eugene

0

给这个c在你的代码隐藏中尝试一下。它应该正确绑定并填充gridview。如果有效,请从此处添加更多代码。如果它不起作用,请查看连接字符串等内容。让我知道。

 SqlConnection conn = new SqlConnection("Data Source="?";Initial Catalog="?";Integrated Security=True"); 

     SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE ID = @ID", conn); 
     cmd.Parameters.Add("@ID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(txtID.Text); 

     SqlDataAdapter da = new SqlDataAdapter(); 
     da.SelectCommand = cmd; 

     DataTable dt = new DataTable(); 

      conn.Open(); 
      da.Fill(dt); 
      conn.Close(); 

     grvResults.DataSource = dt; 
     grvResults.DataBind(); 
+0

为什么?网格可以绑定到DataReader以及DataTable。 –

+0

我不确定他想要完成什么。他在他的代码中使用了'conn.Open()'两次,并且从不调用'conn.Close()'我只是想提供可行的代码,我怀疑数据表和数据读取器之间的性能是一个考虑因素。 – Zerkey

+0

感谢您的帮助,将尽力。 – Eugene

0

我认为你是复杂的自己太多 首先做到这一点: Select the first option: SQL statement or stored procedure Then select your stored procedure Select The source Control (your Textbox) then click next

Then enter your client Id and click the button. NOTE on the button click you need to write this: **SqlDataSource1.DataBind();**

这应该使它发挥作用。你已经有了一个存储过程,这使得它更容易。 (对不起,我想确保你明白这一点)

+0

谢谢,是的,它的工作原理,我以前试过。我只是想以其他方式去做。 – Eugene