Wrox Programmer Forums
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 3rd, 2005, 10:07 PM
Registered User
Join Date: Apr 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Making DLLs in VB.NET

Hi, everyone. I am beginning to learn how to create Business Objects (in the form of VB.NET Dlls) and I am having a terrible time trying to debug this one. Everything looks correct, but when I run the ASP.NET page to use the DLL, it always returns Nothing.

Can someone look at my code and give me some guidance? I would really appreciate it. Thanks

Logan Scott
Here is the VB.NET code portion:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Namespace SQLDb

    Public Class Database

        Public objCS As String
        public objConn As SqlConnection
        Public objCmd As SqlCommand

        Public Function SelectSQL(ByVal strSQL As String) As SqlDataReader

                objConn = New SqlConnection(objCS)
                objCmd = New SqlCommand(strSQL, objConn)
                Return objCmd.ExecuteReader

            Catch ex As Exception
                Return Nothing
            End Try
        End Function

    End Class

End Namespace

You can compile the VB.NET code with this:
vbc /t:library /out..\bin\SQLDb.dll /r:System.dll /r:System.Data.dll Database.vb

Here is the ASP.NET code:

<%@ Page Language="VB" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SQLClient" %>
<%@ import Namespace="SQLDb" %>
<script runat="server">

    ' Insert page code here

    Sub btnSQL_Click(sender As Object, e As EventArgs)
        Dim objDatabase as new SQLDb.Database
        Dim objCmd As New SqlCommand(strSQL, objConn)
        Dim objReader as SQLDataReader = objCmd.ExecuteReader
        objDatabase.SQLConnString = "Data Source=(LOGANLAPTOP);" & _
                            "Initial Catalog=Northwind;" & _
                            "Integrated Security=SSPI"
        objReader = objDatabase.SelectSQL("SELECT * FROM Employees")
        If Not objReader Is Nothing Then
            dgSQLData.DataSource = objReader

            txtMessage.Text = "objReader = Nothing" & vbcrlf
            txtMessage.Text = txtMessage.Text & "objDataBase.SQLConnString = " & _
        End if
    End Sub

    <form runat="server">
            <asp:TextBox id="txtMessage" runat="server" Width="561px" Height="134px" TextMode="MultiLine"></asp:TextBox>
            <asp:Button id="btnSQL" onclick="btnSQL_Click" runat="server" Width="84px" Text="Test SQL"></asp:Button>
            <asp:DataGrid id="dgSqlData" runat="server" Width="100%" BorderColor="Black" CellPadding="4" Font-Names="Arial" Font-Size="8pt">
                <AlternatingItemStyle backcolor="#CCCCCC"></AlternatingItemStyle>
                <ItemStyle backcolor="White"></ItemStyle>
                <HeaderStyle backcolor="#CCCC99"></HeaderStyle>
Old April 9th, 2005, 04:14 PM
Authorized User
Join Date: Jul 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Mayur52o

i think you are missing the constructor buddy

Public Sub New()

Initilizes code here
End Sub
Old April 10th, 2005, 04:40 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

The problem here is that your consumed method in the business logic assembly is closing the reader/connection before the method exits (which you should). But because a datareader is a "connected" object, as soon as you close it (or it's connection) the object gets destroyed.

A better method for what you are trying to do is to return a DataSet or DataTable object. This will provide you with disconnected data. Do all your cleanup in the business object as you normally would, but instead hand back an object that's not dependant on anything.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a COM object in VB.NET Cohadar Pro VB.NET 2002/2003 0 March 30th, 2006 06:49 AM
Using DLLs in VB 6 gjc VB How-To 2 September 7th, 2005 03:47 AM
Strong name to msinet wrapper dlls vb.net rameshu007 Classic ASP XML 0 July 13th, 2004 06:07 AM
Using VB 6.0 DLLs in .Net ECrawford ASP.NET 1.0 and 1.1 Basics 4 June 29th, 2004 07:25 AM
Help with using DLLs in VB vb_programmer Beginning VB 6 2 September 23rd, 2003 05:39 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.