2014-02-07 59 views
-1

美好的一天!我想从我的数据库中检索数据,用户将在文本框中输入,当用户单击搜索按钮时,它将显示在窗体中,但是当我执行代码下面什么都不会发生。谁能帮我。从C#中的数据库检索数据.net

namespace booksreviews 
    { 
    public partial class search : System.Web.UI.Page 
    { 
    private SqlConnection connection; 
    private SqlCommand command; 
    private int count = 0; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     SqlConnection Con = new SqlConnection("connectionstring"); 


    } 
    public SqlConnection con { get; set; } 

    protected void btnSearch_Click(object sender, EventArgs e) 
    { 

     try{ 
     con.Open() 
     int result = command.ExecuteNonQuery(); 
     if (txtTitle.Text == string.Empty) 
    { 
    SqlCommand cmd = new SqlCommand(" 
    Select                 
    title,authors,publisher,price,nopages,pubdate from book ", con); 
    } 
    else 
    { 
    SqlCommand cmd = new SqlCommand(" 
    select title,authors,publisher,price,nopages,pubdate from book where 
    id= '" + txtTitle.Text + "'", con); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 
    dlBooks.DataSource = ds; 
    dlBooks.DataBind(); 
}catch (ArgumentException argumentEx) 
    { 
     Response.Write(argumentEx.Message); 
    } 
    catch (SqlException ex) 
    { 
     Response.Write(ex.Message); 
    } 
    catch (IndexOutOfRangeException bound) 
    { 
     Response.Write(bound.Message); 
    } 
    finally 
    { 
     connection.Close(); 
    } 
     } 
    } 
    } 
    } 
+0

您的连接字符串无效。 – Jade

+0

你是否收到任何错误信息?另外,只要你输入'btnSearch_Click()'方法,你就执行'command' SqlCommand,在这一点上它是NULL? –

回答

0

如果单击按钮,但没有任何反应,这是极有可能是你没有挂钩与按钮控制你的事件处理程序,否则至少黄色网页会出来。