Solved

How to Return Values from a Class to TextBoxes

Posted on 2011-09-19
5
345 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
ID: 36562288
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
ID: 36563030
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
ID: 36565109
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
ID: 36566586
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
ID: 36567054
glad to help :)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to seperate number and letters from a string 3 22
VB.NET Textbox input validation 4 41
How to use NFS (Network File System) in Asp.net mvc 5? 4 45
Code enhancement 4 22
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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