locked
3 tier logic problem RRS feed

  • Question

  • User-556137015 posted

    Hi guys,

    I have the following problem, I am trying to do a simple 3 tier application to grasp the logic. I have a data access layer and a business logic layer, and my presentation layer just has 1 dropdownlist.

    I want the dropdownlist to load and when I click on a item, it must return the ides, Now i know how to do that (selected_index_changed). I can not seem to load information to my dropdownlist. I am using  a simple stored procedure (select * from test). I will paste the code from the different layers and also the error. How can I solve this error.

    Data Access Layer :-

    Imports System
    Imports System.Collections.Generic
    Imports System.Data
    Imports System.Linq
    Imports System.Text
    Imports System.Data.SqlClient
    Imports System.Configuration
    
    Public Class csSQLDALVB
        Protected _strconn As String = ConfigurationManager.ConnectionStrings("MyConn").ConnectionString
        Private conn As SqlConnection
        Private errorstr As String = String.Empty
    
        'default method
        Public Sub New()
            conn = New SqlConnection()
        End Sub
    
        'gets and sets the connection string
        Public Property ConnectionString() As String
            Get
                Return _strconn
            End Get
            Set(ByVal value As String)
                _strconn = value
            End Set
        End Property
    
        'this is to get a open open connection to our database
        Private Function Open_Connection() As Boolean
            If conn.State = System.Data.ConnectionState.Closed Then
                Try
                    conn.ConnectionString = ConnectionString
                    conn.Open()
                    Return True
                Catch a As Exception
                    errorstr += " " & a.Message
                    Return False
                End Try
            Else
                Return True
            End If
        End Function
    
        'this is for our parameters for our queries
        Private Function add_parameters(ByVal objpar As csParameterListType) As SqlParameter
            Dim sqlpar As New SqlParameter()
    
            sqlpar.ParameterName = objpar.Name
            sqlpar.SqlDbType = objpar.SqlType
            sqlpar.SqlValue = objpar.Value
    
            Return sqlpar
        End Function
    
        'This can loop and read through all the records from the table
        Public Function executespreturndr(ByVal spname As String, ByVal objlist As List(Of csParameterListType)) As IDataReader
            Dim cmd As New SqlCommand()
            Dim dr As IDataReader = Nothing
            Try
                If Open_Connection() Then
                    cmd.Connection = conn
                    cmd.CommandType = System.Data.CommandType.StoredProcedure
                    cmd.CommandText = spname
    
                    For Each par As csParameterListType In objlist
                        cmd.Parameters.Add(add_parameters(par))
                    Next
    
                    dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                End If
                Return dr
            Catch e As Exception
                errorstr += " " & e.Message
                Return Nothing
            End Try
        End Function
    
        'this can be used for just executing a stored procedure for example "GET 10 BEST SELLING"
        Public Function executespreturndr(ByVal spname As String) As IDataReader
            Dim cmd As New SqlCommand()
            Dim dr As IDataReader = Nothing
    
            Try
                If Open_Connection() Then
                    cmd.Connection = conn
                    cmd.CommandType = System.Data.CommandType.StoredProcedure
                    cmd.CommandText = spname
                    dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                End If
                Return dr
            Catch e As Exception
                errorstr += " " & e.Message
                Return Nothing
            End Try
        End Function
    
        'this can insert into the database
        Public Sub executespreturnnd(ByVal spname As String, ByVal objlist As List(Of csParameterListType))
            Dim cmd As New SqlCommand()
            Try
                If Open_Connection() Then
                    cmd.Connection = conn
                    cmd.CommandType = System.Data.CommandType.StoredProcedure
                    cmd.CommandText = spname
    
                    For Each par As csParameterListType In objlist
                        cmd.Parameters.Add(add_parameters(par))
                    Next
    
                    cmd.ExecuteNonQuery()
    
                    close_conn()
    
                End If
            Catch e As Exception
                errorstr += " " & e.Message
            End Try
        End Sub
    
        'this inserts the record and returns the ID of the inserted record
        Public Function executespreturnID(ByVal spname As String, ByVal objlist As List(Of csParameterListType))
            Dim cmd As New SqlCommand()
            Dim ID As Integer
    
            Try
                If Open_Connection() Then
                    cmd.Connection = conn
                    cmd.CommandType = System.Data.CommandType.StoredProcedure
                    cmd.CommandText = spname
    
                    For Each par As csParameterListType In objlist
                        cmd.Parameters.Add(add_parameters(par))
                    Next
    
                    ID = cmd.ExecuteScalar()
    
                    close_conn()
    
                End If
            Catch e As Exception
                errorstr += " " & e.Message
            End Try
    
            Return ID
    
        End Function
    
        'this closes the db connection
        Private Sub close_conn()
            If conn.State = System.Data.ConnectionState.Open Then
                conn.Close()
            End If
    
            conn = Nothing
        End Sub
    End Class
    


     

    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Data
    
    Public Class csParameterListType
        Private _Name As String
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
        Private _SqlType As SqlDbType
        Public Property SqlType() As SqlDbType
            Get
                Return _SqlType
            End Get
            Set(ByVal value As SqlDbType)
                _SqlType = value
            End Set
        End Property
        Private _Value As String
        Public Property Value() As String
            Get
                Return _Value
            End Get
            Set(ByVal value As String)
                _Value = value
            End Set
        End Property
    
        Public Sub New(ByVal name__1 As String, ByVal Stype As SqlDbType, ByVal value__2 As String)
            Name = name__1
            SqlType = Stype
            Value = value__2
        End Sub
    End Class


     

    Business Logic Layer

    Imports DataAccessLayer
    
    Public Class DropDownList
    
        Private _IDn As Integer
        Private _Name, _Surname As String
    
        Public Sub New()
    
        End Sub
    
        Public Sub New(ByVal N As String, ByVal S As String, ByVal IDn As Integer)
            propName = N
            propSurname = S
            propID = IDn
        End Sub
    
        Public Property propName() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
    
        Public Property propSurname() As String
            Get
                Return _Surname
            End Get
            Set(ByVal value As String)
                _Surname = value
            End Set
        End Property
    
        Public Property propID() As Integer
            Get
                Return _IDn
            End Get
            Set(ByVal value As Integer)
                _IDn = value
            End Set
        End Property
    
        Public Function Get_All_Names() As List(Of DropDownList)
            Dim objDal As New csSQLDALVB
            Dim objListpb As New List(Of DropDownList)
            Dim dr As System.Data.IDataReader
    
            dr = objDal.executespreturndr("sp_get_all")
            Using dr
                While dr.Read
                    Dim objpb As New DropDownList
                    Populate_data(dr, objpb)
                    objListpb.Add(objpb)
                End While
            End Using
            Return objListpb
        End Function
    
        Private Sub Populate_data(ByVal objread As System.Data.IDataReader, ByVal objt As DropDownList)
            objt.propID = objread.GetInt32(0)
            objt.propName = objread.GetString(1)
            objt.propSurname = objread.GetString(2)
        End Sub
    
    End Class
    


     

    Presentation Layer

    Imports BusinessLogic
    
    Partial Public Class DropDownList
        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
                LoadInfo()
            End If
    
        End Sub
    
        Public Sub LoadInfo()
    
            Dim obj As New BusinessLogic.DropDownList
    
            DropDownList1.DataSource = obj.Get_All_Names
            DropDownList1.DataTextField = obj.propName
            DropDownList1.DataValueField = obj.propID
            DropDownList1.DataBind()
    
    
        End Sub
    
    
    End Class

    Error

     

    Server Error in '/' Application.

    DataBinding: 'BusinessLogic.DropDownList' does not contain a property with the name '0'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: DataBinding: 'BusinessLogic.DropDownList' does not contain a property with the name '0'.

    Source Error:

    Line 19:         DropDownList1.DataTextField = obj.propName
    Line 20:         DropDownList1.DataValueField = obj.propID
    Line 21:         DropDownList1.DataBind()
    Line 22: 
    Line 23: 

    Source File: C:\Users\KRATOS\Documents\Visual Studio 2008\Projects\Example\PresentationLayer\DropDownList.aspx.vb    Line: 21

    Stack Trace:

    [HttpException (0x80004005): DataBinding: 'BusinessLogic.DropDownList' does not contain a property with the name '0'.]
       System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) +8661733
       System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName, String format) +8
       System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +381
       System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +107
       System.Web.UI.WebControls.ListControl.PerformSelect() +34
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
       PresentationLayer.DropDownList.LoadInfo() in C:\Users\KRATOS\Documents\Visual Studio 2008\Projects\Example\PresentationLayer\DropDownList.aspx.vb:21
       PresentationLayer.DropDownList.Page_Load(Object sender, EventArgs e) in C:\Users\KRATOS\Documents\Visual Studio 2008\Projects\Example\PresentationLayer\DropDownList.aspx.vb:9
       System.Web.UI.Control.OnLoad(EventArgs e) +99
       System.Web.UI.Control.LoadRecursive() +50
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
    


    Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927

    <!-- [HttpException]: DataBinding: 'BusinessLogic.DropDownList' does not contain a property with the name '0'. at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName, String format) at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) at System.Web.UI.WebControls.ListControl.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at PresentationLayer.DropDownList.LoadInfo() in C:\Users\KRATOS\Documents\Visual Studio 2008\Projects\Example\PresentationLayer\DropDownList.aspx.vb:line 21 at PresentationLayer.DropDownList.Page_Load(Object sender, EventArgs e) in C:\Users\KRATOS\Documents\Visual Studio 2008\Projects\Example\PresentationLayer\DropDownList.aspx.vb:line 9 at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) [HttpUnhandledException]: Exception of type 'System.Web.HttpUnhandledException' was thrown. at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.dropdownlist_aspx.ProcessRequest(HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) -->

     

    Wednesday, May 19, 2010 4:33 PM

Answers

  • User-311685349 posted

    In  your presentation layer, change

    DropDownList1.DataTextField = obj.propName
    DropDownList1.DataValueField = obj.propID   

    to

    DropDownList1.DataTextField = "propName"
    DropDownList1.DataValueField = "propID"

    It's thinking the field values are the field names

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 19, 2010 9:09 PM

All replies

  • User-311685349 posted

    In  your presentation layer, change

    DropDownList1.DataTextField = obj.propName
    DropDownList1.DataValueField = obj.propID   

    to

    DropDownList1.DataTextField = "propName"
    DropDownList1.DataValueField = "propID"

    It's thinking the field values are the field names

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 19, 2010 9:09 PM
  • User-556137015 posted

    Hi

    I have done what you suggested and it fixed my problem,

    thank you very much

    Thursday, May 20, 2010 4:07 AM