Reputation: 1841
In my web application I have defined a Regex
, Match
and str
. So the Regex
will be used to compare other strings with str
and the Match
to identify any matches found in the textboxes
. In my code I have 3 strings defined as matches
to be compared with str
. A connection to the database is also defined. The following code shows the definitions:
Dim r As Regex = New Regex(str2, RegexOptions.IgnoreCase)
Dim m1 As Match = r.Match(txt1.Text.ToString)
Dim m2 As Match = r.Match(txt2.Text.ToString)
Dim m3 As Match = r.Match(txt3.Text.ToString)
Dim myconn As New SqlConnection(ConnString)
The part I am facing problems with is that I have many if else
statements and I cannot determine a way to control them appropriately as follows:
If (m1.Success) AND (m2.Success) AND (m3.Success) Then
Try
myconn.Open()
Dim cmd = New SqlCommand("Update Std SET Title = @param ", myconn)
cmd.Parameters.Add("@param", Data.SqlDbType.Bit).Value = bit_True
cmd.ExecuteNonQuery()
cmd.Dispose()
myconn.Close()
Catch ex As Exception
Response.Write(ex)
End Try
ElseIf (m2.Success) Then
Try
myconn.Open()
Dim cmd = New SqlCommand("Update Teacher SET In_name = @param ", myconn)
cmd.Parameters.Add("@param", Data.SqlDbType.Bit).Value = bit_True
cmd.ExecuteNonQuery()
cmd.Dispose()
myconn.Close()
Catch ex As Exception
Response.Write(ex)
End Try
ElseIf (m3.Success) Then
Try
myconn.Open()
Dim cmd = New SqlCommand("Update Std SET std_name = @param ", myconn)
cmd.Parameters.Add("@param", Data.SqlDbType.Bit).Value = bit_True
cmd.ExecuteNonQuery()
cmd.Dispose()
myconn.Close()
Catch ex As Exception
Response.Write(ex)
End Try
End If
What I am expecting out of the code is to:
Check the first condition m1.success
if true
then execute the first Try
block otherwise if False
ignore all Try
blocks AND move to the next condition (m2), then
Check the second condition m2.success
if true
then execute the second Try
block, otherwise if False
ignore all Try
blocks AND move to the next condition (m3), then
Same goes for m3.Success
So this implies that the first Try
block relates to the first if statement
(m1.Success)
and should be executed only if the condition (m1) is true. Same goes for m2 and m3. In this way I would be have a dynamic control upon the statements. So
m1
= execute first Try
block.
m2
= execute second Try
block.
m3
= execute third Try
block.
Any condition which returns False
will be ignored along with its Try
block.
Any suggestions or thoughts on how to have a dynamic control over those conditions ?
Upvotes: 0
Views: 1875
Reputation: 36
it is a web application, so I assume you have put the database connections in a connection pool or a connection manager.
Define a abstract class that does the following
You can create as many pattern/logic as you need in sub classes, and put their instances in a collection (array or list). Upon receiving a user request, run through all instances and let them do their matching and database querying.
Upvotes: 0
Reputation: 9878
Maybe just this?
If (m1.Success) Then
'[...]
End If
If (m2.Success) Then
'[...]
End If
If (m3.Success) Then
'[...]
End If
Upvotes: 1
Reputation: 3920
You can use an Enum as a Flag to control which of the blocks will execute by checking them with AND statements and the enum value will be a result of the successes of the regex.
Another optimization would be to open/close the connection before/after the whole If so that you boost performance a bit.
Upvotes: 0