Results 1 to 3 of 3

Thread: Advanced problem with function handling

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99

    Advanced problem with function handling

    Hello everybody,


    I just wrote a datalayer with help from a wrox book. This layer works fine but I have a question about it.

    This is my data layer :

    VB Code:
    1. Public Overloads Function ExecuteSql(ByVal SProcName As String, _
    2.                                         ByRef myDataReader As SqlDataReader) As Boolean
    3.  
    4.         ' Create a new command object
    5.         Dim myCommand As SqlCommand = CreateNoParamCommand(SProcName, CommandType.Text)
    6.  
    7.         ' Fill the DataReader object
    8.         Try
    9.             myDataReader = myCommand.ExecuteReader
    10.             System.Diagnostics.EventLog.WriteEntry(MODULE_NAME, "hi", Diagnostics.EventLogEntryType.Error)
    11.             Return True
    12.         Catch e As Exception
    13.             LogError(e)
    14.             Return False
    15.         End Try
    16.  
    17.     End Function

    As you can see, I made a logging in the event log to see each time a dbase connection has been made.

    I wrote following function to make a separate layer. This function returns me the datalayer.

    VB Code:
    1. Private Function LoadGridje() As SqlDataReader
    2.  
    3.         Dim strSQL As String
    4.         Dim dr As SqlDataReader
    5.  
    6.         Dim DbAccess As New DataAccess()
    7.  
    8.         strSQL = "SELECT DISTINCT Afdeling FROM tblWerkposten"
    9.  
    10.         'Response.Write(DbAccess.ExecuteSql(strSQL, dr))
    11.         Try
    12.             If DbAccess.ExecuteSql(strSQL, dr) = True Then
    13.                 Return dr
    14.             Else
    15.                 Return Nothing
    16.             End If
    17.         Catch
    18.             Return Nothing
    19.         End Try
    20.  
    21.     End Function


    Right now I want to bind a combobox on my function. All this works perfect, except when I call the DataValueField, which needs to give me the values of the datareader, then my connection has been made twice (I can see this in my event log)

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.         With cboAfdeling
    4.             .DataSource = LoadGridje()
    5.             .DataValueField = LoadGridje.GetName(0)
    6.             .DataBind()
    7.         End With
    8.  
    9.     End Sub

    I hope someone knows how I can work around this cause it's a pretty though one and I adore performance

    Thanks in advance !

    Bjorn

    ps : if it isn't clear enough, please return a message !

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    set an object to the return value of the function first, then us that for your properties

    Dim blah As DataReader

    blah = loadGridje
    .DataSource = blah
    .DataValueField = blah.GetName(0)




    see if that works.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Well, not sure why your code is opening a connection twice, i actually don't see where it opens it once but here's how's I'd bind a combobox:

    DataAccess:
    VB Code:
    1. Public Class DataAccess : Implements IDisposable
    2.  
    3.     Private m_cn As SqlConnection
    4.     Private m_connString As String = AppSettings.Item("DBConnString")
    5.  
    6.     Public Sub New()
    7.         m_cn = New SqlConnection(m_connString)
    8.     End Sub
    9.  
    10.     Public Overloads Function ExecuteSqlDataReader(ByVal cmdText As String, _
    11.                                                    ByVal cmdType As CommandType) As SqlDataReader
    12.         Dim cmd As New SqlCommand(cmdText)
    13.         cmd.CommandType = cmdType
    14.         Return ExecuteSqlDataReader(cmd)
    15.     End Function
    16.  
    17.     Public Overloads Function ExecuteSqlDataReader(ByVal cmd As SqlCommand) As SqlDataReader
    18.         cmd.Connection = m_cn
    19.         cmd.Connection.Open()
    20.         Return cmd.ExecuteReader()
    21.     End Function
    22.  
    23.     Public Overloads Sub Dispose() Implements IDisposable.Dispose
    24.         If Not m_cn Is Nothing Then
    25.             If m_cn.State <> ConnectionState.Closed Then m_cn.Close()
    26.             m_cn.Dispose()
    27.         End If
    28.     End Sub
    29.  
    30. End Class

    Then, assuming theres an asp:DropDownList with the ID of "Authors" on the aspx page, the code behind would look like this:
    VB Code:
    1. Protected Authors As DropDownList
    2. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.     If Not Page.IsPostBack Then
    4.         loadComboBox()
    5.     End If
    6. End Sub
    7.  
    8. Private Sub loadComboBox()
    9.     Dim cmdText As String = "Select au_id, au_lname From Authors"
    10.     Dim da As New DataAccess()
    11.     Try
    12.         Authors.DataSource = da.ExecuteSqlDataReader(cmdText, CommandType.Text)
    13.         Authors.DataValueField = "au_id"
    14.         Authors.DataTextField = "au_lname"
    15.         Authors.DataBind()
    16.     Finally
    17.         da.Dispose()
    18.     End Try
    19. End Sub

    There are probably a million different ways of doing this, just givin some options.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width