Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CODE REVIEW -VISUAL BASIC WRITE COMMENTS IN THE CODE OF THINGS THAT NOT ALLOWED

ID: 3890287 • Letter: C

Question

CODE REVIEW -VISUAL BASIC

WRITE COMMENTS IN THE CODE OF THINGS THAT NOT ALLOWED OR DESCRIPTIONS THAT ARE NECESSARY. Visual Basic Coding Conventions

Imports System.Data.SqlClient
Public Class Plan_4___Edit_Plan
    Inherits System.Web.UI.Page

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        If Session.Item("AppUserId") Is Nothing Then
            Dim message As String = "Invalid connection. Please Login to view this page."
            Dim style = MsgBoxStyle.Critical
            MsgBox(message, style)
            Response.Redirect("~/Default.aspx")

        ElseIf Session.Item("AppUserRole").ToString = "student" Then
            Me.MasterPageFile = "~/StudentSite.Master"
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'If Session.Item("AppUserId") Is Nothing Then
        '    Dim message As String = "Invalid connection. Please Login to view this page."
        '    Dim style = MsgBoxStyle.Critical
        '    MsgBox(message, style)
        '    Response.Redirect("~/Default.aspx")

        If Session.Item("StudentId") Is Nothing Then
            Response.Redirect("~/Plan%201%20-%20Find%20Student.aspx")

        ElseIf Session.Item("PlanId") Is Nothing Then
            Response.Redirect("~/Plan%202%20-%20Find%20Plan.aspx")

        Else
            lblPlanId.Text = Session.Item("PlanId").ToString

            If lblPlanId.Text Is Nothing Then
                Dim message As String = "No Plan detected. Please select a valid plan to view this page."
                Dim style = MsgBoxStyle.Critical
                MsgBox(message, style)
                Response.Redirect("~/Plan%202%20-%20Find%20Plan.aspx")
                'Response.Redirect("~/Plan%202%20-%20Find%20Plan.aspx?StudentId=" + Trim(Request("StudentId"))
                                                   + "&StudentName=" + Trim(Request("StudentName")))

            Else
                EditPlanGrid.ShowHeaderWhenEmpty = True
                'Session("StudentId") = Trim(Request("StudentId"))
                'Session("StudentName") = Trim(Request("StudentName"))
                'Session("PlanId") = lblPlanId.Text

                Dim conn As New SqlConnection
                Dim cmd As New SqlCommand
                Dim read As SqlDataReader

                conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("AAAConnectionString").ConnectionString
                cmd.Connection = conn
                cmd.CommandText = "Select Plans.Name + ' - ' + YearTable.Name + ' '
                                                       + Program.Name As [Name]
     From Plans Inner Join CatalogYear
                                            On CatalogYear.CatalogYearId = Plans.CatalogYearId
                          Inner Join YearTable
                                            On YearTable.YearId = CatalogYear.YearId
                          Inner Join Student
                                            On Student.StudentId = Plans.StudentId
                          Inner Join Program
                                           On Program.ProgramId = Student.ProgramId
     Where PlanId = " + Session.Item("PlanId").ToString
                conn.Open()
                read = cmd.ExecuteReader()
                If read.HasRows Then
                    While read.Read()
                        lblEditPlanHeader.Text = Session.Item("StudentName") + ": " + read.GetValue(0).ToString
                        If Session.Item("AppUserRole").ToString = "student" Then
                            lblEditPlanHeader.Text = read.GetValue(0).ToString
                        End If
                    End While
                    ' alternateColors()
                End If
            End If

        End If
    End Sub


    ' Protected Function GetUrl() As String
    'Dim url As String
    '   url = "~/Plan%205%20-%20Edit%20Course.aspx?StudentId=" + lblStudentID.Text + "&StudentName=" + lblStudentName.Text + "&PlanId=" + lblPlanId.Text + "&CourseName=" + lblCourseName.Text
    ' Return url
    'End Function

    'Protected Sub EditPlanGrid_SelectedIndexChanging(sender As Object, e As GridViewSelectEventArgs)
    '    lblCourseName.Text = EditPlanGrid.Rows(e.NewSelectedIndex).Cells(0).Text
    'End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSetActive.Click
        Dim conn As New SqlConnection
        Dim cmd As New SqlCommand

        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("AAAConnectionString").ConnectionString

        cmd.Connection = conn
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "MakeActivePlan"

        cmd.Parameters.Add(New SqlParameter("@PlanId", SqlDbType.Int))
        cmd.Parameters("@PlanId").Value = Session.Item("PlanId")

        cmd.Parameters.Add(New SqlParameter("@StudentId", SqlDbType.Int))
        cmd.Parameters("@StudentId").Value = Session.Item("StudentId")

        conn.Open()
        cmd.ExecuteNonQuery()

    End Sub

    Protected Sub btnSelectNewPlan_Click(sender As Object, e As EventArgs) Handles btnSelectNewPlan.Click
        Response.Redirect("~/Plan%202%20-%20Find%20Plan.aspx")
        'Response.Redirect("~/Plan%202%20-%20Find%20Plan.aspx?StudentId=" + Trim(Request("StudentId")) + "&StudentName=" + Trim(Request("StudentName")))
    End Sub

    Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs) Handles EditPlanGrid.RowDataBound
        Try
            'Get the year on the first row
            Dim yearLabel As Label = EditPlanGrid.Rows(0).Cells(0).FindControl("Label1")
            Dim year As String = yearLabel.Text

            ' get the semester on the current row
            Dim semester As String = EditPlanGrid.Rows(0).Cells(1).Text

            Dim curSemester As String

            Dim colorType As Integer = 0

            Dim gridYearLabel As Label
            Dim gridYear As String = ""
            Dim switchCount As Integer = 0

            For count As Integer = 0 To EditPlanGrid.Rows.Count - 1

                ' get the year on the current row
                gridYearLabel = EditPlanGrid.Rows(count).Cells(0).FindControl("Label1")
                gridYear = gridYearLabel.Text

                ' get the semeseter on the current row
                curSemester = EditPlanGrid.Rows(count).Cells(1).Text

                If year = gridYear Then
                    ' the current row is the same as the previous row
                    If curSemester <> semester Then
                        colorType = colorType + 1
                        semester = curSemester
                    End If
                    If switchCount Mod 2 = 0 Then
                        If colorType = 0 Then
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.LightGreen
                        ElseIf colorType = 1 Then
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.LimeGreen
                        Else
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.Green
                        End If
                    Else
                        If colorType = 0 Then
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.LightYellow 'Coral
                        ElseIf colorType = 1 Then
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.Khaki   'Tomato
                        Else
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.Gold 'Red
                        End If
                    End If
                Else
                    ' the current row is different from the previous row
                    year = gridYear
                    switchCount = switchCount + 1

                    colorType = 0
                    semester = EditPlanGrid.Rows(count).Cells(1).Text

                    If switchCount Mod 2 = 0 Then
                        EditPlanGrid.Rows(count).BackColor = Drawing.Color.LightGreen
                    Else
                        EditPlanGrid.Rows(count).BackColor = Drawing.Color.LightYellow 'Coral
                    End If
                End If


            Next
        Catch

        End Try
    End Sub

    Protected Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
        Response.Redirect("~/Plan%207%20-%20Insert%20Course.aspx")
    End Sub
End Class

Explanation / Answer

// Importing is the Namespace

Imports System.Data.SqlClient

//Declaring the class as public type
Public Class Plan_4___Edit_Plan
    Inherits System.Web.UI.Page

// Declaring the method

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

//Specifying the conditions
        If Session.Item("AppUserId") Is Nothing Then
            Dim message As String = "Invalid connection. Please Login to view this page."
            Dim style = MsgBoxStyle.Critical
            MsgBox(message, style)
            Response.Redirect("~/Default.aspx")

        ElseIf Session.Item("AppUserRole").ToString = "student" Then
            Me.MasterPageFile = "~/StudentSite.Master"
        End If
    End Sub

// Declaring the method

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'If Session.Item("AppUserId") Is Nothing Then
        '    Dim message As String = "Invalid connection. Please Login to view this page."
        '    Dim style = MsgBoxStyle.Critical
        '    MsgBox(message, style)
        '    Response.Redirect("~/Default.aspx")

        If Session.Item("StudentId") Is Nothing Then
            Response.Redirect("~/Plan%201%20-%20Find%20Student.aspx")

        ElseIf Session.Item("PlanId") Is Nothing Then
            Response.Redirect("~/Plan%202%20-%20Find%20Plan.aspx")

        Else
            lblPlanId.Text = Session.Item("PlanId").ToString

            If lblPlanId.Text Is Nothing Then
                Dim message As String = "No Plan detected. Please select a valid plan to view this page."
                Dim style = MsgBoxStyle.Critical
                MsgBox(message, style)
                Response.Redirect("~/Plan%202%20-%20Find%20Plan.aspx")
                'Response.Redirect("~/Plan%202%20-%20Find%20Plan.aspx?StudentId=" + Trim(Request("StudentId"))
                                                   + "&StudentName=" + Trim(Request("StudentName")))

            Else
                EditPlanGrid.ShowHeaderWhenEmpty = True
                'Session("StudentId") = Trim(Request("StudentId"))
                'Session("StudentName") = Trim(Request("StudentName"))
                'Session("PlanId") = lblPlanId.Text

//Creating Database Connections               

Dim conn As New SqlConnection
                Dim cmd As New SqlCommand
                Dim read As SqlDataReader

                conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("AAAConnectionString").ConnectionString
                cmd.Connection = conn
                cmd.CommandText = "Select Plans.Name + ' - ' + YearTable.Name + ' '
                                                       + Program.Name As [Name]
     From Plans Inner Join CatalogYear
                                            On CatalogYear.CatalogYearId = Plans.CatalogYearId
                          Inner Join YearTable
                                            On YearTable.YearId = CatalogYear.YearId
                          Inner Join Student
                                            On Student.StudentId = Plans.StudentId
                          Inner Join Program
                                           On Program.ProgramId = Student.ProgramId
     Where PlanId = " + Session.Item("PlanId").ToString
                conn.Open()
                read = cmd.ExecuteReader()
                If read.HasRows Then
                    While read.Read()
                        lblEditPlanHeader.Text = Session.Item("StudentName") + ": " + read.GetValue(0).ToString
                        If Session.Item("AppUserRole").ToString = "student" Then
                            lblEditPlanHeader.Text = read.GetValue(0).ToString
                        End If
                    End While
                    ' alternateColors()
                End If
            End If

        End If
    End Sub

//Specifying the function


    ' Protected Function GetUrl() As String
    'Dim url As String
    '   url = "~/Plan%205%20-%20Edit%20Course.aspx?StudentId=" + lblStudentID.Text + "&StudentName=" + lblStudentName.Text + "&PlanId=" + lblPlanId.Text + "&CourseName=" + lblCourseName.Text
    ' Return url
    'End Function

//Specifying the function

    'Protected Sub EditPlanGrid_SelectedIndexChanging(sender As Object, e As GridViewSelectEventArgs)
    '    lblCourseName.Text = EditPlanGrid.Rows(e.NewSelectedIndex).Cells(0).Text
    'End Sub

//Specifying the function

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSetActive.Click
        Dim conn As New SqlConnection
        Dim cmd As New SqlCommand

        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("AAAConnectionString").ConnectionString

        cmd.Connection = conn
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "MakeActivePlan"

        cmd.Parameters.Add(New SqlParameter("@PlanId", SqlDbType.Int))
        cmd.Parameters("@PlanId").Value = Session.Item("PlanId")

        cmd.Parameters.Add(New SqlParameter("@StudentId", SqlDbType.Int))
        cmd.Parameters("@StudentId").Value = Session.Item("StudentId")

        conn.Open()
        cmd.ExecuteNonQuery()

    End Sub

//Specifying the function

    Protected Sub btnSelectNewPlan_Click(sender As Object, e As EventArgs) Handles btnSelectNewPlan.Click
        Response.Redirect("~/Plan%202%20-%20Find%20Plan.aspx")
        'Response.Redirect("~/Plan%202%20-%20Find%20Plan.aspx?StudentId=" + Trim(Request("StudentId")) + "&StudentName=" + Trim(Request("StudentName")))
    End Sub

//Specifying the function

    Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs) Handles EditPlanGrid.RowDataBound

// Try- Catch Block
        Try
            'Get the year on the first row
            Dim yearLabel As Label = EditPlanGrid.Rows(0).Cells(0).FindControl("Label1")
            Dim year As String = yearLabel.Text

            ' get the semester on the current row
            Dim semester As String = EditPlanGrid.Rows(0).Cells(1).Text

            Dim curSemester As String

            Dim colorType As Integer = 0

            Dim gridYearLabel As Label
            Dim gridYear As String = ""
            Dim switchCount As Integer = 0

            For count As Integer = 0 To EditPlanGrid.Rows.Count - 1

                ' get the year on the current row
                gridYearLabel = EditPlanGrid.Rows(count).Cells(0).FindControl("Label1")
                gridYear = gridYearLabel.Text

                ' get the semeseter on the current row
                curSemester = EditPlanGrid.Rows(count).Cells(1).Text

                If year = gridYear Then
                    ' the current row is the same as the previous row
                    If curSemester <> semester Then
                        colorType = colorType + 1
                        semester = curSemester
                    End If
                    If switchCount Mod 2 = 0 Then
                        If colorType = 0 Then
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.LightGreen
                        ElseIf colorType = 1 Then
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.LimeGreen
                        Else
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.Green
                        End If
                    Else
                        If colorType = 0 Then
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.LightYellow 'Coral
                        ElseIf colorType = 1 Then
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.Khaki   'Tomato
                        Else
                            EditPlanGrid.Rows(count).BackColor = Drawing.Color.Gold 'Red
                        End If
                    End If
                Else
                    ' the current row is different from the previous row
                    year = gridYear
                    switchCount = switchCount + 1

                    colorType = 0
                    semester = EditPlanGrid.Rows(count).Cells(1).Text

                    If switchCount Mod 2 = 0 Then
                        EditPlanGrid.Rows(count).BackColor = Drawing.Color.LightGreen
                    Else
                        EditPlanGrid.Rows(count).BackColor = Drawing.Color.LightYellow 'Coral
                    End If
                End If


            Next
        Catch

        End Try
    End Sub

//Specifying the function

    Protected Sub btnInsert_Click(sender As Object, e As EventArgs) Handles btnInsert.Click
        Response.Redirect("~/Plan%207%20-%20Insert%20Course.aspx")
    End Sub
End Class

Comments are prefixed with //

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote