2014-02-07 54 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

如果單擊按鈕,但沒有任何反應,這是極有可能是你沒有掛鉤與按鈕控制你的事件處理程序,否則至少黃色網頁會出來。