Solved

How to Return Values from a Class to TextBoxes

Posted on 2011-09-19
5
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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