Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9273643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:09:45+00:00 2026-06-18T16:09:45+00:00

I am trying to make a glossary. I have a form with a listbox,

  • 0

I am trying to make a glossary. I have a form with a listbox, 2 textboxes, and a save button.

The listbox is now populated with words from the database, and when a word is selected, its definition will display in textbox2.

The user can add a record by filling the textbox1 with a new word and textbox2 with its definition,and clicking the save button. If the new word already existed it will not allow to save a new record, also if there’s a null value between the 2 textboxes. If it doesn’t exist it will be inserted on the table and the new word will be added to the listbox.

The user can also update the record by selecting first a word on the list then edit the word and/or definition and clicking the save button.

I already got the updating part to work but I have problem in inserting a new record. I can’t do it properly. The glossary table has only 2 fields: word, definition. Here’s my code:

Dim myCmd As New MySqlCommand
Dim myReader As MySqlDataReader
Dim myAdptr As New MySqlDataAdapter
Dim myDataTable As New DataTable
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Call Connect()
    With Me
        If Blank() = False Then
            If Duplicate() = False Then
                STRSQL = "insert into glossary values (@word, @def)"
                myCmd.Connection = myConn
                myCmd.CommandText = STRSQL
                myCmd.Parameters.AddWithValue("word", txtNew.Text)
                myCmd.Parameters.AddWithValue("def", txtdefine.Text)
                myCmd.ExecuteNonQuery()
                myCmd.Dispose()
                MsgBox("Record Added")
                Dim word As String
                word = txtNew.Text
                lstword.Items.Add(word)
                'myConn.Close()
                'Me.FillListbox()
            Else
                myConn.Open()
                STRSQL = "Update glossary set word = @term, definition = @mean where word = @term"
                myCmd.Connection = myConn
                myCmd.CommandText = STRSQL
                myCmd.Parameters.AddWithValue("term", txtNew.Text)
                myCmd.Parameters.AddWithValue("mean", txtdefine.Text)
                myCmd.ExecuteNonQuery()
                myCmd.Dispose()
                MsgBox("Record Updated", MsgBoxStyle.Information, "New word added")
            End If
        End If
    End With


End Sub

Public Function Blank() As Boolean
    Call Connect()
    With Me
        If .txtNew.Text = "" Or .txtdefine.Text = "" Then
            Blank = True
            MsgBox("Cannot save! Term and definition should not contain null value", MsgBoxStyle.Critical, "Unable to save")
        Else
            Blank = False
        End If
    End With
End Function

Public Function Duplicate() As Boolean
    Call Connect()
    With Me
        STRSQL = "Select * from glossary where word = '" & txtNew.Text & "'"
        myCmd.Connection = myConn
        myCmd.CommandText = STRSQL
        If myDataTable.Rows.Count <> 0 Then
            Duplicate = True
            'MsgBox("Word already exist. Please check the word.", MsgBoxStyle.Critical, "Duplicate.")

        Else
            Duplicate = False
        End If
        myConn.Close()
    End With
End Function

this is my connection module:

Public myConnectionString As String
Public STRSQL As String
Public myConn As New MySqlConnection
Public Sub Connect()
    With myConn
        Try
            If .State = ConnectionState.Open Then
                .Close()
            End If
            myConnectionString = "Database=firstaidcqs;Server=localhost;Uid=root;Password="
            .ConnectionString = myConnectionString
            .Open()
            'MsgBox("Successful Connection")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Connection Error")
            .Close()
        End Try
    End With
End Sub
Public Sub Disconnect()
    With myConn
        .Close()
        .Dispose()
    End With
End Sub

How can I make this work properly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:09:46+00:00Added an answer on June 18, 2026 at 4:09 pm

    You are using global variables in all the code above.

    myConn and myCmd 
    

    in particular, you call Dispose on myCmd but I can’t see anywhere the reinitialization of the object with New. Also, forgetting for a moment the myCmd.Dispose problem, you don’t reset the myCmd parameters collection. In this way you end up with a wrong Parameter collection for the command executed, Also don’t forget to open the connection for the insert part. (and close it for both parts)

    You could easily avoid the use of unnecessary global variables

    ....
    If Duplicate() = False Then
        STRSQL = "insert into glossary values (@word, @def)"
        Using myCmd = new MySqlCommand(STRSQL, myConn)
            myConn.Open()
            myCmd.Parameters.AddWithValue("word", txtNew.Text)
            myCmd.Parameters.AddWithValue("def", txtdefine.Text)
            myCmd.ExecuteNonQuery()
        End Using
        myConn.Close()
    .....
    Else
        STRSQL = "Update glossary set word = @term, definition = @mean where word = @term"
        Using myCmd = new MySqlCommand(STRSQL, myConn)
            myConn.Open()
            myCmd.Parameters.AddWithValue("term", txtNew.Text)
            myCmd.Parameters.AddWithValue("mean", txtdefine.Text)
            myCmd.ExecuteNonQuery()
        End Using
        myConn.Close()
    .....
    

    A better solution will be changing the Connect() method to return the initialized connection instead of using a global variable. In that way you could enclose also the creation and destruction of the connection in a Using statement

     Using myConn as MySqlConnection = Connect()
     .......
     End Using
    

    For this to work you need to change the code of Connect in this way

    Public Function Connect() as MySqlConnection
        Dim myConn As MySqlConnection
        myConnectionString = "Database=firstaidcqs;Server=localhost;Uid=root;Password="
        myConn = New MySqlConnection(myConnectionString)
        myConn.Open()
        return myConn
    End Sub
    

    No need of a Disconnect function because you will always use the Using statement that will close the connection for you, no need to have a global variable to keep the connection because you will reopen the connection every time you need it and close afterward. Don’t think that this is not performant because ADO.NET implements connection pooling (It is an MSDN article for SqlServer, but the concept applies also to MySql)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I'm trying make some browser based game. But I have little questions.
Trying to make the infamous checkall checkbox for dynamically created rows from a MySQL
Trying to make a cross protocol, same domain request from https to http. I
im trying make one replace in string from a array but this dont work
I am trying make a basic database with the SQLliteDatabase but It is keep
I'm trying make my first python app. I want make simple email sender form.
I'm trying to make a change to an old appengine application, but now after
Trying to make my own contact form with php. Is there a better/cleaner way
Trying to make simple jQuery function to create a scrollToTop button that fades in
Trying to make a table in HAML, and I have the following: %table %tbody

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.