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

  • Home
  • SEARCH
  • 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 7049941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:04:57+00:00 2026-05-28T03:04:57+00:00

I want to insert records in the database but before it inserts, it must

  • 0

I want to insert records in the database but before it inserts, it must first check the database whether the value being inserted already exists. Now my problem is that I am unable to insert into the database even though the value still does not exist.

Here’s my code:

Dim check As New SqlCommand
    Dim sqlcheck As String = "SELECT SerialNumber FROM EquipmentDetail WHERE SerialNumber = '" & TextBox1.Text & "'"


    connection.Open()
    check.Connection = connection
    check.CommandText = sqlcheck

    Dim read As SqlDataReader = check.ExecuteReader()

    If read.HasRows Then

        While read.Read()

            If read("SerialNumber").ToString() = TextBox1.Text Then

                MessageBox.Show(read("SerialNumber").ToString() & " was already added")

            Else

                cmd.Connection = connection
                cmd.CommandText = "INSERT INTO EquipmentDetail (SerialNumber, BoxNumber, FATTNumber, FaultTicketNumber, Description, ProductCode)" &
                "VALUES('" & TextBox1.Text & "',  '" & TextBox2.Text & "',  '" & TextBox3.Text & "',  '" & TextBox4.Text & "', '" & TextBox6.Text & "',  '" & ComboBox1.Text & "')"
                cmd.ExecuteNonQuery()

                MessageBox.Show("Equipment successfully added.", "Equipment", MessageBoxButtons.OK)
                TextBox1.Text = ""
                TextBox2.Text = ""
                TextBox3.Text = ""
                TextBox4.Text = ""
                TextBox6.Text = ""
                TextBox1.Focus()
                connection.Close()

            End If
        End While
    End If
    read.Close()
  • 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-05-28T03:04:58+00:00Added an answer on May 28, 2026 at 3:04 am

    There are a number of issues with your code.

    First, you have an sql injection vulnerability which can be resolved using parameters.

    Second, you are performing database activities within an open datareader, which could lead to numerous issues.

    Third, you are performing far too much work to determine whether or not the serial number exists. You can make use of the SQL Server Exists statement and the SqlCommand’s ExecuteScalar method to return and test a single value, which makes the code much faster and easier to understand.

    Finally, you need to ensure that disposable items are disposed and the easiest/best way to do this is through the use of Using blocks.

    These can all be resolved with the following code:

        Using connection As New SqlConnection(connStr)
            Dim sqlcheck As String = "IF EXISTS(select 1 FROM EquipmentDetail WHERE SerialNumber=@SerialNumber) SELECT 1 ELSE SELECT 0"
            Using cmd As New SqlCommand(sqlcheck, connection)
                cmd.Parameters.AddWithValue("@SerialNumber", TextBox1.Text)
                connection.Open()
    
                If CBool(cmd.ExecuteScalar) Then
                    MessageBox.Show(TextBox1.Text & " was already added")
                Else
                    Using insertCmd As New SqlCommand
                        insertCmd.Connection = connection
    
                        insertCmd.CommandText = "INSERT INTO EquipmentDetail (SerialNumber, BoxNumber, FATTNumber, FaultTicketNumber, Description, ProductCode)" &
                        "VALUES(@SerialNumber, @BoxNumber, @FATTNumber, @FaultTicketNumber, @Description, @ProductCode)"
                        insertCmd.Parameters.AddWithValue("@SerialNumber", TextBox1.Text)
                        insertCmd.Parameters.AddWithValue("@BoxNumber", TextBox2.Text)
                        insertCmd.Parameters.AddWithValue("@FATTNumber", TextBox3.Text)
                        insertCmd.Parameters.AddWithValue("@FaultTicketNumber", TextBox4.Text)
                        insertCmd.Parameters.AddWithValue("@Description", TextBox6.Text)
                        insertCmd.Parameters.AddWithValue("@ProductCode", ComboBox1.Text)
    
                        insertCmd.ExecuteNonQuery()
    
                        MessageBox.Show("Equipment successfully added.", "Equipment", MessageBoxButtons.OK)
                        TextBox1.Text = ""
                        TextBox2.Text = ""
                        TextBox3.Text = ""
                        TextBox4.Text = ""
                        TextBox6.Text = ""
                        TextBox1.Focus()
                    End Using
    
                End If
            End Using
            connection.Close()
        End Using
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: insert contacts into database but does not want to duplicate already existing
I want to insert say 50,000 records into sql server database 2000 at a
I want to bulk insert about 700 records into the Android database on my
I want to insert something into a STL list in C++, but I only
I want to insert a new row into an Access database. I'm looking at
I want to insert a pair< string, vector<float> > into a map, first it
I want to insert the following as the value for a variable in some
I want to insert a row into the Database using SqlDataAdapter. I've 2 tables
I want to insert this margin, but I don't know why... I want it
I want to insert certain values from an existing database into another. I know

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.