function connect works; sub doesn't; weird

  • Thread starter Thread starter dennist
  • Start date Start date
D

dennist

I have a bizarre error I can't figure out. If I open a
connection in a subroutine, it returns as error: Here is
the subroutine.



Friend Sub NewTopic() 'As Boolean
'Return False
Dim cn As New OleDbConnection(gstrConn)
cn.Open()

Dim cmd As New OleDbCommand("SELECT * FROM
Topics", cn)
Dim da As OleDbDataAdapter = New OleDbDataAdapter
da.SelectCommand = cmd

' Fill the DataSet
Dim ds As DataSet = New DataSet
'dim tblTopics as ds
da.Fill(ds, "Topics")

'Return True
cn.Close()

End Sub



An unhandled exception of
type 'System.Data.OleDb.OleDbException' occurred in
system.data.dll

However, if I change it to a function, everything else
the same except for return values, it works fine:


Friend Function NewTopic() As Boolean
Return False
Dim cn As New OleDbConnection(gstrConn)
cn.Open()

Dim cmd As New OleDbCommand("SELECT * FROM
Topics", cn)
Dim da As OleDbDataAdapter = New OleDbDataAdapter
da.SelectCommand = cmd

' Fill the DataSet
Dim ds As DataSet = New DataSet
'dim tblTopics as ds
da.Fill(ds, "Topics")

Return True
cn.Close()

End Function

Now this isn't generally true in my program. I call an
overloaded subroutine to fill listboxes or comboboxes and
it works fine. Here is the form load event from which I
call the call the class subroutine.

Private Sub frmtopicfromstart_Load(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Dim clsDChor As New clsDataChor

txtTextGeneral.Text = ""

clsDChor.FillBox(cboTopicIssuers, "SELECT
TopicIssuers.ID, TopicIssuers.Issuer FROM TopicIssuers
ORDER BY TopicIssuers.Issuer;", "TopicIssuers")
clsDChor.FillBox(cboPublications, "SELECT
Publications.ID, Publications.Publication FROM
Publications ORDER BY
Publications.Publication;", "Publications")
clsDChor.FillBox(cboPOV, "SELECT PointsOfView.ID,
PointsOfView.PointOfView FROM PointsOfView ORDER BY
PointsOfView.PointOfView;", "PointsOfView")
clsDChor.FillBox(cboDateType, "SELECT
DateType.ID, DateType.DateType FROM DateType ORDER BY
DateType.DateType;", "DateType")

'Dim strDate As String = "05/22/2003"
'If IsDate(strDate) Then
' dtp1.Value = strDate
'End If


End Sub

and here is the overloaded subroutine, which works
fine:

Overloads Sub FillBox(ByRef lst As ListBox, ByVal
strSQL As String, ByVal tbl As String)


Dim cn As New OleDbConnection(gstrConn)
cn.Open()

Dim cmd As New OleDbCommand(strSQL, cn)
Dim da As OleDbDataAdapter = New OleDbDataAdapter
da.SelectCommand = cmd

' Fill the DataSet
Dim ds As DataSet = New DataSet
da.Fill(ds, tbl)

Dim tbl1 As DataTable = ds.Tables(0)
Dim row As DataRow = tbl1.Rows(0)


' Bind the DataSet to the DataList
lst.ValueMember = ds.Tables(tbl).Columns
(0).ColumnName
lst.DisplayMember = ds.Tables(tbl).Columns
(1).ColumnName
lst.DataSource = ds.Tables(tbl)

cn.Close()


End Sub

Overloads Sub FillBox(ByRef lst As ComboBox, ByVal
strSQL As String, ByVal tbl As String)


Dim cn As New OleDbConnection(gstrConn)
cn.Open()

Dim cmd As New OleDbCommand(strSQL, cn)
Dim da As OleDbDataAdapter = New OleDbDataAdapter
da.SelectCommand = cmd

' Fill the DataSet
Dim ds As DataSet = New DataSet
da.Fill(ds, tbl)


' Bind the DataSet to the DataList
lst.ValueMember = ds.Tables(tbl).Columns
(0).ColumnName
lst.DisplayMember = ds.Tables(tbl).Columns
(1).ColumnName
lst.DataSource = ds.Tables(tbl)

cn.Close()


End Sub

I've wasted a lot of time trying to figure this one
out. I hope one of you have an idea.

thanks.

dennist
 
Dennist:

Which line is throwing the exception. The Sub and Function aren't
identical, and in the function, the first line is return false. Is that a
typo? If you could psot the line that's throwing the exception, it'd be a
lot easier to diagnose.
 
William,

The function version works; it's the subroutine version
that is throwing the exception. So no line in the
function version is throwing the exception.

dennist
 
Hi Dennist,

I have some questions:

In which line of code in the frmtopicfromstart_Load(), the sub NewTopic()
is called?

What is the EXACT error message you have seen?

Debug into the NewTopic() step by step, which line of code causes the
problem?

Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------
| Content-Class: urn:content-classes:message
| From: "dennist" <[email protected]>
| Sender: "dennist" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: function connect works; sub doesn't; weird
| Date: Fri, 12 Sep 2003 01:48:21 -0700
| Lines: 191
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcN5Cp5ciF/s+N0nTV2UWO6BQzyizw==
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:60970
| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| William,
|
| The function version works; it's the subroutine version
| that is throwing the exception. So no line in the
| function version is throwing the exception.
|
| dennist
| >-----Original Message-----
| >Dennist:
| >
| >Which line is throwing the exception. The Sub and
| Function aren't
| >identical, and in the function, the first line is return
| false. Is that a
| >typo? If you could psot the line that's throwing the
| exception, it'd be a
| >lot easier to diagnose.
| >| >> I have a bizarre error I can't figure out. If I open a
| >> connection in a subroutine, it returns as error: Here
| is
| >> the subroutine.
| >>
| >>
| >>
| >> Friend Sub NewTopic() 'As Boolean
| >> 'Return False
| >> Dim cn As New OleDbConnection(gstrConn)
| >> cn.Open()
| >>
| >> Dim cmd As New OleDbCommand("SELECT * FROM
| >> Topics", cn)
| >> Dim da As OleDbDataAdapter = New
| OleDbDataAdapter
| >> da.SelectCommand = cmd
| >>
| >> ' Fill the DataSet
| >> Dim ds As DataSet = New DataSet
| >> 'dim tblTopics as ds
| >> da.Fill(ds, "Topics")
| >>
| >> 'Return True
| >> cn.Close()
| >>
| >> End Sub
| >>
| >>
| >>
| >> An unhandled exception of
| >> type 'System.Data.OleDb.OleDbException' occurred in
| >> system.data.dll
| >>
| >> However, if I change it to a function, everything else
| >> the same except for return values, it works fine:
| >>
| >>
| >> Friend Function NewTopic() As Boolean
| >> Return False
| >> Dim cn As New OleDbConnection(gstrConn)
| >> cn.Open()
| >>
| >> Dim cmd As New OleDbCommand("SELECT * FROM
| >> Topics", cn)
| >> Dim da As OleDbDataAdapter = New
| OleDbDataAdapter
| >> da.SelectCommand = cmd
| >>
| >> ' Fill the DataSet
| >> Dim ds As DataSet = New DataSet
| >> 'dim tblTopics as ds
| >> da.Fill(ds, "Topics")
| >>
| >> Return True
| >> cn.Close()
| >>
| >> End Function
| >>
| >> Now this isn't generally true in my program. I call an
| >> overloaded subroutine to fill listboxes or comboboxes
| and
| >> it works fine. Here is the form load event from which
| I
| >> call the call the class subroutine.
| >>
| >> Private Sub frmtopicfromstart_Load(ByVal sender
| >> As System.Object, ByVal e As System.EventArgs) Handles
| >> MyBase.Load
| >> Dim clsDChor As New clsDataChor
| >>
| >> txtTextGeneral.Text = ""
| >>
| >> clsDChor.FillBox(cboTopicIssuers, "SELECT
| >> TopicIssuers.ID, TopicIssuers.Issuer FROM TopicIssuers
| >> ORDER BY TopicIssuers.Issuer;", "TopicIssuers")
| >> clsDChor.FillBox(cboPublications, "SELECT
| >> Publications.ID, Publications.Publication FROM
| >> Publications ORDER BY
| >> Publications.Publication;", "Publications")
| >> clsDChor.FillBox(cboPOV, "SELECT
| PointsOfView.ID,
| >> PointsOfView.PointOfView FROM PointsOfView ORDER BY
| >> PointsOfView.PointOfView;", "PointsOfView")
| >> clsDChor.FillBox(cboDateType, "SELECT
| >> DateType.ID, DateType.DateType FROM DateType ORDER BY
| >> DateType.DateType;", "DateType")
| >>
| >> 'Dim strDate As String = "05/22/2003"
| >> 'If IsDate(strDate) Then
| >> ' dtp1.Value = strDate
| >> 'End If
| >>
| >>
| >> End Sub
| >>
| >> and here is the overloaded subroutine, which works
| >> fine:
| >>
| >> Overloads Sub FillBox(ByRef lst As ListBox,
| ByVal
| >> strSQL As String, ByVal tbl As String)
| >>
| >>
| >> Dim cn As New OleDbConnection(gstrConn)
| >> cn.Open()
| >>
| >> Dim cmd As New OleDbCommand(strSQL, cn)
| >> Dim da As OleDbDataAdapter = New
| OleDbDataAdapter
| >> da.SelectCommand = cmd
| >>
| >> ' Fill the DataSet
| >> Dim ds As DataSet = New DataSet
| >> da.Fill(ds, tbl)
| >>
| >> Dim tbl1 As DataTable = ds.Tables(0)
| >> Dim row As DataRow = tbl1.Rows(0)
| >>
| >>
| >> ' Bind the DataSet to the DataList
| >> lst.ValueMember = ds.Tables(tbl).Columns
| >> (0).ColumnName
| >> lst.DisplayMember = ds.Tables(tbl).Columns
| >> (1).ColumnName
| >> lst.DataSource = ds.Tables(tbl)
| >>
| >> cn.Close()
| >>
| >>
| >> End Sub
| >>
| >> Overloads Sub FillBox(ByRef lst As ComboBox, ByVal
| >> strSQL As String, ByVal tbl As String)
| >>
| >>
| >> Dim cn As New OleDbConnection(gstrConn)
| >> cn.Open()
| >>
| >> Dim cmd As New OleDbCommand(strSQL, cn)
| >> Dim da As OleDbDataAdapter = New
| OleDbDataAdapter
| >> da.SelectCommand = cmd
| >>
| >> ' Fill the DataSet
| >> Dim ds As DataSet = New DataSet
| >> da.Fill(ds, tbl)
| >>
| >>
| >> ' Bind the DataSet to the DataList
| >> lst.ValueMember = ds.Tables(tbl).Columns
| >> (0).ColumnName
| >> lst.DisplayMember = ds.Tables(tbl).Columns
| >> (1).ColumnName
| >> lst.DataSource = ds.Tables(tbl)
| >>
| >> cn.Close()
| >>
| >>
| >> End Sub
| >>
| >> I've wasted a lot of time trying to figure this one
| >> out. I hope one of you have an idea.
| >>
| >> thanks.
| >>
| >> dennist
| >>
| >
| >
| >.
| >
|
 
Kevin, thank you for the suggestions. It'll take a
little time but I'll follow through.

dennist
 
Back
Top