2012-09-29 2 views
0

이 오류가 발생합니다 System.OutOfMemoryException : 'System.OutOfMemoryException'형식의 예외가 throw되었습니다. 왜? 친절하게 도와주세요. 이 오류가 발생합니다 (로컬 컴퓨터가 아닌 웹 사이트를 온라인으로 호스팅 할 때만).메모리 누수가 발생할 가능성이 있습니까?

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 문을 사용하여 OleDbConnection, OleDbAdapter, OleDbCommand와 같은 Disposable 객체를 올바르게 처분 할 수 있습니다.

+0

과 같이 오래 사용하지 않는 모든 객체를 처리하십시오. 제안 사항과 함께 질문을 업데이트했습니다. 감사합니다 – Monodeep

+2

@ Monodep이 제안에 대한 질문 기지를 변경하는 것은 논리적이지 않습니다. 당신이해야 할 일은 여기에 문제가 있다면 투표하고 수락하는 것입니다. 그 사람에게 코드를 디버그하고 오류를 찾아내는 크레딧을줍니다. – Aristos

+0

오케이. 내 나쁜 ... v 1.0로 돌아 가기 ;-) 왜 투표가 실패 했습니까? 귀하의 질문이 기사를 읽고 실제로 다른 도움이되지 않고, 너무 프로젝트에 지역화되어 @Monodeep – Monodeep

관련 문제