2013-07-25 47 views
3

我完全不熟悉ASP.NET,並試圖將一個簡單的Web應用程序放在一起,該應用程序允許用戶查看(現在就是這樣)來自SQL數據庫基於他們從ListBox和Dropdown中的選擇。這個想法是,在選擇之後單擊按鈕將返回基於正在傳遞的存儲過程的數據庫結果列表和下拉選擇作爲參數。我想我非常接近,但我似乎無法將結果返回到爲此目的指定的DataGrid。我正在運行的C#代碼如下。任何幫助是極大的讚賞!!!ASP.NET新手 - 無法從SQL數據庫更新DataGrid

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Xml.Linq; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace Timeline_Analytics_App_Test 
{ 
    public partial class Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      string Host = ListBox1.SelectedValue; 
      string Test = DropDownList1.SelectedValue; 
      this.SqlDataSource3.SelectCommand = "EXEC dbo.TEST_RESULT_DETAIL '" + Host + "', " + Test; 
      //MessageBox.Show(this.SqlDataSource3.SelectCommand); 
      //this.GridView1.DataBind(); 

      String queryString = SqlDataSource3.SelectCommand; 

      MessageBox.Show(queryString); 

      DataSet ds = GetData(queryString); 

      if (ds.Tables.Count > 0) 
      { 
       //MessageBox.Show("1"); 
       GridView1.DataSource = ds; 
       //MessageBox.Show("2"); 
       GridView1.DataBind(); 
       //MessageBox.Show("3"); 
      } 
      else 
      { 
       MessageBox.Show("Unable to connect to the database."); 
      } 

     } 

     DataSet GetData(String queryString) 
     { 

      // Retrieve the connection string stored in the Web.config file. 
      String connectionString = ConfigurationManager.ConnectionStrings["Timeline_AnalyticsConnectionString3"].ConnectionString; 

      DataSet ds = new DataSet(); 

      try 
      { 
       // Connect to the database and run the query. 
       SqlConnection connection = new SqlConnection(connectionString); 
       SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection); 

       // Fill the DataSet. 
       adapter.Fill(ds); 
      } 
      catch (Exception ex) 
      { 
       // The connection failed. Display an error message. 
       MessageBox.Show("Unable to connect to the database."); 
      } 

      return ds; 
     } 
    } 
} 

加載項:

我的Default.aspx:

<%@ Page Language="C#" MasterPageFile="~/TATRRT.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Timeline_Analytics_App_Test.Default" Title="Untitled Page" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 

<asp:ScriptManager ID="ScriptManager1" runat="server" /> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
<ContentTemplate> 

    <%--Host List Box--%> 
    <div> 
     <h4 style="font-family:verdana; font-size:10pt; position:absolute;left:10px;top:140px">Please select the host you want to review</h4> 
     <asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" style="font-family:verdana; font-size:10pt; position:absolute;left:10px;top:180px" 
      DataTextField="Host" DataValueField="Host" Height="150" Width="320"></asp:ListBox> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:Timeline_Analytics_App_TestConnectionString %>" 
      SelectCommand="SELECT DISTINCT Host FROM Test_Summary WHERE Category IS NULL AND Responsive = 1"> 
     </asp:SqlDataSource> 
     <br></br> 
    </div> 

    <%--Test Dropdown--%> 
    <div> 
     <h4 style="font-family:verdana; font-size:10pt; position:absolute;left:350px;top:140px">Please select the test you want to review</h4> 
     <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" style="font-family:verdana; font-size:10pt; position:absolute;left:350px;top:180px" 
      DataTextField="Test_Name" DataValueField="Test_Name" Width="320"> 
     </asp:DropDownList> 
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
      ConnectionString="<%$ ConnectionStrings:Timeline_Analytics_App_TestConnectionString2 %>" 
      SelectCommand="SELECT DISTINCT CAST(RIGHT(REVERSE(RIGHT(REVERSE(Test_Name), 8)), 2) AS INT) AS Test_Name FROM Test_Summary ORDER BY CAST(RIGHT(REVERSE(RIGHT(REVERSE(Test_Name), 8)), 2) AS INT)"> 
     </asp:SqlDataSource> 

    <%--Test Result Summary--%> 
    <asp:GridView ID="GridView1" runat="server" 
      style="font-family:verdana; font-size:10pt; position:absolute;left:10px;top:350px" AllowPaging="True" 
      AutoGenerateColumns="False"> 
      <%--DataSourceID="SqlDataSource3"--%> 
    </asp:GridView> 
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
     ConnectionString="<%$ ConnectionStrings:Timeline_AnalyticsConnectionString3 %>" 
     SelectCommand=""> 
    </asp:SqlDataSource> 
    </div> 
    <asp:Button ID="Button1" runat="server" Text="Retrieve results" 
     style="font-family:verdana; font-size:10pt; position:absolute;left:680px;top:180px" 
     onclick="Button1_Click"/> 

</ContentTemplate> 
</asp:UpdatePanel> 

</asp:Content> 
+0

哪裏是在您的標記你的GridView的定義是什麼?另外,是否正確地從數據庫返回數據;換句話說,DataSet是否被填充或發生任何錯誤? –

+0

你是否收到任何錯誤信息? –

+0

在此代碼迭代中沒有任何錯誤被拋出 - if - else條件中的消息不包含任何問題。我用我用來測試應用程序的確切參數測試了sproc,它確實返回結果。 –

回答

0

您的問題是AutoGenerateColumns="False",你必須在你的標記沒有Columns定義,因此當GridView數據綁定有沒有什麼可綁定的。

最簡單的解決方法是設置AutoGenerateColumnsTrue,像這樣:

AutoGenerateColumns="True" 

注:這會使你的列名一樣的數據庫字段,這可能會或可能不是你想要的是。

,並得到了GridView列更多的控制權,然後檢查了GridView Column Property MSDN Documentation

+0

卡爾 - 非常感謝你!這似乎已經成功了,我的網格正在填充!另外,這是我第一次發佈Stack Overflow(雖然我以前用它作爲參考),但是我想要感謝所有對我的問題感興趣的人 - 儘管我知道情況並非總是如此,欣賞這個解決的速度!再次感謝!!! –

+0

沒問題,祝您的GridView代碼順利。如果你覺得這個答案對你有幫助,那麼可以隨時接受它。 –