troubleshooting Question

How to Pass DDL Selected Value to Data Access Layer

Avatar of iNetBlazer
iNetBlazerFlag for United States of America asked on
.NET ProgrammingMySQL ServerASP.NET
5 Comments1 Solution518 ViewsLast Modified:
Hello Experts,

I'm having a difficult challenge of passing a DropDownList Selected Value to a Data Layer having a Function containing 'cmd.Parameters.AddWithValue("@CourseDDLid", CoursesDDL.SelectedValue.ToString)'.  Visual Studio is indicating I need to declare 'CoursesDDL'.  I tried accessing the 'Find Control' method but it did not work.  Below are the codes for your review.  Thank you for your assistance.

ASPX PAGE

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div id="courseTitle"><h3>Courses</h3></div>
     
    <div id="courseSearchBar">
       
        <asp:DropDownList ID="CoursesDDL" runat="server" Width="140px"              
            OnSelectedIndexChanged="CoursesDDL_SelectedIndexChanged" AutoPostBack="True" AppendDataBoundItems="True">
            <asp:ListItem Value="0" Text="Please Select:"></asp:ListItem>
        </asp:DropDownList>
</div>

<div id="courseGridview">
        <asp:GridView ID="CoursesGridView" runat="server" Width="610px"
            AllowPaging="True" OnRowDataBound="courseGridview_RowDataBound"
                EnableSortingAndPagingCallbacks="True">
            <HeaderStyle CssClass="coursesGridviewHeader" />
        </asp:GridView>
        <%--End Gridview--%>
</div>


ASPX.VB PAGE

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then

            CoursesGridView.DataSource = CoursesLogicManager.DisplayCourses
            CoursesGridView.DataBind()
            'CourseGridview.HeaderRow.Cells(0).Visible = False

            CoursesDDL.DataSource = CoursesLogicManager.CoursesDDL_Search
            CoursesDDL.DataTextField = "Courses"
            CoursesDDL.DataValueField = "Id"
            CoursesDDL.DataBind()

        End If
    End Sub

Protected Sub CourseGridview_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(0).Visible = False
End If
End Sub

Protected Sub CoursesDDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CoursesDDL.SelectedIndexChanged

        If CoursesDDL.SelectedValue Then
            LmsDataBaseManager.CoursesDDL_Filter()
        End If

        CoursesGridView.DataSource = LmsDataBaseManager.CoursesDDL_Filter()
        CoursesGridView.DataBind()
End Sub


BUSINESS LOGIC

Public Class CoursesLogicManager

Public Shared Function DisplayCourses() As DataSet
        Return LmsDataBaseManager.GetCourses()
    End Function

Public Shared Function CoursesDDL_Filter_GV() As DataSet
        Return LmsDataBaseManager.CoursesDDL_Filter()
    End Function

End Class

DATA ACCESS

Public Class LmsDataBaseManager

Public Shared Function GetCourses() As DataSet
        Dim ds As DataSet = New DataSet()
        Using conn As New SqlConnection(LmsConnectionConfig.CoursesConnection)
            Dim cmd As SqlCommand = New SqlCommand("spCourses", conn)
            cmd.CommandType = CommandType.StoredProcedure
            Dim da As SqlDataAdapter
            da = New SqlDataAdapter
            da.SelectCommand = cmd
            da.Fill(ds, "Courses, Code, Date, StartTime, EndTime, Enrollment")
            Return ds
            conn.Close()
        End Using
    End Function
End Class


Public Shared Function CoursesDDL_Filter() As DataSet
        Dim ds As DataSet = New DataSet()
Using conn As New SqlConnection(LmsConnectionConfig.CoursesConnection)
            Dim cmd As SqlCommand = New SqlCommand("spCoursesDDL_Filter", conn)
            cmd.CommandType = CommandType.StoredProcedure            
           
            cmd.Parameters.AddWithValue("@CourseDDLid", CoursesDDL.SelectedValue.ToString)

            Dim da As SqlDataAdapter
            da = New SqlDataAdapter

            da.SelectCommand = cmd
            da.Fill(ds, "Id, Courses, Code, Date, StartTime, EndTime, Enrollment")
            Return ds
            conn.Close()
        End Using
    End Function


THANK YOU
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros