2016-11-22 47 views
0

我對C#非常陌生。我正在嘗試創建一個Web應用程序,使用戶可以根據他們從下拉列表中選擇的主題查看課程。我擁有的代碼沒有向用戶顯示他們從下拉列表中選擇的信息。無論選擇哪個選項,它都會顯示相同的gridview數據。我的web應用程序中的gridview數據不對應於從下拉列表中選擇的選項

這是將顯示Web應用程序的代碼。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Summer.aspx.cs"Inherits="Summer" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Summer 2016</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <h1>Summer 2016</h1> 
     <h2>Classes offered</h2> 
     <h3>Please Choose a Subject</h3> 
    </div> 
     <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Subjects" DataValueField="Subjects"> 
     </asp:DropDownList> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RegistrationConnectionString %>" SelectCommand="SELECT [Subject], [Id] FROM [Classes]"></asp:SqlDataSource> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource2"> 
      <Columns> 
       <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" /> 
       <asp:BoundField DataField="Instrutor" HeaderText="Instrutor" SortExpression="Instrutor" /> 
       <asp:BoundField DataField="CRN" HeaderText="CRN" SortExpression="CRN" /> 
       <asp:BoundField DataField="Credits" HeaderText="Credits" SortExpression="Credits" /> 
       <asp:BoundField DataField="Day" HeaderText="Day" SortExpression="Day" /> 
       <asp:BoundField DataField="Time" HeaderText="Time" SortExpression="Time" /> 
       <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> 
       <asp:BoundField DataField="Section" HeaderText="Section" SortExpression="Section" /> 
       <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" /> 
       <asp:BoundField DataField="BeginEnd" HeaderText="BeginEnd" SortExpression="BeginEnd" /> 
       <asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" /> 
       <asp:BoundField DataField="SubjectId_Fk" HeaderText="SubjectId_Fk" SortExpression="SubjectId_Fk" /> 
      </Columns> 
     </asp:GridView> 
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:RegistrationConnectionString %>" SelectCommand="SELECT [Id], [Instrutor], [CRN], [Credits], [Day], [Time], [Title], [Section], [Location], [BeginEnd], [Number], [SubjectId_Fk] FROM [Classes]"></asp:SqlDataSource> 
     <br /> 
    </form> 
</body> 
</html> 

這是Web應用程序背後的代碼。

enter code here 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Data; 
using System.Configuration; 
public partial class Summer : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      // DropDownList1.DataSource = GetDataTable(); 
      DropDownList1.DataValueField = "Id"; 
      DropDownList1.DataTextField = "Subject"; 
      DropDownList1.DataBind(); 
     } 
    } 

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
      using (SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings[@"C:\Users\Keith\Documents\Registration.mdf"].ConnectionString)) 
      { 
      using (SqlCommand Cmd = new SqlCommand("select * from Classes where Id=" + DropDownList1.SelectedValue.ToString(), Cn)) 
      { 
      Cn.Open(); 

       Cmd.Parameters.AddWithValue("@Id", int.Parse(DropDownList1.SelectedValue)); 
       SqlDataReader Dr = Cmd.ExecuteReader(); 
       if (Dr.HasRows) 
       { 
        GridView1.DataSource = Dr; 
        GridView1.DataBind(); 
       } 
       Dr.Close(); 

       Cn.Close(); 
      } 

     } 
    } 
    public static DataTable GetDataTable(string sqlCommand) 
    { 
     DataTable table = new DataTable(); 
     try 
     { 
      using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[@"C:\Users\Keith\Documents\Registration.mdf"].ConnectionString)) 
      { 
       using (SqlCommand cmd = new SqlCommand(sqlCommand, myConnection)) 
       { 
        using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) 
        { 
         adapter.Fill(table); 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      table = null; 
      throw ex; 
     } 
     return table; 
    } 
} 
+0

當你把上面代碼中的斷點,都被打? – CodingYoshi

+0

@CodingYoshi我不知道如何做到這一點 –

+0

如果您使用Visual Studio,請轉到特定的行按F9。你會看到一個紅點。然後啓動網站並更改下拉菜單,並停止在該點。如果沒有,那就是你的問題。 – CodingYoshi

回答

0

如下更新下拉元素,

<asp:DropDownList ID="DropDownList1" 

runat="server" AutoPostBack="True" 

DataSourceID="SqlDataSource1" DataTextField="Subjects" 

DataValueField="Subjects" 

onselectedindexchanged="DropDownList1_SelectedIndexChanged"> 

    </asp:DropDownList> 
+0

它現在給我一個NullReferenceException在我的連接字符串 –

+0

沒關係,我相信我修復了它 –

相關問題