2015-02-08 78 views
0

我試圖顯示一個菜單的音樂類別嵌入在default.aspx頁面的用戶控件。我運行default.aspx頁面時沒有收到任何生成錯誤,但沒有顯示我的sql查詢結果。數據列表不顯示SQL查詢 - 沒有生成錯誤

我自己運行了sql查詢,它正確顯示,所以它是關於數據列表從.mdf中檢索數據以顯示它的方式。

這裏是我的.cs文件代碼:

using System; 
using System.Web; 
using System.Web.Configuration; 
using System.Data; 
using System.Data.SqlClient; 

/// <summary> 
/// Provides methods for Sql Server database queries 
/// Requires database connection string to be located in web.config 
/// </summary> 
public class MusicStoreSample2008 

{ 
//global scope 
private SqlConnection myConnection = new SqlConnection(); 

//constructor 
public MusicStoreSample2008(string ConnectingStringNameInWebConfig) 
{ 
    string connectString = WebConfigurationManager.ConnectionStrings[ConnectingStringNameInWebConfig].ConnectionString; 
    myConnection.ConnectionString = connectString; 
} 

/// <summary> 
/// SELECT queries: Copy and modify this method for queries that return 
/// multiple rows and/or fields of data. 
/// </summary> 
/// <returns>DataTable</returns> 
public DataTable fillMenu() 
{ 
    string sql = "select s.styleID, s.StyleName from tblStyles s, tblStyleASIN sa where s.StyleID = sa.StyleID group by s.StyleID, s.StyleName order by s.StyleName"; 
    DataTable dt = new DataTable(); 
    //using statement destroys object when operation is complete or throws exception. 
    using (SqlCommand cmd = new SqlCommand(sql, myConnection)) 
    { 
     //cmd.Parameters.AddWithValue("@StyleName", StyleName); 
     myConnection.Open(); 
     dt.Load(cmd.ExecuteReader()); 
     myConnection.Close(); 
    } 
    return dt; 
} 

public DataTable selectCustID(int custID) 
{ 
    string sql = "select custID, nameF, nameL from tblCustomers where custID = @custID"; 
    DataTable dt = new DataTable(); 

    using (SqlCommand cmd = new SqlCommand(sql, myConnection)) 
    { 
     cmd.Parameters.AddWithValue("@custID", custID); 
     myConnection.Open(); 
     dt.Load(cmd.ExecuteReader()); 
     myConnection.Close(); 
    } 
    return dt; 
} 

和我的leftmenu.acsx文件代碼:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="leftMenu.ascx.cs" Inherits="leftMenu" %> 

<script runat="server"> 

void page_load() 
{ 
    if (!Page.IsPostBack) 
    { 
     PopulateMenu(); 
    } 
} 

void PopulateMenu() 
{ 
    MusicStoreSample2008 dbMusic = new MusicStoreSample2008("music"); 
    dlStyles.DataSource = dbMusic.fillMenu(); 
    dlStyles.DataBind(); 
} 

// Search onTextChanged 
public void search(object sender, EventArgs e) 
{ 
    Response.Redirect("SearchBrowse.aspx?query=" + tbSearch.Text); 
} 

// Search onClick 
public void style(object sender, EventArgs e) 
{ 
    Response.Redirect("SearchBrowse.aspx?query=" + tbSearch.Text); 
} 

</script> 

<div class="col-md-3 noPaddingRight"> 
     <div class="leftMenu equalHeightColumn"> 
      <div class="menubox"> 
       <b>Search</b><br /> 
       <asp:TextBox ID="tbSearch" runat="server" OnTextChanged="search" Width="113px"/><br /> 
       <asp:Button ID="btSearch" runat="server" OnClick="style" Text="search" /> 
       <br /> 
       <br /> 
       <br /> 

       <b>Browse</b><br /> 

       <asp:DataList ID="dlStyles" runat="server"> 
        <ItemTemplate> 
         <asp:HyperLink ID="hlMenu" CssClass="menulink" runat="server" 
         NavigateUrl='<%# Eval("StyleID", "SearchBrowse.aspx?style={0}") %>' 
         Text = '<%# Eval("StyleName") %>' /> 
        </ItemTemplate> 
       </asp:DataList> 

       <br /> 
      </div> 
     </div> 
    </div> 

最後的代碼爲我的web.config:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
<system.web> 
<compilation debug="true"/> 
</system.web> 
<connectionStrings> 

<!-- Use Sql Server Express connection string on server, comment it when not in use --> 
<!--<add name="customers" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\A05Customers.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>--> 
<!--<add name="music" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MusicStoreSample2008.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>--> 

<!-- Use use LocalDB connection string in Visual Studio --> 
<add name="customers" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\A05Customers.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/> 
<add name="music" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MusicStoreSample2008.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/> 

</connectionStrings> 
</configuration> 

謝謝你的幫助!

+0

嘗試修改您的代碼與和地方VAR的結果= com.ExecuteReader( );然後檢查結果中是否有數據。然後加載dt.Load(result); – Dnyanesh 2015-02-08 03:14:14

回答

0

發現那是因爲我的ascx文件鏈接的其他地方,所有我需要做的就是消除了以下解決我的問題:

CodeFile="leftMenu.ascx.cs" Inherits="leftMenu"