Question on Registration Check

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sweatha
    New Member
    • Mar 2008
    • 44

    Question on Registration Check

    Hi friends
    This is Sweatha. My problem is, I have created the Registration Form with
    one LabelBox as Label1
    one TextBox as TextBox1 &
    one Button as Button1. Then I have generated the random numbers by writing the WebMethod in Web Service as

    <WebMethod()> Public Function random1() As Double
    Dim Rand As New Random
    Dim i As Double
    i = Rand.NextDouble
    End Function
    and calling the WebMethod in Page Load event as

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load

    Dim obj As New RandomService
    Dim value As Double
    Dim Random1 As New Random
    Label1.Text = Random1.Next

    End Sub

    In the back end(SQL Server) I have created the table named matching with one field named 'match'.

    Now the trouble is

    I have to check whether the value entered in the TextBox is equal to that of the one which is in the LabelBox (which is the random one which is automatically generated) and if the value is same then the particular value should be inserted into the (backend) match field in the matching table by using insert query as

    If TextBox1.Text = Label1.Text Then

    str1 = "Insert into matching values(" & TextBox1.Text & ")"
    cmd = New SqlCommand(str1 , con)
    cmd.ExecuteNonQ uery()
    con.Close()
    Response.Write( "Inserted")
    End If

    My foremost problem is once if I click the button, the page is getting refreshed and the random number in label is changing and the TextBox Values are not getting equal to the LabelBox values & so I cant insert it into the DataBase.
  • balame2004
    New Member
    • Mar 2008
    • 142

    #2
    Hi,

    In Asp.Net page load event occurs whenever page is getting refreshed. Page is automatically getting refreshed whenever your request is posted back to the server. So page load event is called whenever you click the button since request is posted back to the server if you click a button. This is the reason to lebel text is automatically changing whenever you click the button.


    So you have to restrict the page load source code to be executed only when the page is requested for the first time and not for the later server requests. For that you have to use IsPostBack property of Page object. This IsPostBack property would be false when your page is requested for the first time and it would be true when your page is requested later(here whenever you click a button) .

    So you have to set the label control only when page is requested for the first time. Please use the below code.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load

    Dim obj As New RandomService
    Dim value As Double

    IF(!IsPostBack) 'If isPostBack is false
    Dim Random1 As New Random
    Label1.Text = Random1.Next
    EndIF

    End Sub

    Regards,
    Balaji U

    Comment

    • kunal pawar
      Contributor
      • Oct 2007
      • 297

      #3
      Hi Sweatha,
      balame2004 gives right solution just write your code into ! isPostback, so ur label is not get changed.
      And u will get right value on button click

      Comment

      Working...