Link to home
Start Free TrialLog in
Avatar of dotsandcoms
dotsandcoms

asked on

System.OutOfMemoryException

Hi folks,

Whenever i am deleting the all the records from single table then i am getting the below error

"Exception of type 'System.OutOfMemoryException' was thrown".

I am using the below query to delete the records:

"DELETE * FROM tablename"

Any help would be appreciated.

Many Thanks
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Hi dotsandcoms,
My quick guess is this might related to crash library(dll) that using same function? Can you post other related code for further inspection?
Avatar of dotsandcoms
dotsandcoms

ASKER

Hi,

As requested i am attaching the code file for your reference.


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
public partial class ViewAllRecipient : System.Web.UI.Page
{
    PowerGeneralFunction objGen;
    DataSet ds;
    string strQuery = "";
 
    protected void Page_Load(object sender, EventArgs e)
    {
        lblmsg.Text = "";
        Label1.Text = "";
        tab3.Visible = false;
        Tabok.Visible = false;
        if (Request.QueryString["msg"] != null)
        {
            Tabok.Visible = true;
            Label1.Visible = true;
            Label1.Text = Request.QueryString["msg"];
            tab3.Visible = false;
        }
 
        if (!Page.IsPostBack)
        {
 
            this.Filling_Gridview();
            this.Filling_Group();
            ViewState["CheckAll"] = "0";
            Session["CHECKED_ITEMS"] = null;
            ViewState["searchcriteria"] = "0";
            
        }
    }
    private void RememberOldValues()
    {
        ArrayList categoryIDList = new ArrayList();
        int index = -1;
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (GridView1.DataKeys[row.RowIndex].Value != null)
            {
                index = (int)GridView1.DataKeys[row.RowIndex].Value;
                bool result = ((CheckBox)row.FindControl("chkBxRecipient")).Checked;
 
                // Check in the Session
                if (Session["CHECKED_ITEMS"] != null)
                    categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
                if (result)
                {
                    if (!categoryIDList.Contains(index))
                        categoryIDList.Add(index);
                }
                else
                    categoryIDList.Remove(index);
            }
            if (categoryIDList != null && categoryIDList.Count > 0)
                Session["CHECKED_ITEMS"] = categoryIDList;
        }
    }
    private void RePopulateValues()
    {
        ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
        if (categoryIDList != null && categoryIDList.Count > 0)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                int index = (int)GridView1.DataKeys[row.RowIndex].Value;
                if (categoryIDList.Contains(index))
                {
                    CheckBox myCheckBox = (CheckBox)row.FindControl("chkBxRecipient");
                    myCheckBox.Checked = true;
                }
            }
        }
    }
 
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index;
        String id;
 
        if (e.CommandName == "myEdit")
        {
            index = Convert.ToInt32(e.CommandArgument);
            id = GridView1.Rows[index].Cells[0].Text;
            Response.Redirect("editrecipient.aspx?id=" + id + "&rtflg=3");
 
        }
        else if (e.CommandName == "myDetails")
        {
            index = Convert.ToInt32(e.CommandArgument);
            id = GridView1.Rows[index].Cells[0].Text;
            Response.Redirect("recipientDetails.aspx?id=" + id + "&rtflg=3");
 
        }
 
        else if (e.CommandName == "Block")
        {
            //Response.Write(e.CommandArgument.ToString());
 
            int chvalue1 = 0;
 
            strQuery = "Select * from NewsLetterRecipients where id=" + e.CommandArgument.ToString();
            objGen = new PowerGeneralFunction();
            ds = new DataSet();
 
            try
            {
                ds = objGen.GenerateDataset(strQuery);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["Unsubscribe"].ToString() == "True")
                    {
                        chvalue1 = 0;
                    }
                    else
                    {
                        chvalue1 = 1;
                    }
 
 
 
                }
 
            }
            catch (Exception ex)
            {
                Tabok.Visible = false;
                tab3.Visible = true;
                lblmsg.Visible = true;
                lblmsg.Text = "Error : " + ex.Message;
 
            }
            finally
            {
                strQuery = "";
                ds.Dispose();
                objGen = null;
            }
 
 
 
 
            int result = 0;
            objGen = new PowerGeneralFunction();
 
            strQuery = "update NewsLetterRecipients set Unsubscribe=" + chvalue1 + " where ID = " + e.CommandArgument.ToString() + "";
            try
            {
 
                result = objGen.InsertUpdateDeleteCommand(strQuery);
                if (result > 0)
                {
 
                    tab3.Visible = false;
                    Tabok.Visible = true;
                    Label1.Visible = true;
                    Label1.Text = "Status Successfully Updated.";
 
 
                }
            }
            catch (Exception ex)
            {
                Tabok.Visible = false;
                tab3.Visible = true;
                lblmsg.Visible = true;
                lblmsg.Text = "Error : " + ex.Message;
 
            }
            finally
            {
                objGen = null;
                strQuery = "";
            }
 
            this.Filling_Gridview();
 
 
 
        }
        
 
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ViewState["CheckAll"].ToString() == "1")
        {
            RememberOldValues();
 
            GridView1.PageIndex = e.NewPageIndex;
 
            if (ViewState["searchcriteria"].ToString() == "1")
            {
                Btn_search_Click(null, null);
            }
            else
            {
                this.Filling_Gridview();
            }
            
            RePopulateValues();
        }
        else
        {
            RememberOldValues();
 
            GridView1.PageIndex = e.NewPageIndex;
            if (ViewState["searchcriteria"].ToString() == "1")
            {
                Btn_search_Click(null, null);
            }
            else
            {
                this.Filling_Gridview();
            }
 
            RePopulateValues();
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (int.Parse(Session["UserType"].ToString()) == 8)
        {
            int result = 0;
            objGen = new PowerGeneralFunction();
            GridView1.SelectedIndex = e.RowIndex;
            strQuery = "Delete from NewsLetterRecipients where ID = " + GridView1.SelectedDataKey[0].ToString() + "";
            try
            {
 
                result = objGen.InsertUpdateDeleteCommand(strQuery);
                if (result > 0)
                {
                    tab3.Visible = false;
                    Tabok.Visible = true;
                    Label1.Visible = true;
                    Label1.Text = "Recipient Deleted Successfully.";
 
                }
                else
                {
                    Tabok.Visible = false;
                    tab3.Visible = true;
                    lblmsg.Visible = true;
                    lblmsg.Text = "System cannot delete this recipient.";
                }
            }
            catch (Exception ex)
            {
                Tabok.Visible = false;
                tab3.Visible = true;
                lblmsg.Visible = true;
                lblmsg.Text = "Error : " + ex.Message;
            }
            finally
            {
                objGen = null;
                strQuery = "";
            }
 
            this.Filling_Gridview();
        }
        else
        {
            Tabok.Visible = false;
            tab3.Visible = true;
            lblmsg.Visible = true;
            lblmsg.Text = "You are not authorise to delete the data.";
        }
    }
    protected void Filling_Gridview()
    {
        Response.Write(" ");
        Response.Flush();
        strQuery = "Select *,IIF(Unsubscribe = -1,'Unsubscribe','Subscribe') AS block6 from NewsLetterRecipients order by id desc";
        objGen = new PowerGeneralFunction();
        ds = new DataSet();
 
        try
        {
            ds = objGen.GenerateDataset(strQuery);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSourceID = null;
                GridView1.DataSource = ds;
                GridView1.DataBind();
                Lbl_rc.ForeColor = System.Drawing.Color.Black;
                Lbl_rc.Font.Bold = true;
                Lbl_rc.Text = "Total no of records : " + ds.Tables[0].Rows.Count;
 
 
            }
            else
            {
 
                GridView1.DataSourceID = null;
                Lbl_rc.ForeColor = System.Drawing.Color.Black;
                Lbl_rc.Font.Bold = true;
                Lbl_rc.Text = "Record not found.";
            }
        }
        catch (Exception ex)
        {
            Tabok.Visible = false;
            tab3.Visible = true;
            lblmsg.Visible = true;
            lblmsg.Text = "Error : " + ex.Message;
 
        }
        finally
        {
            strQuery = "";
            ds.Dispose();
            objGen = null;
        }
    }
    protected void Filling_Group()
    {
        Response.Write(" ");
        Response.Flush();
        DDL_group.Items.Clear();
        strQuery = "Select * from Group_Tab order by GroupName asc";
        objGen = new PowerGeneralFunction();
        ds = new DataSet();
 
        try
        {
            ds = objGen.GenerateDataset(strQuery);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow info in ds.Tables[0].Rows)
                {
                    ListItem Lst = new ListItem();
                    Lst.Value = info["GroupID"].ToString();
                    Lst.Text = info["GroupName"].ToString();
 
 
                    DDL_group.Items.Add(Lst);
                    
                    
 
                }
            }
            else
            {
                DDL_group.Visible = false;
                
            }
        }
        catch (Exception ex)
        {
 
            tab3.Visible = true;
            lblmsg.Visible = true;
            lblmsg.Text = "Error : " + ex.Message;
 
        }
        finally
        {
            strQuery = "";
            ds.Dispose();
            objGen = null;
        }
    }
 
    protected void Btn_search_Click(object sender, ImageClickEventArgs e)
    {
 
        Response.Write(" ");
        Response.Flush();
        if (DDL_searchby.Text == "Group")
        {
            
            strQuery = "Select *,IIF(Unsubscribe = -1,'Unsubscribe','Subscribe') AS block6 from NewsLetterRecipients where id in (select ReciID from GroupRecipient where GrpID=" + DDL_group.SelectedValue + ") order by id desc";
            objGen = new PowerGeneralFunction();
            ds = new DataSet();
 
            try
            {
                ds = objGen.GenerateDataset(strQuery);
 
                if (ds.Tables[0].Rows.Count > 0)
                {
                    GridView1.Visible = true;
                    GridView1.DataSourceID = null;
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                    Lbl_rc.ForeColor = System.Drawing.Color.Black;
                    Lbl_rc.Font.Bold = true;
                    Lbl_rc.Text = "Total no of records : " + ds.Tables[0].Rows.Count;
                }
                else
                {
 
                    GridView1.DataSourceID = null;
                    GridView1.DataSource = null;
                    GridView1.DataBind();
                    GridView1.Visible = false;
                    Lbl_rc.ForeColor = System.Drawing.Color.Black;
                    Lbl_rc.Font.Bold = true;
                    Lbl_rc.Text = "Record not found.";
 
 
                }
            }
            catch (Exception ex)
            {
 
                Tabok.Visible = false;
                tab3.Visible = true;
                lblmsg.Visible = true;
                lblmsg.Text = "Error : " + ex.Message;
            }
            finally
            {
                strQuery = "";
                ds.Dispose();
                objGen = null;
            }
            ViewState["searchcriteria"] = "1";
        }
        else
        {
            string stext = Tbx_stext.Text.Replace("'", "'");
            if (stext != "")
            {
 
                if (DDL_searchby.Text == "firstname")
                {
                    strQuery = "Select *,IIF(Unsubscribe = -1,'Unsubscribe','Subscribe') AS block6 from NewsLetterRecipients where Name like '%" + stext + "%' order by id desc";
                }
                else if (DDL_searchby.Text == "lastname")
                {
                    strQuery = "Select *,IIF(Unsubscribe = -1,'Unsubscribe','Subscribe') AS block6 from NewsLetterRecipients where LastName like '%" + stext + "%' order by id desc";
                }
                else if (DDL_searchby.Text == "email")
                {
                    strQuery = "Select *,IIF(Unsubscribe = -1,'Unsubscribe','Subscribe') AS block6 from NewsLetterRecipients where Email like '%" + stext + "%' order by id desc";
                }
                else if (DDL_searchby.Text == "country")
                {
                    strQuery = "Select *,IIF(Unsubscribe = -1,'Unsubscribe','Subscribe') AS block6 from NewsLetterRecipients where Country like '%" + stext + "%' order by id desc";
                }
 
                else
                {
                    strQuery = "Select *,IIF(Unsubscribe = -1,'Unsubscribe','Subscribe') AS block6 from NewsLetterRecipients order by id desc";
 
                }
 
                objGen = new PowerGeneralFunction();
                ds = new DataSet();
 
                try
                {
                    ds = objGen.GenerateDataset(strQuery);
 
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        GridView1.Visible = true;
                        GridView1.DataSourceID = null;
                        GridView1.DataSource = ds;
                        GridView1.DataBind();
                        Lbl_rc.ForeColor = System.Drawing.Color.Black;
                        Lbl_rc.Font.Bold = true;
                        Lbl_rc.Text = "Total no of records : " + ds.Tables[0].Rows.Count;
                    }
                    else
                    {
 
                        GridView1.DataSourceID = null;
                        GridView1.DataSource = null;
                        GridView1.DataBind();
                        GridView1.Visible = false;
                        Lbl_rc.ForeColor = System.Drawing.Color.Black;
                        Lbl_rc.Font.Bold = true;
                        Lbl_rc.Text = "Record not found.";
 
 
                    }
                }
                catch (Exception ex)
                {
 
                    Tabok.Visible = false;
                    tab3.Visible = true;
                    lblmsg.Visible = true;
                    lblmsg.Text = "Error : " + ex.Message;
                }
                finally
                {
                    strQuery = "";
                    ds.Dispose();
                    objGen = null;
                }
                ViewState["searchcriteria"] = "1";
 
            }
        }
    }
    protected void IBtn_viewall_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewAllRecipient.aspx");
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ViewState["CheckAll"] = "1";
        int intPageCount = GridView1.PageCount;
        int pgIndex = GridView1.PageIndex;
        //Response.Write(pgIndex);
        //GridViewPageEventArgs e2 = new GridViewPageEventArgs(0);
        GridViewPageEventArgs e2 = new GridViewPageEventArgs(pgIndex);
        GridView1_PageIndexChanging(null, e2);
        
        for (int ctr = 0; ctr < GridView1.Rows.Count; ctr++)
        {
            GridViewRow row = GridView1.Rows[ctr];
            CheckBox chk = (CheckBox)row.FindControl("chkBxRecipient");
            chk.Checked = true;
            if (ctr == GridView1.Rows.Count - 1)
            {
                if (intPageCount > 1)
                {
                    GridViewPageEventArgs e1 = new GridViewPageEventArgs(GridView1.PageIndex + 1);
                    pgIndex++;
 
                    if (pgIndex > intPageCount)
                        break;
                    else
                    {
                        GridView1_PageIndexChanging(null, e2);
                        ctr = -1;
                    }
                }
            }
        }
        GridView1_PageIndexChanging(null, e2);
    }
    protected void IBtn_removeselected_Click(object sender, ImageClickEventArgs e)
    {
        if (int.Parse(Session["UserType"].ToString()) == 8)
        {
            string strCheckAll = "";
            int groupID = 0;
            int queryResult = 0;
 
            int intPageCount = GridView1.PageCount;
 
            int pgIndex = GridView1.PageIndex;
            GridViewPageEventArgs e2 = new GridViewPageEventArgs(pgIndex);
            GridView1_PageIndexChanging(null, e2);
            for (int ctr = 0; ctr < GridView1.Rows.Count; ctr++)
            {
                GridViewRow row = GridView1.Rows[ctr];
                string str = row.Cells[1].Text;
                CheckBox chk = (CheckBox)row.FindControl("chkBxRecipient");
                if (chk.Checked == true)
                {
                    strCheckAll += GridView1.DataKeys[row.RowIndex].Value.ToString() + ",";
                }
                if (ctr == GridView1.Rows.Count - 1)
                {
                    if (intPageCount > 1)
                    {
                        GridViewPageEventArgs e1 = new GridViewPageEventArgs(GridView1.PageIndex + 1);
                        pgIndex++;
 
                        if (pgIndex >= intPageCount)
                        {
                            break;
                        }
                        else
                        {
                            GridView1_PageIndexChanging(null, e1);
                            ctr = -1;
                        }
                    }
                }
            }
            //Response.Write(strCheckAll);
 
            if (strCheckAll.Length > 0)
            {
                strCheckAll = strCheckAll.Substring(0, strCheckAll.Length - 1);
                string[] deleteID = strCheckAll.Split(',');
                PowerGeneralFunction objGen;
                for (int ctr = 0; ctr < deleteID.Length; ctr++)
                {
                    string strDelete = "Delete from NewsLetterRecipients where ID = " + deleteID[ctr].ToString() + "";
                    objGen = new PowerGeneralFunction();
                    try
                    {
                        queryResult = objGen.InsertUpdateDeleteCommand(strDelete);
                        if (queryResult > 0)
                        {
                            tab3.Visible = false;
                            Tabok.Visible = true;
                            Label1.Visible = true;
                            Label1.Text = "Selected Recipients Deleted Successfully.";
                        }
 
                    }
                    catch (Exception ex)
                    {
                        Tabok.Visible = false;
                        tab3.Visible = true;
                        lblmsg.Visible = true;
                        lblmsg.Text = "Error : " + ex.Message;
                    }
                }
                this.Filling_Gridview();
            }
            else
            {
                Tabok.Visible = false;
                tab3.Visible = true;
                lblmsg.Visible = true;
                lblmsg.Text = "Select the recipients to delete.";
            }
        }
        else
        {
            Tabok.Visible = false;
            tab3.Visible = true;
            lblmsg.Visible = true;
            lblmsg.Text = "You are not authorise to delete the data.";
        }
        
    }
    protected void IBtn_deleteall_Click(object sender, ImageClickEventArgs e)
    {
        if (int.Parse(Session["UserType"].ToString()) == 8)
        {
            int result = 0;
            objGen = new PowerGeneralFunction();
 
            strQuery = "Delete from NewsLetterRecipients";
            try
            {
 
                result = objGen.InsertUpdateDeleteCommand(strQuery);
                if (result > 0)
                {
                    tab3.Visible = false;
                    Tabok.Visible = true;
                    Label1.Visible = true;
                    Label1.Text = "All recipient with their associated records has been deleted.";
 
                }
                else
                {
                    Tabok.Visible = false;
                    tab3.Visible = true;
                    lblmsg.Visible = true;
                    lblmsg.Text = "System cannot delete this recipient.";
                }
            }
            catch (Exception ex)
            {
                Tabok.Visible = false;
                tab3.Visible = true;
                lblmsg.Visible = true;
                lblmsg.Text = "Error : " + ex.Message;
            }
            finally
            {
                objGen = null;
                strQuery = "";
            }
 
            GridView1.DataSource = null;
            GridView1.DataSourceID = null;
            GridView1.DataBind();
            this.Filling_Gridview();
        }
        else
        {
                Tabok.Visible = false;
                tab3.Visible = true;
                lblmsg.Visible = true;
                lblmsg.Text = "You are not authorise to delete the data.";  
        }
    }
    protected void DDL_searchby_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDL_searchby.Text == "Group")
        {
            DDL_group.Visible = true;
            Tbx_stext.Visible = false;
        }
        else
        {
            DDL_group.Visible = false;
            Tbx_stext.Visible = true;
        }
    }
}

Open in new window

Is that possible to attach the custom details error that returned from .net compiler (yellow background)? Lastly, can you tell me the the line of posted code that halted due to the mentioned error?
I am getting the error at line no 648 of posted code and i am getting the error

"Exception of type 'System.OutOfMemoryException' was thrown".
Ok, can you post the code snippet that used in "InsertUpdateDeleteCommand" function? Lastly, what is the database that using in this application? Please clarify for better understanding.
as per your request, plz see the function of insertupdatedelete command

public int InsertUpdateDeleteCommand(String strQuery)
    {
        if (conn.State == ConnectionState.Open)
            conn.Close();
        conn.Open();

        //cmd = new OleDbCommand();
        cmd = new OdbcCommand();
        cmd.CommandText = strQuery;
        cmd.Connection = conn;

        try
        {
            return (queryResult = cmd.ExecuteNonQuery());
        }
        catch (Exception ex)
        {
            throw ex;

        }
        finally
        {
            cmd = null;
            conn.Close();
        }
    }
we are using the MS Access database
dotsandcoms
Does the error only return for every time you're trying to remove all records from single table? Does it happend during deleting single record from the selected table?
i am getting the error when removing all records from single table and not when deleting the single record of the selected table
ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi dotsandcoms,

Is your problem solved?