Solved

How to Return Values from a Class to TextBoxes

Posted on 2011-09-19
5
341 Views
Last Modified: 2012-05-12
Hello Experts,

I'm would like to return sql data from a Class to textboxes based on DropDownList selection  but I'm having no success.  Below are my codes.

ASPX Page

<%@ Page Language="VB" MasterPageFile="~/admin/AdminCoursesMasterPage.master" AutoEventWireup="false" CodeFile="adminCoursesBackOffice.aspx.vb" Inherits="adminCoursesBackOffice" title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <div id="backOfficeCourseTitleDDL">
   
    <asp:DropDownList ID="BackOfficeCourseTitleDDL" runat="server"
        AutoPostBack="True" OnSelectedIndexChanged="BackOfficeCourseTitleDDL_SelectedIndexChanged" >
    </asp:DropDownList>
</div>
<br /><br />
<div id="backOfficeCourseTitleTxtBox">
    <asp:Label ID="CourseTitleLabel" runat="server" Text="Course Title:"></asp:Label>&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="CourseTitleTextBox" runat="server"></asp:TextBox>&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="CourseLevelTextBox" runat="server"></asp:TextBox>
</div>
<br /><br />
<div id="preReqLbl"><asp:Label ID="preReqLabel" runat="server" Text="Prerequisite:"></asp:Label></div>
<div id="preRequisite">    
    <asp:TextBox ID="PreReqTextBox" runat="server" TextMode="MultiLine" Width="400px" Height="100px"></asp:TextBox>
</div>
<br /><br />
<div id="courseObjectivesLbl">
    <asp:Label ID="courseObjectiveLabel" runat="server" Text="Course Objectives:"></asp:Label>
</div>
</asp:Content>


ASPX.VB Page

Partial Class adminCoursesBackOffice
    Inherits System.Web.UI.Page

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

            BackOfficeCourseTitleDDL.DataSource = MyDataBaseManager.CoursesBackOffice()
            BackOfficeCourseTitleDDL.DataTextField = "CourseAndLevel"
            BackOfficeCourseTitleDDL.DataValueField = "Id"
            BackOfficeCourseTitleDDL.DataBind()
            BackOfficeCourseTitleDDL.Items.Insert(0, New UI.WebControls.ListItem("Please Select Course:"))

        End If      
    End Sub

Protected Sub BackOfficeCourseTitleDDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BackOfficeCourseTitleDDL.SelectedIndexChanged

        If BackOfficeCourseTitleDDL.SelectedValue Then

BackOfficeCourseTitleDDL.Items.Remove("Please Select Course:")

Dim sCourseId As String            
            sCourseId = BackOfficeCourseTitleDDL.SelectedValue            

            Dim sCourseTitle As String
            sCourseTitle = ""
            Dim sCourseLevel As String
            sCourseLevel = ""
            Dim sPrerequisites As String
            sPrerequisites = ""

Dim MyClass As New MyClass
MyClass.MyFunction(sCourseId, sCourseTitle, sCourseLevel, sPrerequisites)

CourseTitleTextBox.Text = MyClass .sCourseTitle
            CourseLevelTextBox.Text = MyClass .sCourseLevel
            PreReqTextBox.Text = MyClass .sPrerequisites

End If
End Sub
End Class



App_Code > Business Logic > MyClass.VB

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlConnection

Public sCourseTitle As String
    Public sCourseLevel As String
    Public sPrerequisites As String


#Region "Properties"

Public ReadOnly Property CourseTitle() As String
        Get
            Return sCourseTitle
        End Get

Public ReadOnly Property CourseLevel() As String
            Get
                Return sCourseLevel
            End Get

Public ReadOnly Property Prerequisites() As String
            Get
                Return sPrerequisites
            End Get

#End Region

#Region "Method"

Public Function AdminCourses(ByVal sCourseId As String, ByRef sCourseTitle As String, ByRef sCourseLevel, ByRef sPrerequisites As String As String) As DataTable
        Dim ds As DataSet = New DataSet()
        Using conn As New SqlConnection(MyConnectionConfig.CoursesConnection)
            Dim cmd As SqlCommand = New SqlCommand("spAdminCourses", conn)
            cmd.CommandType = CommandType.StoredProcedure
            Dim da As SqlDataAdapter
            da = New SqlDataAdapter
            Dim dt As New DataTable
            Dim dr As DataRow
            cmd.Parameters.AddWithValue("@sCourseId", sCourseId)


            da.SelectCommand = cmd
            da.Fill(ds, "Id, Courses, SkillLevel, CourseDescription")
            dt = ds.Tables("Id, Courses, SkillLevel, CourseDescription")

            For Each dr In dt.Rows
                sCourseId = dr("Id").ToString
                sCourseTitle = dr("Courses").ToString
                sCourseLevel = dr("SkillLevel").ToString
                sPrerequisites = dr("CourseDescription").ToString
               
            Next            
            Return dt            
            conn.Close()
        End Using
    End Function

#End Region

End Class
0
Comment
Question by:iNetBlazer
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:gery128
Comment Utility
Could you please post the error message?
Or better you can debug through code especially in selectedIndexChanged method of dropdownlist and see whats happening. like if the data is populating in your ADO.NET code in AdminCourse/MyMethod.
0
 

Author Comment

by:iNetBlazer
Comment Utility
I'm not getting an error message.  When I debug the app it return an empty string for each variable/textbox.  The selectedIndexChanged method of dropdownlist is firing/working and it is passing the "ID" value to return only one row of the database table.  I tried testing by returning a dataset and binding it to a gridview to see it I could return anything and it did returned the data/info based on the dropdownlist.  I want to return that same database row and have each field of that row populate each textbox.
0
 
LVL 9

Accepted Solution

by:
gery128 earned 500 total points
Comment Utility
By this code statement:
MyClass.MyFunction(sCourseId, sCourseTitle, sCourseLevel, sPrerequisites)
I guess you are calling AdminCourses function.
I have one question here, why you are passing local string variables (other than sCourseId which is required for accessing data from DB)?
MyClass.MyFunction(sCourseId, sCourseTitle, sCourseLevel, sPrerequisites)
You don't need to pass by ref this three: [sCourseTitle, sCourseLevel, sPrerequisites] because when you are setting variable in AdminCourses function, you have class properties to be later accessed value through.
You are passing local string variables of code behind of AdminCoursesBackOffice.aspx and then using Class variable to set, which is not going to work.
CourseTitleTextBox.Text = MyClass .sCourseTitle
CourseLevelTextBox.Text = MyClass .sCourseLevel
PreReqTextBox.Text = MyClass .sPrerequisites

Modify your AdminCourses like this:
Public Function AdminCourses(ByVal sCourseId As String) As DataTable
        Dim ds As DataSet = New DataSet()
        Using conn As New SqlConnection(MyConnectionConfig.CoursesConnection)
            Dim cmd As SqlCommand = New SqlCommand("spAdminCourses", conn)
            cmd.CommandType = CommandType.StoredProcedure
            Dim da As SqlDataAdapter
            da = New SqlDataAdapter
            Dim dt As New DataTable
            Dim dr As DataRow
            cmd.Parameters.AddWithValue("@sCourseId", sCourseId)


            da.SelectCommand = cmd
            da.Fill(ds, "Id, Courses, SkillLevel, CourseDescription")
            dt = ds.Tables("Id, Courses, SkillLevel, CourseDescription")

            For Each dr In dt.Rows
                sCourseId = dr("Id").ToString
                sCourseTitle = dr("Courses").ToString
                sCourseLevel = dr("SkillLevel").ToString
                sPrerequisites = dr("CourseDescription").ToString
                
            Next            
            Return dt            
            conn.Close()
        End Using
    End Function

Open in new window


Than call and set values in code-behind of the page like this:
Dim MyClass As New MyClass
MyClass.AdminCourses(sCourseId)
'' Use Properties of MyClass instead of the class memebers directly
CourseTitleTextBox.Text = MyClass .CourseTitle
CourseLevelTextBox.Text = MyClass .CourseLevel
PreReqTextBox.Text = MyClass .Prerequisites

Open in new window


0
 

Author Comment

by:iNetBlazer
Comment Utility
Thank you... Your suggestion worked... I had a feeling it was something basic that I needed to do for it to work.  Your help is greatly appreciated.
0
 
LVL 9

Expert Comment

by:gery128
Comment Utility
glad to help :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now