2013-07-25 1 views
3

저는 완전히 ASP.NET에 익숙하며 사용자가 (현재는 그만큼) 데이터를 볼 수있는 간단한 웹 응용 프로그램을 만들려고합니다. SQL 데이터베이스는 ListBox 및 드롭 다운에서 선택한 항목을 기반으로합니다. 선택 후 버튼을 클릭하면 목록 및 드롭 다운 선택 항목이 매개 변수로 전달되는 저장된 프로 시저를 기반으로 데이터베이스 결과가 반환됩니다. 나는 거의 비슷하다고 생각하지만 그 목적을 위해 지정된 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"이며, 따라서 GridView 데이터가있다 바인딩 할 때, 더 Columns이 마크 업에 정의가 없다 구속 할 것이 없다.

가장 쉬운 수정과 같이 TrueAutoGenerateColumns을 설정하는 것입니다 :

AutoGenerateColumns="True" 

참고 :이 또는 당신이 원하는하지 않을 수 있습니다 데이터베이스 필드와 같은 열 이름이 동일 할 것입니다.

는 마크 업에서의 GridView의 정의입니다 GridView Column Property MSDN Documentation

+0

Karl - 정말 고마워요! 이것은 트릭을 수행 한 것, 내 표가 바로 채우고 있습니다! 또한, Stack Overflow에 대한 저의 첫 번째 게시물입니다. (비록 과거에 참조로 사용 했었지만) 제 질문에 관심이있는 모든 분들께 감사 드리고 싶습니다. 항상 그렇지는 않을 것입니다. 이것이 해결 된 속도에 감사드립니다! 다시 한 번 감사드립니다! –

+0

GridView 코드에 문제가 없습니다. 행운을 빈다. 이 답변으로 도움이되었다고 생각되면 언제든지 받아 들일 수 있습니다. –

관련 문제