2012-09-29 105 views
0

我收到此錯誤System.OutOfMemoryException:類型'System.OutOfMemoryException'的異常被拋出。請幫助我。我得到這個錯誤(只有當我在網上主持網站,而不是在本地機器)。任何可能發生內存泄漏的機會?

Dim db As SqlDatabase = Connection.connection 
Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click 

    'Dim lblNodeID As Label = CType(Master.FindControl("lblParentId"), Label) 
    Using conn As DbConnection = db.CreateConnection() 
     Dim cmdInsertGroup As SqlCommand = db.GetSqlStringCommand("Insert Into CategoryGroups Values ('" & BLL.getNewGroupIDfromCategoryGroups & "','" & lblParentId.Text.Trim & "','" & txtGroupName.Text.Trim & "')") 

     Try 
     If fuGroupAttributes.HasFile Then 
      fuGroupAttributes.SaveAs(IO.Path.Combine(Server.MapPath("~/Admin/SpecificationExcels"), lblParentId.Text.Trim & IO.Path.GetExtension(fuGroupAttributes.FileName))) 

      Dim path As String = Server.MapPath("~/Admin/SpecificationExcels/" & lblParentId.Text.Trim & IO.Path.GetExtension(fuGroupAttributes.FileName)) 
      Dim strmail As String = String.Empty 
      Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path & ";Extended Properties=Excel 12.0;" 
      Dim objConn As New OleDbConnection(connectionString) 
      objConn.Open() 
      Dim strConString As String = "SELECT * FROM [Sheet1$]" 
      'where date = CDate('" + DateTime.Today.ToShortDateString() + "')"; 
      Dim objCmdSelect As New OleDbCommand(strConString, objConn) 
      ' Create new OleDbDataAdapter that is used to build a DataSet 
      ' based on the preceding SQL SELECT statement. 
      Dim objAdapter1 As New OleDbDataAdapter() 
      ' Pass the Select command to the adapter. 
      objAdapter1.SelectCommand = objCmdSelect 
      ' Create new DataSet to hold information from the worksheet. 
      Dim ds As New DataSet() 
      ' Fill the DataSet with the information from the worksheet. 
      objAdapter1.Fill(ds, "ExcelData") 
      'My Exp 
      Dim _newAttributeID As Integer = BLL.getNewAttributeIDfromGroupAttributes 
      Dim _newGroupID As Integer 
      conn.Open() 
      Dim trans As DbTransaction = conn.BeginTransaction() 
      If cbInsertInExistingGroup.Checked Then 
       If gvExistingGroups.SelectedValue IsNot Nothing Then 
        _newGroupID = gvExistingGroups.SelectedRow.Cells(1).Text 
       Else 
        pnlMessage.Visible = True 
        pnlMessage.BackColor = Drawing.Color.Red 
        lblMessage.ForeColor = Drawing.Color.White 
        lblMessage.Font.Bold = True 
        lblMessage.Text = "Select a Group" 
        Exit Sub 
       End If 
      Else 
       _newGroupID = BLL.getNewGroupIDfromCategoryGroups 
       db.ExecuteNonQuery(cmdInsertGroup, trans) 

      End If 

      For i = 0 To ds.Tables(0).Rows.Count - 1 
       ds.Tables(0).Rows(i).Item(0) = _newAttributeID 
       ds.Tables(0).Rows(i).Item(1) = _newGroupID 
       _newAttributeID = _newAttributeID + 1 
      Next 
      ' Clean up objects. 
      objConn.Close() 
      'Dim db As SqlDatabase = Connection.connection 
      Dim sqlBulk As New SqlBulkCopy(conn, SqlBulkCopyOptions.Default, trans) 
      sqlBulk.DestinationTableName = "GroupAttributes" 
      sqlBulk.WriteToServer(ds.Tables(0)) 

      trans.Commit() ' commit the transaction 
      pnlMessage.Visible = True 
      pnlMessage.BackColor = Drawing.Color.Green 
      lblMessage.ForeColor = Drawing.Color.White 
      lblMessage.Font.Bold = True 
      lblMessage.Text = "Successfully Uploaded" 
      'Response.Redirect("~/Admin/AddSpecifications.aspx?id=" & Request.QueryString(0)) 
     Else 
      pnlMessage.Visible = True 
      pnlMessage.BackColor = Drawing.Color.Red 
      lblMessage.ForeColor = Drawing.Color.White 
      lblMessage.Font.Bold = True 
      lblMessage.Text = "Select an Excel File" 
      'Response.Write("") 
     End If 
     Catch ex As Exception 
     trans.Rollback() ' rollback the transaction 
     pnlMessage.BackColor = Drawing.Color.Red 
     lblMessage.ForeColor = Drawing.Color.White 
     lblMessage.Font.Bold = True 
     lblMessage.Text = "Some Error Occured" 
     End Try 
    End Using 
End Sub 
+0

請處理所有不再使用的對象,如:DS,objConn,.... – jainvikram444

回答

1

你的代碼是有點複雜可循,但在該塊退出小組而不關閉連接objConn

If cbInsertInExistingGroup.Checked Then 
    If gvExistingGroups.SelectedValue IsNot Nothing Then 
     _newGroupID = gvExistingGroups.SelectedRow.Cells(1).Text 
    Else 
     pnlMessage.Visible = True 
     pnlMessage.BackColor = Drawing.Color.Red 
     lblMessage.ForeColor = Drawing.Color.White 
     lblMessage.Font.Bold = True 
     lblMessage.Text = "Select a Group" 
     Exit Sub 
End If 

你確實應該嘗試重構這個龐大的代碼塊在更小的單位。通過這種方式,您可以使用Using語句正確處理Disposable對象,如OleDbConnection,OleDbAdapter,OleDbCommand ....

+0

用你的建議更新了問題。謝謝 – Monodeep

+2

@Monodeep改變這個建議的問題基礎是不合邏輯的。你必須做的是投票並接受,如果這是這個問題。給這個人一個調試你的代碼和搜索錯誤的功勞。 – Aristos

+0

好的..我的壞..回到v 1.0 ;-)爲什麼投票? – Monodeep