Asp.net if statement - asp.net

Im having a problem with an IF statement.
The purpose of this statement is that all 3 managers must approve an order before it can prossesed.
This is the statement :
Dim RstAllchk
Dim RstAllchk_numRows
Set RstAllchk = Server.CreateObject("ADODB.Recordset")
RstAllchk.ActiveConnection = MM_DBConn_STRING
RstAllchk.Source = "SELECT comitee.OrderNo, comitee.Bart, comitee.Carel, comitee.Charl FROM comitee WHERE (((comitee.OrderNo)='" + Replace(RstAllData__varOrderNum, "'", "''") + "'));"
RstAllchk.CursorType = 0
RstAllchk.CursorLocation = 2
RstAllchk.LockType = 1
RstAllchk.Open()
RstAllchk_numRows = 0
if (RstAllchk.Fields.Item("Bart").Value)= "Approved" then
if (RstAllchk.Fields.Item("Carel").Value)= "Approved" then
if (RstAllchk.Fields.Item("Charl").Value)= "Approved" then
set cdata1 = Server.CreateObject("ADODB.Command")
cdata1.ActiveConnection = MM_DBConn_STRING
cdata1.CommandText = " UPDATE TblOrderData SET Fldapproved = 'Approved' WHERE FldOrderID = '" & RstAllData__varOrderNum & "'"
cdata1.CommandType = 1
cdata1.CommandTimeout = 0
cdata1.Prepared = true
cdata1.Execute()
cdata1.ActiveConnection.Close
set cdata2 = Server.CreateObject("ADODB.Command")
cdata2.ActiveConnection = MM_DBConn_STRING
cdata2.CommandText = " UPDATE TblOrderDetail SET FldMainapproved = 'Approved' WHERE FldOrderNum = '" & RstAllData__varOrderNum & "'"
cdata2.CommandType = 1
cdata2.CommandTimeout = 0
cdata2.Prepared = true
cdata2.Execute()
cdata2.ActiveConnection.Close
`
Sometimes if only one of the managers confirmed the order, the order is still approved. I have been struggling with this for days. Hope any of you can give me some advice.
Thanks

Assuming VB.NET:
IF (RstAllchk.Fields.Item("Bart").Value = "Approved" AND _
RstAllchk.Fields.Item("Carel").Value = "Approved" AND _
RstAllchk.Fields.Item("Charl").Value = "Approved") THEN
In C#:
if (RstAllchk.Fields.Item("Bart").Value == "Approved" &&
RstAllchk.Fields.Item("Carel").Value == "Approved" &&
RstAllchk.Fields.Item("Charl").Value == "Approved")

With C# (&& is And , == is equal)
if ( RstAllchk.Fields.Item("Bart").Value == "Approved"
&& RstAllchk.Fields.Item("Carel").Value == "Approved"
&& RstAllchk.Fields.Item("Charl").Value == "Approved"
)
{
//treatment
}

You can merge all like
(if var1 == x && var2 == y && var3 == z) {
//Do what you want
}

Related

Stop While Not when the correct answer is found

I've got a table with six records, so far. Everyone has 2 fields: Min_lenght, Max_lenght.
Those field define a range, so the first item has Min_lenght = 160, Max_lenght = 179 and so on.
When defining 1 value (custom_lenght) I need to stop the SQL when this values is in range.
So (custom_lenght > Min_lenght) AND (custom_lenght < Max_lenght)
custom_lenght = cint(request("custom_lenght"))
Set objRS2 = Server.CreateObject("ADODB.Recordset")
sql2 = "SELECT * FROM tbl_model where Order by ID_model ASC"
objRS2.Open sql2, ConString
If Not objRS2.EOF Then
While Not objRS2.EOF
Min_lenght = cint(objRS2("Min_lenght"))
Max_lenght = cint(objRS2("Max_lenght"))
order = objRS2("order")
Price = objRS2("price")
if (custom_lenght > Min_lenght ) AND (custom_lenght < Max_lenght) then
Outofrange="True"
else
Outofrange = "False"
End If
objRS2.MoveNext
Wend
End If
PuliziaRS(objRS2)
The problem is, the SQL browse all the records. I need to obtain the data (price and order) of the item that falls between the range
Change your SQL statement to "SELECT order, price FROM tbl_model WHERE (Min_length < " & custom_length & " AND Max_length > " & custom_length & ") ORDER BY ID_Model"
This will limit the recordset to records that match your custom_length.
As a alternative;
custom_lenght = cint(request("custom_lenght"))
Set objRS2 = Server.CreateObject("ADODB.Recordset")
sql2 = "SELECT order, price FROM tbl_model WHERE " & custom_lenght & " BETWEEN Min_lenght AND Max_lenght Order by ID_model ASC"
objRS2.Open sql2, ConString
If Not objRS2.EOF Then
order = objRS2("order")
Price = objRS2("price")
Outofrange = "True"
Else
Outofrange = "False"
End If
objRS2.Close

What's the best way to parse an SQL fragment string into a List(of string) for a Listbox control?

I'm trying to take this string:
(("DISPLAY_NAME" like N'sadf%') And ("ID" = 2) And ("IsCRITERION" = null))
and parse it into a List(of string) so that it can be displayed like:
(
(
"DISPLAY_NAME" like N'sadf%'
)
And
(
"ID" = 2
)
Or
(
"IsCRITERION" = null
)
)
I'm close but don't quite have it. My code currently looks like:
Dim filterlist As New List(Of String)
Dim temp As String = String.Empty
Dim lvl As Integer = 0
Dim pad As String = String.Empty
For Each chr As Char In originalString '--- filter is the string i posted above
Select Case chr.ToString.ToLower()
Case "("
filterlist.Add(pad.PadLeft(lvl * 5) & chr)
lvl += 1
Case ")"
filterlist.Add(pad.PadLeft(lvl * 5) & temp)
If lvl > 0 Then lvl -= 1
filterlist.Add(pad.PadLeft(lvl * 5) & chr)
'If lvl > 0 Then lvl -= 1
temp = String.Empty
Case Else
temp &= chr
End Select
Next
'--- Removes the empty line produced by generating the List(of String)
filterlist = filterlist.Where(Function(s) Not String.IsNullOrWhiteSpace(s)).ToList()
listSelectedCriteria.DataSource = filterlist
listSelectedCriteria.DataBind()
Unfortunately, the above code produces something close to what I desire but the "And"s and "Or"s are not in the right places:
(
(
"DISPLAY_NAME" like N'sadf%'
)
(
And "ID" = 2
)
(
Or "IsCRITERION" = null
)
)
Would using regular expressions be better? Thanks for the help
Probably the "best" way (although that's getting into "primarily opinion-based" territory) would be to use a parser, but assuming that your input is limited to similar looking strings, here's what I came up with:
Dim originalString = "((""DISPLAY_NAME"" like N'sadf%') And (""ID"" = 2) And (""IsCRITERION"" = null))"
Dim filterlist = New List(Of String)()
Dim temp = New StringBuilder()
Dim lvl = 0
Dim addLine =
Sub(x As String)
filterlist.Add(New String(" ", lvl * 4) & x.Trim())
End Sub
For Each c In originalString
Select Case c
Case "("
If temp.Length > 0 Then
addLine(temp.ToString())
temp.Clear()
End If
addLine("(")
lvl += 1
Case ")"
If temp.Length > 0 Then
addLine(temp.ToString())
temp.Clear()
End If
lvl -= 1
addLine(")")
Case Else
temp.Append(c)
End Select
Next
If temp.Length > 0 Then
addLine(temp.ToString())
temp.Clear()
End If
filterlist.Dump() ' LINQPad ONLY
This results in:
(
(
"DISPLAY_NAME" like N'sadf%'
)
And
(
"ID" = 2
)
And
(
"IsCRITERION" = null
)
)
However, you will probably end up having to add code as you find different inputs that don't quite work how you want.
Instead of looking at each characters, I would start be doing a split. And then add/remove padding depending on what character is at the start.
Dim tempString As String = "((""DISPLAY_NAME"" like N'sadf%') And (""ID"" = 2) And (""IsCRITERION"" = null))"
Dim curPadding As String = ""
Const padding As String = " "
Dim result As New List(Of String)
For Each s As String In Text.RegularExpressions.Regex.Split(tempString, "(?=[\(\)])")
If s <> "" Then
If s.StartsWith("(") Then
result.Add(curPadding & "(")
curPadding &= padding
result.Add(curPadding & s.Substring(1).Trim())
ElseIf s.StartsWith(")") Then
curPadding = curPadding.Substring(padding.Length)
result.Add(curPadding & ")")
result.Add(curPadding & s.Substring(1).Trim())
Else
result.Add(curPadding & s)
End If
End If
Next

My page dies when I execute the second ASP object

I don't know too much about ASP, here is my code:
<%
dim objCmd, objRS1, objRS2, strDate1, strDate2, strName, strStyle, datDate, strLastGroup, datNow
set objCmd = Server.CreateObject("ADODB.Command")
Set objRS1 = Server.CreateObject("ADODB.Recordset")
Set objRS2 = Server.CreateObject("ADODB.Recordset")
set objCmd.ActiveConnection = objConn
objCmd.CommandText = "Select Site_Pages.Filename, HeadingName, MatrixName, Site_Matrices.MatrixID, TurnaroundOverride, TurnaroundName, MAX(Turnaround) AS AutoTurnaround, TurnaroundGroup FROM Site_Headings, Site_Pages, Site_Matrices, Site_Items, Site_Items_Prices, Items WHERE Site_Headings.HeadingID = Site_Pages.HeadingID AND Site_Pages.ProdPageID = Site_Matrices.ProdPageID AND Site_Matrices.MatrixID = Site_Items.MatrixID AND Site_Items.ItemID = Site_Items_Prices.ItemID AND Site_Items_Prices.PriceID = Items.PriceID AND SiteID = 0 AND TurnaroundPage = 1 GROUP BY HeadingName, MatrixName, TurnaroundOverride, TurnaroundName, Site_Pages.Filename, TurnaroundGroup, TurnaroundSortOrder, Site_Matrices.MatrixID ORDER BY TurnaroundGroup, TurnaroundSortOrder, HeadingName, MatrixName, TurnaroundName"
set objRS1 = objCmd.Execute
if not objRS1.eof then
Call NewTable(objRS1("TurnaroundGroup"))
strLastGroup = objRS1("TurnaroundGroup")
end if
do until objRS1.eof
if strLastGroup <> objRS1("TurnaroundGroup") then
Call EndTable()
Call NewTable(objRS1("TurnaroundGroup"))
end if
strLastGroup = objRS1("TurnaroundGroup")
'decide todays date for checking dated turnaround overrides
datNow = dateadd("h", GetSetting("TimeOffset"), now())
'if before 7am, knock off a day
if hour(datnow) < 7 then datnow = dateadd("d", -1, datnow)
objCmd.CommandText = "SELECT Turnaround FROM DatedTurnarounds WHERE MatrixID = " & CLng(objRS1("MatrixID")) & " AND TDate = '" & Year(datNow) & PadWithZeros(Month(datNow)) & PadWithZeros(Day(datNow)) & " 00:00'"
set objRS2 = objCmd.Execute
if objRS1("TurnaroundOverride") = 0 OR IsNull(objRS1("TurnaroundOverride")) then
if objRS2.eof then 'use automatic (3-last)
datDate = AddDays(objRS1("AutoTurnaround")-1)
else 'use dated turnaround override (2-second)
datDate = AddDays(objRS2("Turnaround")-1)
end if
else 'use matrix overrride (1-first - overrides all)
datDate = AddDays(objRS1("TurnaroundOverride")-1)
end if
strDate1 = Left(WeekDayName(DatePart("w", datDate)), 3) & " " & DateSuffix(DatePart("d", datDate)) & " of " & MonthName(DatePart("m", datDate))
if objRS1("TurnaroundOverride") = 0 OR IsNull(objRS1("TurnaroundOverride")) then
if objRS2.eof then 'use automatic (3-last)
datDate = AddDays(objRS1("AutoTurnaround"))
else 'use dated turnaround override (2-second)
datDate = AddDays(objRS2("Turnaround"))
end if
else 'use matrix overrride (1-first - overrides all)
datDate = AddDays(objRS1("TurnaroundOverride"))
end if
objRS2.close
strDate2 = Left(WeekDayName(DatePart("w", datDate)), 3) & " " & DateSuffix(DatePart("d", datDate)) & " of " & MonthName(DatePart("m", datDate))
if objRS1("TurnaroundName") = "" OR IsNull(objRS1("TurnaroundName")) then
if objRS1("MatrixName") = "" OR IsNull(objRS1("MatrixName")) then
strName = objRS1("HeadingName")
else
strName = objRS1("MatrixName")
end if
else
strName = objRS1("TurnaroundName")
end if
if strStyle = "PriceMatrixRow1" then
strStyle = "PriceMatrixRow2"
else
strStyle = "PriceMatrixRow1"
end if
%>
I think the problem should be somewhere here
objCmd.CommandText = "SELECT Turnaround FROM DatedTurnarounds WHERE MatrixID = " & CLng(objRS1("MatrixID")) & " AND TDate = '" & Year(datNow) & PadWithZeros(Month(datNow)) & PadWithZeros(Day(datNow)) & " 00:00'"
set objRS2 = objCmd.Execute
I'm not sure if I can execute another object in an object though
Any help would be very much appreciated.

Any ideas how to convert array into FOR Loop?

The code below works great as is:
dashboard1.Text = charArray(0)
dashboard2.Text = charArray(1)
dashboard3.Text = charArray(2)
dashboard4.Text = charArray(3)
dashboard5.Text = charArray(4)
dashboard6.Text = charArray(5)
dashboard7.Text = ""
dashboard8.Text = ""
dashboard9.Text = ""
dashboard10.Text = ""
If dashboardl >= 7 Then
dashboard7.Text = charArray(6)
End If
If dashboardl >= 8 Then
dashboard8.Text = charArray(7)
End If
If dashboardl >= 9 Then
dashboard9.Text = charArray(8)
End If
If dashboardl >= 10 Then
dashboard10.Text = charArray(9)
End If
However, I would like to convert them to FOR Loop as in example below but I am getting errors.
For i = 1 To (dashboardl)
("dashboard" & CStr(i)) = charArray(i - 1)
Next i
Your assistance is greatly appreciated.
You can try this...
For i = 1 To dashboardl
Dim txtBox As TextBox = FindControl("dashboard" & i)
txtBox.Text = charArray(i - 1)
Next i
Ha, I missed the point. Still not on a real computer, but try this...
For i = 1 To dashboardl
Me.Controls("dashboard" & CStr(i)).Text = charArray(i - 1)
Next i

InStr() asp classic form field validation

I'm trying to check for valid email address in a form field using:
if Request ("email") = "" then
bError = true
ElseIf Instr(1, email," ") <> 0 Then
bError = true
ElseIf InStr(1, email, "#", 1) < 2 Then
bError = true
else
*/go to success page*/
But if there is a space in the email address it still passes the validation. So my question is, how do I check for spaces using this method?
You're better off using a regular expression for this.
http://classicasp.aspfaq.com/email/how-do-i-validate-an-e-mail-address.html
Function isEmailValid(email)
Set regEx = New RegExp
regEx.Pattern = "^\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w{2,}$"
isEmailValid = regEx.Test(trim(email))
End Function
Forget about all the elseif stuff do it simple...
Dim strEmail
Dim intErrors
intErrors = 0
strEmail = REQUEST("email")
strEmail = Trim(strEmail)
if strEmail = "" then intErrors = intErrors +1;
if instr(strEmail," ") > 0 then intErrors = intErrors +1;
if instr(strEmail,".") = 0 then intErrors = intErrors +1;
if instr(strEmail,"#") < 2 then intErrors = intErrors +1;
' Put as many test conditions as you want here
if intErrors = 0 then GotoSuccessPage
if Request ("email") = "" or Instr(email," ") > 0 or InStr(email, "#") < 2 then
bError = true
else
'go to success page
'BUT ABOUT OTHER ISSUES?
end if
---------------HERE IS A NON-REGEXP BASED EMAIL CHECKER, NOT SURE IF ITS FOOL PROOF BUT BETTER THAN THE SUBMITTED SNIPPET THAT SHOULD GET YOU GOING...
Function IsEmail(sCheckEmail)
Dim SEmail, NAtLoc
IsEmail = True
SEmail = Trim(sCheckEmail)
NAtLoc = InStr(SEmail, "#")
If Not (nAtLoc > 1 And (InStrRev(sEmail, ".") > NAtLoc + 1)) Then
IsEmail = False
ElseIf InStr(nAtLoc + 1, SEmail, "#") > NAtLoc Then
IsEmail = False
ElseIf Mid(sEmail, NAtLoc + 1, 1) = "." Then
IsEmail = False
ElseIf InStr(1, Right(sEmail, 2), ".") > 0 Then
IsEmail = False
End If
End Function

Resources