Keith Code Keith Code - 2 months ago 8
ASP.NET (C#) Question

The gridview data in my web application is not corresponding to the selection chosen from the dropdownlist

I am very new to C#. I am trying to create a web application that will allow users to see classes based on a subject they choose from a drop down list. The code I have isn't showing the user the information they chose from the dropdownlist. It shows the same gridview data no matter which option is chosen.

This is the code that will show the web application.

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

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<title>Summer 2016</title>
<form id="form1" runat="server">
<h1>Summer 2016</h1>
<h2>Classes offered</h2>
<h3>Please Choose a Subject</h3>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Subjects" DataValueField="Subjects">
<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">
<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" />
<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 />

This is the code behind for the web application.

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";

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))

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


public static DataTable GetDataTable(string sqlCommand)
DataTable table = new DataTable();
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))
catch (Exception ex)
table = null;
throw ex;
return table;


Update Dropdown element as below,

runat="server" AutoPostBack="True"

DataSourceID="SqlDataSource1" DataTextField="Subjects"