Results 1 to 7 of 7

Thread: [2005] Record Exist Problem?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    [2005] Record Exist Problem?

    The Following code i am using at the moment is to ADD (only) to a database.

    Code:
     
            Dim connection As New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = ..\db1.mdb")
            Dim command As New OleDbCommand("INSERT INTO Table1 (Barcode, Box, Description, Whos) VALUES (@Barcode, @Box, @Description,@Whos)", connection)
            command.Parameters.AddWithValue("@Barcode", txtBarcode.Text)
            command.Parameters.AddWithValue("@Box", txtBox.Text)
            command.Parameters.AddWithValue("@Description", rttDetail.Text)
            command.Parameters.AddWithValue("@Whos", txtWho.Text)
            connection.Open()
            command.ExecuteNonQuery()
            connection.Close()
    But what i need before it adds it to the database is too check the Barcode column to see if what is typed in the txtbarcode.text textbox matches anything in the barcode column in the DB. if it does a messagebox appears to tell them, if not carry on.

    any help is helpful

    thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Record Exist Problem?

    So you need to query the database to see if there are any existing records with matching values. Follow the Data Access link in my signature to see how to use ExecuteScalar to get a single value from a database. You query would be something like:
    SQL Code:
    1. SELECT COUNT(*) FROM SomeTable WHERE SomeColumn = @SomeColumn
    If the result is non-zero then the value already exists in the database.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Record Exist Problem?

    vb.net Code:
    1. dim BarCOdeQuery as string = "SELECT * FROM Table WHERE Barcode = @BarCode"
    2. dim connection as SqlConnection = new SalCOnnection(connectionString)
    3. dim command as new SqlCOmmand(BarCodeQuery, connection)
    4.  
    5. try
    6.    connection.open
    7.    dim result as object = command.executeScalar()
    8.    if not isNothing(result) then throw new Exception("Code bar already exists")
    9.    
    10.    'Save as you did before
    11. Catch ex as exception
    12.    messagebox.show(ex.tostring)
    13. Finally
    14.    connection.close
    15. end try

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Re: [2005] Record Exist Problem?

    Thanks For Your Help. I used this code to do it.

    Code:
            Dim con As New OleDb.OleDbConnection
            Dim ds As New DataSet
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = ..\db1.mdb"
            Dim command As New OleDbCommand("SELECT COUNT(Barcode) FROM Table1 WHERE Barcode = '" & txtBarcode.Text & "'", con)
            con.Open()
            Dim total As Double = CDbl(command.ExecuteScalar())
            con.Close()
            If total = "1" Then
                MessageBox.Show("Record Already Exists - [Barcode]", "Existing Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtBarcode.Text = ""
            End If
    This works perfectly, but only for the first column - Barcode.

    when i used this code to use with the box column

    Code:
            
    Dim con1 As New OleDb.OleDbConnection
            Dim ds1 As New DataSet
            con1.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = ..\db1.mdb"
            Dim command1 As New OleDbCommand("SELECT COUNT(Box) FROM Table1 WHERE Box = '" & txtBox.Text & "'", con1)
            con1.Open()
            Dim total1 As Double = CDbl(command1.ExecuteScalar())
            con1.Close()
            If total1 = "1" Then
                MessageBox.Show("Record Already Exists - [Box Number]", "Existing Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtBox.Text = ""
            End If
    it throws up an error:

    Data type mismatch in criteria expression.
    for this line of code:

    Code:
    Dim total1 As Double = CDbl(command1.ExecuteScalar())
    Is this because the .ExecueScalar command is only for the first column. and how do i resolve this.

    thank you

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Record Exist Problem?

    The problem is that the value you search for has to be the same type as the column you search in. For instance, you can't search a numeric column for "Hello World". What's the type of the column you're searching in? Is the parameter value you're passing compatible? Obviously the answer to the second question is NO.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Re: [2005] Record Exist Problem?

    The type of column is a "Number" column (1,2,3 etc.)

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Record Exist Problem?

    So... pass a number to search for and not text. Single quotes around a value mean it's text. That said, you should be using parameters anyway. Follow the Data Access link in my signature to see how.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width