Solved

How to Return Values from a Class to TextBoxes

Posted on 2011-09-19
5
344 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

810 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