2013-04-17 59 views
0

每當我嘗試此代碼頁面保持清爽。ASP.NET頁面不斷刷新(VB.NET)

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 [email protected]" 

     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 

爲什麼發生這種情況?

我檢查了未關閉的連接/ datareaders,但一切正常。

有什麼我失蹤了嗎?

+1

這是太多代碼來查看一個SO問題。您應該進行一些調試以縮小範圍。 – jbabey

+0

@jbabey是的,我試過了,但是我究竟需要檢查調試? – Brian

回答

1

您每次回發郵件時都會執行頁面加載方法。您應該檢查以查看!isPostBack以防止完整執行該代碼。

+0

我在哪裏檢查? – Brian

+0

你應該使用if(Not Page.isPostBack)將Page_Load中的所有代碼包裝在一起。我認爲這對於VB語法是正確的。基本上任何你不想在每個帖子後面執行的內容都應該在if語句之內。 – MikeR

+0

我試過,無濟於事。 – Brian