3 Replies - 224 Views - Last Post: 02 December 2017 - 03:43 PM Rate Topic: -----

#1 Nigels   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 78
  • Joined: 17-May 13

Generating a serial and check database for duplicate

Posted 02 December 2017 - 02:36 PM

Hi, I am generating a serial then comparing that serial to a database to check for duplicates. I have included an image so you may see what it looks like. Basically I have 3 textbox's and when I press a button it generates a number in each box. I then string all 3 numbers out the textboxes together and compare it against a database to see if there is a duplicate. I simply keep pressing the button and generating different numbers until I get a MsgBox come up and say "Good to go"

The problem is I don't want to have to press a button 50 times until I get a serial that is not a duplicate. This is the button code that generates the number
 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Generate.Click
        TextBox1.Text = Int(Rnd() * 700)
        TextBox2.Text = Int(Rnd() * 600)
        TextBox3.Text = Int(Rnd() * 500)
        UserSerialKey.Text = TextBox1.Text + TextBox2.Text + TextBox3.Text
        DuplicateTxtBox.Text = TextBox1.Text + TextBox2.Text + TextBox3.Text

        Try
            Using con As New SqlConnection("Data Source=111.111.111.111; Initial Catalog=serialTestResults; User ID=serialTestResults; Password= 'password'; ")
                cmd.Connection = con

                cmd.CommandText = ("select Serial from SerialKey where Serial='" + DuplicateTxtBox.Text() + "'")
                con.Open()
                Dim lrd As SqlDataReader = cmd.ExecuteReader()
                If lrd.HasRows Then
                    lrd.Read()
                    IAmADuplicate.Text = lrd.GetValue(0).ToString()               
                    If DuplicateTxtBox.Text = IAmADuplicate.Text Then
                        TextBox1.Text = Int(Rnd() * 600)
                        TextBox2.Text = Int(Rnd() * 500)
                        TextBox3.Text = Int(Rnd() * 400)
                        UserSerialKey.Text = TextBox1.Text + TextBox2.Text + TextBox3.Text
                        DuplicateTxtBox.Text = TextBox1.Text + TextBox2.Text + TextBox3.Text
                      
                    End If
                End If
            End Using
          
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try

        Try
            Using con As New SqlConnection("Data Source=111.111.11.111; Initial Catalog=SerialResults; UserID=serialTestResults; Password= 'password'; ")
                cmd.Connection = con

                cmd.CommandText = ("select Serial from SerialKey where Serial='" + DuplicateTxtBox.Text() + "'")
                con.Open()
                Dim lrd As SqlDataReader = cmd.ExecuteReader()
                If lrd.HasRows Then
                    lrd.Read()
                    IAmADuplicate.Text = lrd.GetValue(0).ToString()

                Else
                    MsgBox("Good to go")

                End If
            End Using
            
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try

    End Sub



So can someone tell me how to do it so I only have to press the button once.
Thanks Nige

Posted Image

This post has been edited by Nigels: 02 December 2017 - 02:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Generating a serial and check database for duplicate

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14552
  • View blog
  • Posts: 58,359
  • Joined: 12-June 08

Re: Generating a serial and check database for duplicate

Posted 02 December 2017 - 03:12 PM

Move the button's code into a function and wrap the funtion call in a for loop that goes 50 times.
Was This Post Helpful? 1
  • +
  • -

#3 Radius Nightly   User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 304
  • Joined: 07-May 15

Re: Generating a serial and check database for duplicate

Posted 02 December 2017 - 03:32 PM

Or until its ready to go.
Was This Post Helpful? 0
  • +
  • -

#4 Nigels   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 78
  • Joined: 17-May 13

Re: Generating a serial and check database for duplicate

Posted 02 December 2017 - 03:43 PM

Thanks a million!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1