Link to home
Start Free TrialLog in
Avatar of lvandevender
lvandevender

asked on

Do i have too many End IF statements in the code

I need to know if I need this many IF statements in this code


If Request.Form("StartDate") <> "" Or Request.Form("EndDate") <> "" Then
            If Request.Form("datetype") = "Orders Shipped" Then
                scmd.Append(" AND (")
                If Request.Form("StartDate") <> "" Then
                    scmd.Append(" SHIP_DT >= TO_DATE('")
                    If Request.Form("StartDate") <> "" Then
                        scmd.Append(Year(Request.Form("StartDate")))
                        scmd.Append("/")
                        scmd.Append(Month(Request.Form("StartDate")))
                        scmd.Append("/")
                        scmd.Append(Day(Request.Form("StartDate")))
                        scmd.Append("','yyyy/mm/dd')")
                    End If
                End If
            End If
        End If
Avatar of lvandevender
lvandevender

ASKER

I am using VB.NET
Avatar of Mike Tomlinson
Looks right to me...are you getting an error?
ASKER CERTIFIED SOLUTION
Avatar of Joe_Griffith
Joe_Griffith

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what about this code??

If Request.Form("EndDate") <> "" Then
                    If Request.Form("StartDate") <> "" Then
                        scmd.Append(" SHIP_DT <= TO_DATE('")
                        scmd.Append(Year(Request.Form("StartDate")))
                        scmd.Append("/")
                        scmd.Append(Month(Request.Form("StartDate")))
                        scmd.Append("/")
                        scmd.Append(Day(Request.Form("StartDate")))
                        scmd.Append(", yyyy/mm/dd)")
                        scmd.Append(" AND ")
                    End If
                    scmd.Append(" SHIP_DT <= TO_DATE('")
                    scmd.Append(Year(Request.Form("EndDate")))
                    scmd.Append("/")
                    scmd.Append(Month(Request.Form("EndDate")))
                    scmd.Append("/")
                    scmd.Append(Day(Request.Form("EndDate")))
                    scmd.Append("','yyyy/mm/dd')")
                End If
                scmd.Append(") ")
            End If
"I don't see any problem with End If but the "(" don't match the ")" what does not match???
one too many end if's from what i can see
in the second sample that is
Private ReadOnly Property HasStartDate() As Boolean
      Get
            Dim value As Boolean = ( DateTime.Compare(StartDate,DateTime.MinValue) > 0)
            Return value
      End Get
End Property

Private ReadOnly Property HasEndDate() As Boolean
      Get
            Dim value As Boolean = ( DateTime.Compare(EndDate,DateTime.MinValue) > 0)
            Return value
      End Get
End Property



Private ReadOnly Property StartDate() As DateTime
      Get
            Dim value As String = GetItem("StartDate")
            Dim dt As DateTime
            If value.Length > 0 Then
                  dt = DateTime.Parse(value)
            End If
            Return dt
      End Get
End Property

Private ReadOnly Property DateType() As String
      Get
            Return GetItem("datetype")
      End Get
End Property

Private ReadOnly Property EndDate() As DateTime
      Get
            Dim value As String = GetItem("EndDate")
            Dim dt As DateTime
            If value.Length > 0 Then
                  dt = DateTime.Parse(value)
            End If
            Return dt
      End Get
End Property


Private Function GetItem(ByVal name As String) As String
      Dim value As String = String.Empty
      If Not Request.Form(name) Is Nothing Then
            value = Request.Form(name)
      End If
      Return value.Trim()
End Function

' your code
If HasStartDate AndAlso HasEndDate AndAlso DateType = "Orders Shipped" Then
      scmd.Append(" AND (")
      If HasStartDate Then
            'if scmd is StringBuilder
            scmd.AppendFormat("SHIP_DT >= TO_DATE('{0}','yyyy/MM/dd')",StartDate.ToString("yyyy/MM/dd"))
      End If
End If


If HasDateType AndAlso (HasStartDate OrElse HasEndDate)Then
      scmd.Append(" AND (")
      If HasStartDate AndAlso HasEndDate Then
            scmd.AppendFormat("SHIP_DT >= TO_DATE('{0}','yyyy/MM/dd') AND SHIP_DT <= TO_DATE('{1}','yyyy/MM/dd')",StartDate.ToString("yyyy/MM/dd"),EndDate.ToString("yyyy/MM/dd"))
      ElseIf HasStartDate Then
            scmd.AppendFormat("SHIP_DT >= TO_DATE('{0}','yyyy/MM/dd')",StartDate.ToString("yyyy/MM/dd" ))
      Else
            scmd.AppendFormat("SHIP_DT <= TO_DATE('{0}','yyyy/MM/dd')",EndDate.ToString("yyyy/MM/dd" ))
      End If
      scmd.Append(") ")
End If
you really need to use properties to not only make your code readable but easily maintained..
The left parentheses don't match the right parentheses.  I'm not sure what your basic problem is.  You asked about "End If"s and that has been answered.  

Are you getting some error or exception?  What is it you are asking?
My problem is that I created a data grid for a web form and when i view it in the browser it will not come up. the link buttons are there and the images and label but no datagrid. I am trying to figure out if my selection criteria is wrong . There is a page that is opened before this page that controls what data comes up on the datagrid.
i can give you the entire code if you want but its long ha ha but it may help here it is.. its saved at pipelineviewresults.aspx.vb so i guess this is vb.net and asp.net


Imports System.Data
Imports System.Data.Oledb
Imports System.Text

Public Class PipelineViewResults
    Inherits System.Web.UI.Page
    Public Criteria
    Public ocriteria
    Public Codes
    Public OpCo
    Public PO
    Public obox
    Public CodeCriteria
    Public StartDate
    Public EndDate
    Public Datetype

    Protected Property [SortField]() As String
        Get
            Return CStr(ViewState("SortField"))
        End Get
        Set(ByVal Value As String)
            ViewState("SortField") = Value
        End Set
    End Property
    Protected Property [Direction]() As String
        Get
            Return CStr(Viewstate("Direction"))
        End Get
        Set(ByVal Value As String)
            ViewState("Direction") = Value
        End Set
    End Property
    Private reader As OleDbDataReader


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Button1 As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents Button2 As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents button3 As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents button4 As System.Web.UI.HtmlControls.HtmlInputButton
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents Image1 As System.Web.UI.WebControls.Image

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Criteria = Request.Form("Criteria")
        obox = Request.Form("obox")
        OpCo = Request.Form("operatingcompany")
        PO = Request.Form("po")
        CodeCriteria = Request.Form("CodeCriteria")
        ocriteria = Request.Form("ocriteria")
        datetype = Request.Form("datetype")
        StartDate = Request.Form("StartDate")
        EndDate = Request.Form("EndDate")
        If Request.Form("Codes") = "" Then
            Codes = 1
        Else
            If (IsNumeric(Left(Request.Form("Codes"), InStr(1, Request.Form("Codes"), ".")))) Then
                Codes = CLng(Left(Request.Form("Codes"), InStr(1, Request.Form("Codes"), ".")))
            Else
                Codes = Left(Request.Form("Codes"), InStr(1, Request.Form("Codes"), ".") - 1)
            End If
        End If
        If (IsNumeric(Request.Form("operatingcompany"))) Then
            OpCo = CLng(Request.Form("operatingcompany"))
        Else
            OpCo = Request.Form("operatingcompany")
        End If
    End Sub


    Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
        If SortField = e.SortExpression() And Direction = " asc" Then
            Direction = " desc"
        Else
            Direction = " asc"
        End If
        SortField = e.SortExpression()
        getinfo()
    End Sub
    Public Sub getinfo()
        Const slen = 250, loops = 100
        Dim scmd As New StringBuilder

        Dim sSQL As String

        Dim strConnection As String = "Provider=msdaora ;DataSource=LISPROD;User id=Pipeuser;Password=Web"
        Dim con As New OleDbConnection(strConnection)

        Const sSEL1 = "SELECT DISTINCT a.PO_NUM, a.DEPT,a.DCNAME,a.LEGVENDOR, a.SHIP_DT, a.CANCEL_DT, a.STATUS, b.es_bol, sum(b.ed_piece), "
        Const sSEL2 = " FROM PIPELINE.VPV001PO a, maxmain.v_exptpo b "
        Const sSEL3 = " WHERE Rtrim(a.PO_NUM) = b.ed_pono"

        Const OrderBy = " Order By "
        Const Quote = "'"

        scmd.Append(sSEL1)
        scmd.Append(sSEL2)
        scmd.Append(sSEL3)

        Select Case CodeCriteria
            Case "GMM"
                scmd.Append("and gmm_num = ")
                scmd.Append(Codes)
            Case "DMM"
                scmd.Append("and dmm_num = ")
                scmd.Append(Codes)
            Case "Buyer"
                scmd.Append("and buyer_cd = ")
                scmd.Append(Codes)
            Case "Dept"
                If Len(Codes) = 1 Then
                    Codes = "00" & Codes
                ElseIf Len(Codes) = 2 Then
                    Codes = "0" & Codes
                End If
                scmd.Append(" AND DEPT = ")
                scmd.Append(Codes)
        End Select

        If OpCo <> "All" Then
            scmd.Append(" AND OPCO = ")
            scmd.Append(OpCo)
        End If

        If Request.Form("po") <> "" Then
            scmd.Append(" AND PO_NUM = '")
            scmd.Append(PO)
        End If
        If Request.Form("obox") <> "" Then
            Select Case Criteria
                Case "Legacy Vendor"
                    scmd.Append(" AND LEGVENDOR LIKE '%")
                    scmd.Append(obox)
                    scmd.Append("%'")
                Case "Corporate Vendor"
                    scmd.Append(" AND CORPVENDOR LIKE '%")
                    scmd.Append(obox)
                    scmd.Append("%'")
                Case "Division Vendor"
                    scmd.Append(" AND DIVVENDOR LIKE '%")
                    scmd.Append(obox)
                    scmd.Append("%'")
                Case "Outbound Manifest"
                    scmd.Append("select po_num from tpv014manx where manfst_num = ")
                    scmd.Append(obox)
                    scmd.Append(")")
                Case "BOL"
                    scmd.Append(" AND BOL = '")
                    scmd.Append(obox)
                    scmd.Append("'")
                Case "Buying Division"
                    scmd.Append(" AND DMM_NUM = '")
                    scmd.Append(obox)
                    scmd.Append("'")
            End Select
        End If


        If Request.Form("StartDate") <> "" Or Request.Form("EndDate") <> "" Then
            If Request.Form("datetype") = "Orders Shipped" Then
                scmd.Append(" AND (")
                If Request.Form("StartDate") <> "" Then
                    scmd.Append(" SHIP_DT >= TO_DATE('")
                    If Request.Form("StartDate") <> "" Then
                        scmd.Append(Year(Request.Form("StartDate")))
                        scmd.Append("/")
                        scmd.Append(Month(Request.Form("StartDate")))
                        scmd.Append("/")
                        scmd.Append(Day(Request.Form("StartDate")))
                        scmd.Append("','yyyy/mm/dd')")
                    End If
                End If
                If Request.Form("EndDate") <> "" Then
                    If Request.Form("StartDate") <> "" Then
                        scmd.Append(" SHIP_DT <= TO_DATE('")
                        scmd.Append(Year(Request.Form("StartDate")))
                        scmd.Append("/")
                        scmd.Append(Month(Request.Form("StartDate")))
                        scmd.Append("/")
                        scmd.Append(Day(Request.Form("StartDate")))
                        scmd.Append(", yyyy/mm/dd)")
                        scmd.Append(" AND ")
                    End If
                    scmd.Append(" SHIP_DT <= TO_DATE('")
                    scmd.Append(Year(Request.Form("EndDate")))
                    scmd.Append("/")
                    scmd.Append(Month(Request.Form("EndDate")))
                    scmd.Append("/")
                    scmd.Append(Day(Request.Form("EndDate")))
                    scmd.Append("','yyyy/mm/dd')")
                End If
                scmd.Append(") ")
            End If
            If Request.Form("datetype") = "Orders Canceled" Then
                scmd.Append(" AND (")
                If Request.Form("StartDate") <> "" Then
                    scmd.Append("CANCEL_DT >= TO_DATE('")
                    scmd.Append(Year(Request.Form("StartDate")))
                    scmd.Append("/")
                    scmd.Append(Month(Request.Form("StartDate")))
                    scmd.Append("/")
                    scmd.Append(Day(Request.Form("StartDate")))
                    scmd.Append("','yyyy/mm/dd')")
                End If
                If Request.Form("EndDate") <> "" Then
                    If Request.Form("StartDate") <> "" Then
                        scmd.Append(" AND ")
                        scmd.Append(Year(Request.Form("StartDate")))
                        scmd.Append("/")
                        scmd.Append(Month(Request.Form("StartDate")))
                        scmd.Append("/")
                        scmd.Append(Day(Request.Form("StartDate")))
                        scmd.Append(", yyyy/mm/dd)")
                        scmd.Append(" AND ")
                    End If
                    scmd.Append("CANCEL_DT <= TO_DATE('")
                    scmd.Append(Year(Request.Form("EndDate")))
                    scmd.Append("/")
                    scmd.Append(Month(Request.Form("EndDate")))
                    scmd.Append("/")
                    scmd.Append(Day(Request.Form("EndDate")))
                    scmd.Append("','yyyy/mm/dd')")
                End If
                scmd.Append(") ")
            End If
            If Request.Form("datetype") = "Orders Received" Then
                scmd.Append(" AND (")
                If Request.Form("StartDate") <> "" Then
                    scmd.Append("DELIVER_DT >= TO_DATE('")
                    scmd.Append(Year(Request.Form("StartDate")))
                    scmd.Append("/")
                    scmd.Append(Month(Request.Form("StartDate")))
                    scmd.Append("/")
                    scmd.Append(Day(Request.Form("StartDate")))
                    scmd.Append("','yyyy/mm/dd')")
                End If
                If Request.Form("EndDate") <> "" Then
                    If Request.Form("StartDate") <> "" Then
                        scmd.Append(" AND ")
                    End If
                    scmd.Append("DELIVER_DT <= TO_DATE('")
                    scmd.Append(Year(Request.Form("EndDate")))
                    scmd.Append("/")
                    scmd.Append(Month(Request.Form("EndDate")))
                    scmd.Append("/")
                    scmd.Append(Day(Request.Form("EndDate")))
                    scmd.Append("','yyyy/mm/dd')")
                End If
                scmd.Append(") ")
            End If
        End If

        If Not SortField = String.Empty Then
            scmd.Append(OrderBy)
            scmd.Append(SortField)
            scmd.Append(Direction)
        End If

        sSQL = scmd.ToString()
        'Response.Write(sSQL)

        Dim cmd As New OleDbCommand(sSQL, con)
        con.Open()
        reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        DataGrid1.DataSource = reader
        DataGrid1.DataBind()
        con.Close()

    End Sub

    Private Sub OleDbConnection1_InfoMessage(ByVal sender As System.Object, ByVal e As System.Data.OleDb.OleDbInfoMessageEventArgs)

    End Sub

    Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChanged

    End Sub
End Class