Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How to Pass DDL Selected Value to Data Access Layer

Avatar of iNetBlazer
iNetBlazerFlag for United States of America asked on
MySQL Server.NET ProgrammingASP.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
Avatar of masterpass
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answers