Click here to Skip to main content
15,903,175 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: System.Drawing.Bitmap.save does not save image Pin
DaveAuld5-Dec-10 9:48
professionalDaveAuld5-Dec-10 9:48 
GeneralRe: System.Drawing.Bitmap.save does not save image Pin
pbsjr5-Dec-10 9:51
pbsjr5-Dec-10 9:51 
AnswerRe: System.Drawing.Bitmap.save does not save image Pin
Luc Pattyn5-Dec-10 7:56
sitebuilderLuc Pattyn5-Dec-10 7:56 
GeneralRe: System.Drawing.Bitmap.save does not save image Pin
pbsjr5-Dec-10 9:44
pbsjr5-Dec-10 9:44 
AnswerRe: System.Drawing.Bitmap.save does not save image Pin
Luc Pattyn5-Dec-10 10:08
sitebuilderLuc Pattyn5-Dec-10 10:08 
GeneralRe: System.Drawing.Bitmap.save does not save image Pin
pbsjr5-Dec-10 10:59
pbsjr5-Dec-10 10:59 
GeneralRe: System.Drawing.Bitmap.save does not save image Pin
Luc Pattyn5-Dec-10 11:08
sitebuilderLuc Pattyn5-Dec-10 11:08 
Question"Cannot open any more tables" happens when I optimize my code [modified] Pin
i_kant_spel4-Dec-10 19:04
i_kant_spel4-Dec-10 19:04 
I'm trying to make a button that will search through a database and return a list of names of all the records matching a regular expression, plus data from a table if it exists. My first version looks like this:

Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
    namesList.Items.Clear()

    Me.MainTableAdapter.Connection.Open()

    Dim myCommand As OleDbCommand = Me.MainTableAdapter.Connection.CreateCommand()
    Me.MainTableAdapter.Transaction = Me.MainTableAdapter.Connection.BeginTransaction()
    myCommand.Connection = Me.MainTableAdapter.Connection
    myCommand.Transaction = Me.MainTableAdapter.Transaction

    myCommand.CommandText = "Select * from main WHERE Title Like '%" & nameTB.Text & "%' order by title asc, ID asc"
    Dim dbReader As OleDbDataReader = myCommand.ExecuteReader

    While dbReader.Read()
        Dim title As String = dbReader("Title")
        Dim ID As Integer = dbReader("ID")
        Dim qualifier As String = getQualifier(ID, Me.QualifiersTableAdapter)
        If Not qualifier Is Nothing Then
            title &= "- " & qualifier &
        End If
        namesList.Items.Add(title)
    End While
    Me.MainTableAdapter.Connection.Close()

End Sub

Private Function getQualifier(ByVal TitleID As Integer, ByVal adapter As QualifiersTableAdapter) As String
    adapter.Connection.Open()
    Dim myCommand As OleDbCommand = adapter.Connection.CreateCommand()
    adapter.Transaction = adapter.Connection.BeginTransaction
    myCommand.Connection = adapter.Connection
    myCommand.Transaction = adapter.Transaction

    myCommand.CommandText = "Select * from Qualifiers WHERE Title = " & TitleID
    Dim dbReader As OleDbDataReader = myCommand.ExecuteReader
    myCommand.Transaction.Commit()

    Dim s as String = Nothing
    If dbReader.Read() Then
        s = dbReader("Qualifier")
    End If
    adapter.Connection.Close()
    Return s


End Function


This works, but it's slow if the regular expression has lots of matches. So I revised it, moving the opening and closing of the table adapter to the button click function, so that they only get performed once each time you click the search button, instead of once for every record in the DB.

(modified since original post, problem still occurs)
Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        namesList.Items.Clear()

        Dim dBaseConnectionString As String = "Provider=Microsoft.jet.oledb.4.0;" & _
        "Data Source=demo.mdb" & ";"

        Dim dBaseConnection As New OleDbConnection(dBaseConnectionString)
        dBaseConnection.Open()

        Dim dBaseCommand As New OleDbCommand("Select * from main WHERE Title Like '%" & titleTB.Text & "%' order by title asc, ID asc", dBaseConnection)
        Dim dBaseDataReader As OleDb.OleDbDataReader = dBaseCommand.ExecuteReader()

        While dBaseDataReader.Read()
            Dim title As String = dbReader("Title")
            Dim ID As Integer = dbReader("ID")
            Dim qualifier As String = getQualifier(ID, dBaseConnection)
            If Not qualifier Is Nothing Then
                title &= "- " & qualifier &
            End If
            namesList.Items.Add(title)
        End While
        Me.MainTableAdapter.Connection.Close()

    End Sub

    Private Function getQualifier(ByVal TitleID As Integer, ByVal dBaseConnection As OleDbConnection) As String


        Dim dBaseCommand As New OleDbCommand("Select * from Qualifiers WHERE Title = " & TitleID, dBaseConnection)
        Dim dBaseDataReader As OleDb.OleDbDataReader = dBaseCommand.ExecuteReader()

        If dbReader.Read() Then
            Return dbReader("Qualifier")
        Else
            Return Nothing
        End If

    End Function


This is much faster. But I just realized something - if I use the search button enough times, I get an unhandled exception at myCommand.ExecuteReader, "Cannot open any more tables." It works fine once, even if I leave the regex blank and return everything in the database. With the old approach, I can hit the search button as many times as I want without seeing this exception.

So, what am I doing wrong here? I will admit I'm not totally comfortable with oledb and am only dimly aware of what my code is actually doing, so if you have any miscellaneous tips on how to use it better, I will listen.

Thanks.

modified on Sunday, December 5, 2010 2:50 PM

AnswerRe: "Cannot open any more tables" happens when I optimize my code [modified] Pin
Luc Pattyn4-Dec-10 23:27
sitebuilderLuc Pattyn4-Dec-10 23:27 
GeneralRe: "Cannot open any more tables" happens when I optimize my code Pin
i_kant_spel5-Dec-10 7:58
i_kant_spel5-Dec-10 7:58 
AnswerRe: "Cannot open any more tables" happens when I optimize my code Pin
Luc Pattyn5-Dec-10 8:16
sitebuilderLuc Pattyn5-Dec-10 8:16 
GeneralRe: "Cannot open any more tables" happens when I optimize my code Pin
i_kant_spel5-Dec-10 9:06
i_kant_spel5-Dec-10 9:06 
AnswerRe: "Cannot open any more tables" happens when I optimize my code Pin
Luc Pattyn5-Dec-10 9:52
sitebuilderLuc Pattyn5-Dec-10 9:52 
GeneralRe: "Cannot open any more tables" happens when I optimize my code Pin
i_kant_spel5-Dec-10 10:59
i_kant_spel5-Dec-10 10:59 
GeneralRe: "Cannot open any more tables" happens when I optimize my code Pin
Luc Pattyn5-Dec-10 11:04
sitebuilderLuc Pattyn5-Dec-10 11:04 
QuestionHow to use Insert Command for Varchar(Max) Pin
Paramu19734-Dec-10 3:34
Paramu19734-Dec-10 3:34 
AnswerRe: How to use Insert Command for Varchar(Max) Pin
Luc Pattyn4-Dec-10 3:45
sitebuilderLuc Pattyn4-Dec-10 3:45 
QuestionVB6 ActiveX program get invoked from Source folder not program files. Pin
QuickBooksDev4-Dec-10 3:29
QuickBooksDev4-Dec-10 3:29 
QuestionType conversion? Pin
waner michaud1-Dec-10 8:10
waner michaud1-Dec-10 8:10 
AnswerRe: Type conversion? Pin
Dave Kreskowiak1-Dec-10 8:54
mveDave Kreskowiak1-Dec-10 8:54 
GeneralRe: Type conversion? Pin
waner michaud1-Dec-10 9:36
waner michaud1-Dec-10 9:36 
GeneralRe: Type conversion? Pin
Dave Kreskowiak1-Dec-10 14:08
mveDave Kreskowiak1-Dec-10 14:08 
GeneralRe: Type conversion? Pin
phil.o2-Dec-10 22:46
professionalphil.o2-Dec-10 22:46 
QuestionDoes AnyBody Faced This Problem? While sending E-Mails to Multiple clients... Pin
Paramu19731-Dec-10 1:00
Paramu19731-Dec-10 1:00 
AnswerRe: Does AnyBody Faced This Problem? While sending E-Mails to Multiple clients... Pin
Luc Pattyn1-Dec-10 2:36
sitebuilderLuc Pattyn1-Dec-10 2:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.