How to Pass DDL Selected Value to Data Access Layer

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
iNetBlazerAsked:
Who is Participating?
 
masterpassConnect With a Mentor Commented:
you cannot access the DDL from the DB Layer. What you should be doing is pass the value as argument.
You will have to re-write two functions here
DB LAYER

Public Shared Function CoursesDDL_Filter(ByVal CourseID as String) 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", CourseID)

        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

ASPX

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(CoursesDDL.SelectedValue.ToString())
        CoursesGridView.DataBind()
End Sub

Open in new window

0
 
iNetBlazerAuthor Commented:
rkworlds:,

Thank you for your response but I currently have the data layer implemented.  The problem is at run time I get the following error; 'cmd.Parameters.AddWithValue("@CourseDDLid", CoursesDDL.SelectedValue.ToString)'.  This tells me the DropDownList is not recognized as existing.  How do I tell it to find the dropdownlist on the UI page.  I have written a 'Find Control" action as follows, but it did not work.  

Dim CoursesDDL As DropDownList
CoursesDDL = CType(ContentPlaceHolder1.FindControl("CoursesDDL"), DropDownList)

All I just want to do is... make a selection with the DDL, pass the DDL value to the 'CoursesDDL_Filter' Fuction (data layer), and return the requested query back to the Gridview.



0
 
masterpassCommented:
All I just want to do is... make a selection with the DDL, pass the DDL value to the 'CoursesDDL_Filter' Fuction (data layer)---------> Did you have look  the code I posted ?
0
 
iNetBlazerAuthor Commented:
masterpass,

Your code was posted as I was writing my last post and when I noticed it I took note.  And, "Pow" it worked.  I did have to declare it in the DDL method and pass it to the business logic, but all-in-all it work perfect.  Thank you.  In the great words of Kevin Spacey, "pay it forward" and I'll do just that.  Thank you.
0
All Courses

From novice to tech pro — start learning today.