?
Solved

Do i have too many End IF statements in the code

Posted on 2005-04-05
13
Medium Priority
?
179 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:lvandevender
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
13 Comments
 

Author Comment

by:lvandevender
ID: 13708943
I am using VB.NET
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13709000
Looks right to me...are you getting an error?
0
 
LVL 7

Accepted Solution

by:
Joe_Griffith earned 2000 total points
ID: 13709051
I don't see any problem with End If but the "(" don't match the ")"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lvandevender
ID: 13709106
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
0
 

Author Comment

by:lvandevender
ID: 13709119
"I don't see any problem with End If but the "(" don't match the ")" what does not match???
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13709134
one too many end if's from what i can see
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13709143
in the second sample that is
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13709152
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


0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13709576
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
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13709617
you really need to use properties to not only make your code readable but easily maintained..
0
 
LVL 7

Expert Comment

by:Joe_Griffith
ID: 13709803
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?
0
 

Author Comment

by:lvandevender
ID: 13709890
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.
0
 

Author Comment

by:lvandevender
ID: 13709905
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

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question