2017-06-04 134 views
1

我只想顯示一些特定查詢的gridview。 我試過下面。 當查詢中傳遞了錯誤的參數時,此代碼顯示「無數據匹配」,但即使參數正確,也不顯示任何數據。 我是新來asp.net。請告訴我,有什麼重要的線,我錯過Gridview表不在asp.net中顯示webform

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      ViewState["Filter"] = "ALL"; 
      BindGrid(); 
     } 
    } 



private void BindGrid() 
    { 
     DataTable dt = new DataTable(); 
     String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString1"].ConnectionString; 
     SqlConnection con = new SqlConnection(strConnString); 
     SqlDataAdapter sda = new SqlDataAdapter(); 
     string query = "SELECT ContactName, City, Country, PostalCode FROM Customers"; 
     SqlCommand cmd = new SqlCommand("SELECT ContactName, City, Country, PostalCode FROM Customers WHERE Country='UK'"); 
     cmd.Connection = con; 
     sda.SelectCommand = cmd; 
     sda.Fill(dt); 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
    } 

回答

0

的代碼工作,並返回從Northwind數據庫7行到GridView控件。

enter image description here

連接字符串應該是這樣的web.config文件:

<connectionStrings> 
     <add name="NorthwindConnectionString1" connectionString="server=.; database=northwnd; integrated security=true" providerName="System.Data.SqlClient"/> 
</connectionStrings>