0

每当我尝试此代码时,页面都会保持刷新。

Imports System.Data.SqlClient
Imports System.Data

Partial Class ProjectReport
    Inherits System.Web.UI.Page

    Private myTotal As Decimal = 0


    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load, Chart1.Load

        Dim ProjectID As Integer = Session("project_id")
        Session("ProjectID") = ProjectID

        lblProjNameHeading.Text = "[ " + ProjectID.ToString + " ]"

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim projComm As String = "SELECT project_id, project_start, project_finish, project_budget, project_cost FROM projects WHERE project_id=@parameter"

        Dim projSQL As New SqlCommand

        conn.Open()

        projSQL = New SqlCommand(projComm, conn)
        projSQL.Parameters.AddWithValue("@parameter", ProjectID.ToString)

        Dim datareader As SqlDataReader = projSQL.ExecuteReader()


        While datareader.Read

            lblProjectCode.Text = datareader("project_id").ToString
            lblProjectStart.Text = datareader("project_start").ToString
            lblProjectStart2.Text = datareader("project_start").ToString
            lblProjectEnd.Text = datareader("project_finish").ToString
            lblProjectEnd2.Text = datareader("project_finish").ToString
            lblProjectBudget.Text = datareader("project_budget").ToString
            lblProjectBudget2.Text = datareader("project_budget").ToString
            lblProjectCost.Text = datareader("project_cost").ToString
            lblProjectCost2.Text = datareader("project_cost").ToString
            ' lblProjectLeader.Text = datareader("project_cost").ToString
            'lblProjectExpenditures.Text = agdgssag

            Dim StartDate As DateTime = datareader("project_start")

            Dim FinishDate As DateTime = datareader("project_finish")

            Dim today As DateTime = DateTime.Now

            Dim sumDays = (FinishDate - StartDate).TotalDays
            Dim daysToNow = (today - StartDate).TotalDays

            Dim percentage = daysToNow / sumDays * 100

            Dim percentageLeft = 100 - percentage

            Session("PercentageCompleted") = percentage
            Session("PercentageLeft") = percentageLeft

            GetTable()

            Expenditures()
            lblProjectPercentage.Text = percentage.ToString("N2") + "%"

        End While

        datareader.Close()
        conn.Close()

        If Not Page.IsPostBack Then
            BindData()
        End If

    End Sub

    Private Sub BindData()

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim query As New SqlCommand("SELECT Items.item_name, Items.item_cost, project_items.item_quantity FROM Items INNER JOIN project_items ON items.item_id = project_items.item_id WHERE project_items.project_id = @parameter", conn)

        conn.Open()

        query.Parameters.AddWithValue("@parameter", Convert.ToInt32(Session("ProjectID")))
        Dim da As New SqlDataAdapter(query)

        da.SelectCommand = query

        Dim table As New DataTable()

        da.Fill(table)

        grdItems.DataSource = table
        conn.Close()
        grdItems.DataBind()

    End Sub

    Protected Sub grdItems_RowDataBound(sender As Object, e As GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
            myTotal += (CDec(rowView("item_cost")) * CDec(rowView("item_quantity")))

        End If

        If e.Row.RowType = DataControlRowType.Footer Then
            Dim lblTotalPrice As Label = DirectCast(e.Row.FindControl("lblTotalPrice"), Label)
            lblTotalPrice.Text = myTotal.ToString()

        End If
    End Sub

    Function GetTable() As DataTable

        Dim table As New DataTable

        table.Columns.Add("Percentage Completed", GetType(Double))
        table.Columns.Add("Percentage Not-Completed", GetType(Double))

        table.Rows.Add(Session("PercentageCompleted"))
        table.Rows.Add(Session("PercentageLeft"))

        Chart1.DataSource = table
        Chart1.Series("Series1").XValueMember = "Percentage Not-Completed"
        Chart1.Series("Series1").YValueMembers = "Percentage Completed"
        Chart1.DataBind()

        Return table
    End Function

    Private Sub Expenditures()

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim projExpComm As String = "SELECT Items.item_cost, project_items.item_quantity FROM Items INNER JOIN project_items ON items.item_id = project_items.item_id WHERE project_items.project_id = @parameter"

        Dim projExpSQL As New SqlCommand

        conn.Open()

        projExpSQL = New SqlCommand(projExpComm, conn)
        projExpSQL.Parameters.AddWithValue("@parameter", Session("project_id"))

        Dim datareader As SqlDataReader = projExpSQL.ExecuteReader()

        datareader.Read()

        While datareader.Read

            While datareader.HasRows

                Dim ItemsTotal As Double = 0

                For Each row In datareader

                    Dim ItemCost = datareader("item_cost")
                    Dim ItemQuantity = datareader("item_quantity")
                    Dim ItemsSubTotal As Double = ItemCost * ItemQuantity

                    ItemsTotal = ItemsSubTotal
                Next

                ItemsTotal = ItemsTotal + ItemsTotal
                lblProjectExpenditures.Text = ItemsTotal.ToString

            End While


        End While

        datareader.Close()
        conn.Close()

    End Sub

End Class

为什么会这样?

我检查了未关闭的连接/数据读取器,但一切正常。

有什么我想念的吗?

4

1 回答 1

1

每次回发时,您的页面加载方法都会完全执行。您应该查看 !isPostBack 以防止完全执行该代码。

于 2013-04-17T13:34:36.860 回答