Click here to Skip to main content
15,881,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have this details view control where users enter their leave application I have used the if statement to set conditions but the details are still inserted even when the conditions are not met. I have tried using exit sub but its giving the same result.
Here is the code.
VB
If days <= 1 Then
            Label3.Visible = True
            lvconn.Close()
            Exit Sub
        ElseIf days >= 30 Then
            Label4.Visible = True
            lvconn.Close()
            Exit Sub
        Else
            Response.Redirect("Leave_details.aspx")
        End If
Posted

1 solution

Is this code in a loop?
If so you could use Exit For or Exit Do which will exit from a for loop and a do loop respectively.
 
Share this answer
 
v2
Comments
larsh from Nairobi 27-Jan-11 8:21am    
Ryan, its just an if statement with two conditions nothing much.
Thanks 4 the advice.
Ryan Zahra 27-Jan-11 8:23am    
Can I see the whole method instead?
larsh from Nairobi 27-Jan-11 8:31am    
Its here.

Protected Sub LinkButton1_Click1(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lvconn As New SqlConnection("Data Source=MARS\SQLEXPRESS;Initial Catalog=leave;Persist Security Info=True;User ID=sa;Password=charkushck")
Dim cmd As New SqlCommand()
cmd.Connection = lvconn
Dim tdate, fdate As Date
Dim fdate2 As TextBox = CType(DetailsView1.FindControl("fdate2"), TextBox)
Dim tdate1 As TextBox = CType(DetailsView1.FindControl("tdate1"), TextBox)
tdate = tdate1.Text
fdate = fdate2.Text
Dim ts As TimeSpan = tdate.Subtract(fdate)
Dim days As Integer = ts.Days
lvconn.Open()
If days <= 1 Then
Label3.Visible = True
lvconn.Close()
Exit Sub
ElseIf days >= 30 Then
Label4.Visible = True
lvconn.Close()
Exit Sub
Else
Response.Redirect("Leave_details.aspx")
End If
End Sub
Ryan Zahra 27-Jan-11 8:39am    
I must ask why you are opening the connection and not always closing it, apart from not being in a try catch finally block.

Also try to debug your code and watch the variables value. What are the values of tdate and fdate? Are you sure they are storing the date correctly? Also what is the value of days?
larsh from Nairobi 27-Jan-11 8:46am    
First, about the opening and closing of the connection its something I was trying to fix the problem.

For the variables tdate and fdate am just using them in the timespan since am certain it cant accept to use the text boxes(fdate2&tdate1).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900