Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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. I have the drop down list displaying subjects from the drop down list. The code that I have isn't showing the user the information they chose from the drop down list. It shows the same gridview data not matter which option is chosen.

What I have tried:

This is the code that will show the web application.
ASP.NET
<%@ 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:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RegistrationConnectionString %>" SelectCommand="SELECT [Subjects], [Id] FROM [Subjects]">
        <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" />
            
        
        <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]">
        <br>
    </form>
</body>
</html>

This is the code behind for the application.
C#
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 = "Subjects";
            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;
    }
}
Posted
Updated 22-Nov-16 11:13am
v4
Comments
Er. Puneet Goel 22-Nov-16 3:53am    
So basically what is your problem ? The null reference error or how to bind grid view on dropdown selected value change ?
Member 12863707 22-Nov-16 14:15pm    
I would say both of those issues are my problem But I think if the NullReferenceException error would disappear with a change of code.
haisol 22-Nov-16 14:29pm    
I've typically used sql command query type of stored procedures and added parameters to it (not query type of text). I would change your query to a stored procedure, set the command type to procedure and use the cmd.Parameters.AddWithValue. Another quicker option would be to change your query to this: select * from ODetails where orderID= " + DropDownList1.SelectedValue.ToString() (pseudo code).
Member 12863707 22-Nov-16 14:40pm    
I have updated the code with your suggestion. I am getting a NullReferenceException at my second ConnectionStrings.
Er. Puneet Goel 24-Nov-16 5:10am    
you mean at GetDataTable?

1 solution

Don't think you need the Cn.Open() in the GetData function. This works for me:

C#
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;
}
 
Share this answer
 
v2
Comments
Member 12863707 22-Nov-16 15:28pm    
So this would replace the GetData() function?
Member 12863707 22-Nov-16 15:40pm    
I am also getting an error when I do this saying that "myConnection" doesn't exist in this context.
haisol 22-Nov-16 16:04pm    
One too many close parentheses. Try to copy and paste again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900