This topic is locked

Random number

10/21/2014 1:24:56 PM
ASPRunner.NET General questions
M
mroman98 author

Hi Forum,
I need to use an event to a button, here is what i need to do....

  1. Generate a Random number for a range between 5999 - 9999
  2. Read the table to make sure the record exists, if it does go back and generate another number until the record doesn't exist so i can added
    The button has 3 events?..
    Client Before

    Server

    Client After
    Do i generate the number in the Client Before or can i just do everything on the Server
    I guess i don't know how to code it. I have the same routine in VB 2008 and works fine but i don't know how to apply it here.
    The following is the code that i am using with vb 2008:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Me.Refresh()

    ' Initialize the random-number generator.

    Randomize()

    ' Generate random value between 5999 and 9999.

    Dim value As Integer = CInt(Int((9999 Rnd()) + 5999))

    EmployeeIDTextBox.Text = value
    ' Verify if Employee Number Exists ....
    Dim con As New SqlConnection("Data Source=SQLServer;Integrated Security=SSPI;Initial Catalog=Test")

    con.Open()

    mycommand = New SqlCommand("select count(
    ) from Employee where EmployeeID='" & EmployeeIDTextBox.Text & "'")

    mycommand.Connection = con

    PKExists = mycommand.ExecuteScalar
    If (PKExists > 0) Then

    MsgBox("Employee Already Exists, Please use Another Number !!!! ")
    End If
    con.Close()
    ' Set defaults ....

    CurrentFlagTextBox.Text = "1"

    UserUpdatedTextBox.Text = My.User.Name

    ModifiedDateDateTimePicker.Text = System.DateTime.Today
    End Sub
    Any suggestions?
    Manuel Roman

admin 10/21/2014

Your code needs to be placed to Server event. ClientBefore and ClientAfter are Javascript events.