2016-11-22 5 views
0

私はC#の新機能です。私はユーザーがドロップダウンリストから選択した科目に基づいてクラスを見ることを可能にするウェブアプリケーションを作成しようとしています。私が持っているコードは、ユーザーがドロップダウンリストから選択した情報を表示していません。どちらのオプションを選択しても同じグリッドビューのデータが表示されます。私の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

@コーディングYoshiどうすればよいかわからない –

+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

心配しないで、私はそれを修正したと信じています –

関連する問題