locked
Memory Leak C# ASP.NET WebApp RRS feed

  • Question

  • I created a web application that consists of a table in a update panel. I have to dynamically change the text, text color, and background color of distinct cells in the table based on values that are updated to a table in a SQL server DB every three seconds. To do this I am dynamically creating the table with c#. Everything is working perfectly except the thin client used to display the WebApp will lock up after running for several hours. It appears to be a memory leak, but I am not sure. Is there anyhting in the following code that could cause issues?

    using

     

    System;

    using

     

    System.Configuration;

    using

     

    System.Data;

    using

     

    System.Linq;

    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;

    using

     

    System.Xml.Linq;

    using

     

    System.Data.SqlClient;

    using

     

    System.Drawing;

    public

     

    partial class _Default : System.Web.UI.Page

    {

    //begin class

     

    protected void Page_Load(object sender, EventArgs e)

    {

    }

     

    protected void Timer1_Tick(object sender, EventArgs e)//Event that occurs every three seconds.

    {

    //begin timer

     

    DateTime dtn = DateTime.Now;//Sets time and date to variable dtn.

    Label1.Text = dtn.ToString();

    //Sets time and date to text property of the label.

     

    //if (Request.QueryString["ID"] != null)//Checks to see if the ID varable in the URL is missing.

     

    // try

    {

    // begin if

     

    int x, y;

     

    switch (Request.QueryString["ID"])

    {

    //begin switch

     

    case "1": x = 2; y = 1;

     

    break;

     

    case "2": x = 3; y = 2;

     

    break;

     

    case "3": x = 4; y = 3;

     

    break;

     

    default: goto case "1";

    }

    //end switch

     

    string connString = @"Data Source=DATASOURCEDB\DATASOURCEDB;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=Password";

     

    string[] sql = new string[2] { @"SELECT * from dbo.Tbl_Man_Console" + x, @"SELECT * from dbo.tblVAndOvConfig where asset_id = " + y };

     

    SqlConnection conn = new SqlConnection(connString);//Creates a new SQL connection.

    conn.Open();

    //Opens the SQL connection.

     

    DataTable dt = new DataTable();//Creates new data table dt.

     

    SqlDataAdapter adapter = new SqlDataAdapter(sql[0], conn);//Creates new sql adapter and assigns a query to it.

    adapter.Fill(dt);

    //Fills the datatable with the data pulled back from the query.

    conn.Close();

    conn.Open();

     

    DataTable dtCnf = new DataTable();//Creates new data table dtCnf.

     

    SqlDataAdapter adapterCnf = new SqlDataAdapter(sql[1], conn);//Creates new sql adapter and assigns a query to it.

    adapterCnf.Fill(dtCnf);

    //Fills the datatable with the data pulled back from the query.

    conn.Close();

     

    //Declare variables.

     

    int rowCnt, rowCtr, cellCtr, cellCnt, cellColor, textColor, i, cellVis;

    cellVis = 0; cellCtr = 1; i = 0; rowCtr = 1;

    cellCnt =

    Convert.ToInt32(dtCnf.Rows[0][1]); rowCnt = Convert.ToInt32(dtCnf.Rows[0][0]);//Set cell count.

     

    for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)//for loop creates a table row until the number of rows = the row count variable set by the user (rowCnt).

    {

    //begin for row

     

    TableRow tRow = new TableRow();

    tblDisplay.Rows.Add(tRow);

     

    for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)//for loop creates a number of cells = the number set by the user (cellCnt)for each row.

    {

    //begin for cell

     

    TableCell tCell = new TableCell();

    i = ((((rowCtr - 1) * cellCnt) + cellCtr)) - 1;

    tCell.BorderWidth = 2;

    //sets border width.

    tCell.BorderColor =

    Color.Black;//sets border color.

    tCell.Text =

    Convert.ToString(dt.Rows[i][2]);//sets text of each cell based on user input.

    cellColor =

    Convert.ToInt32(dt.Rows[i][3]);//sets Cell color for each cell based on user input.

    tCell.Height =

    Convert.ToInt32(dt.Rows[i][5]);//sets Cell height for each cell based on user input.

    tCell.Width =

    Convert.ToInt32(dt.Rows[i][4]);//sets Cell width for each cell based on the users input.

    tCell.Font.Size =

    Convert.ToInt32(dt.Rows[i][8]);//sets font size of each cell based on the users input.

    textColor =

    Convert.ToInt32(dt.Rows[i][0]);//Sets text color for each cell based on the users input.

    cellVis =

    Convert.ToInt32(dt.Rows[i][6]);//sets visibility of each cell cell based on the users input.

    tCell.HorizontalAlign =

    HorizontalAlign.Center;

     

    switch (textColor)

    {

    //begin switch

     

    case 0: tCell.ForeColor = Color.Tomato;

     

    break;

     

    case 1: tCell.ForeColor = Color.Yellow;

     

    break;

     

    case 2: tCell.ForeColor = Color.White;

     

    break;

     

    case 3: tCell.ForeColor = Color.Green;

     

    break;

     

    case 4: tCell.ForeColor = Color.Red;

     

    break;

     

    case 5: tCell.ForeColor = Color.Blue;

     

    break;

     

    case 6: tCell.ForeColor = Color.Goldenrod;

     

    break;

     

    case 7: tCell.ForeColor = Color.Firebrick;

     

    break;

     

    default: tCell.ForeColor = Color.Black;

     

    break;

    }

    //end switch

     

    switch (cellColor)

    {

    //begin switch

     

    case 0: tCell.BackColor = Color.Tomato;

     

    break;

     

    case 1: tCell.BackColor = Color.Yellow;

     

    break;

     

    case 2: tCell.BackColor = Color.LightGray;

     

    break;

     

    case 3: tCell.BackColor = Color.Green;

     

    break;

     

    case 4: tCell.BackColor = Color.Red;

     

    break;

     

    case 5: tCell.BackColor = Color.Blue;

     

    break;

     

    case 6: tCell.BackColor = Color.Goldenrod;

     

    break;

     

    case 7: tCell.BackColor = Color.Firebrick;

     

    break;

     

    default: tCell.BackColor = Color.Lime;

     

    break;

    }

    //end switch

    tRow.Cells.Add(tCell);

    tCell.Dispose();

    }

    //end for cell

    tRow.Dispose();

    }

    //end for row

     

    dt.Dispose();

    dtCnf.Dispose();

    }

    //end if 2

     

    //finally

     

    // {

     

    // Label1.Text = "Error";

     

    // }

    }

    //end timer

     

     

    }

    //end class

     

     

     

     

     

     

     

     

    • Moved by Figo Fei Monday, July 6, 2009 4:00 AM (From:Visual C# General)
    Thursday, July 2, 2009 7:27 PM

Answers

  • Hi msurratt76

    You may get forums.asp.net for ASP.NET questions.

    Thanks.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
    • Marked as answer by msurratt76 Thursday, July 30, 2009 5:42 PM
    Monday, July 6, 2009 4:00 AM

All replies

  • I don't think you are disposing your data adapters and connection properly.


    Homepage | Articles | Blog
    Thursday, July 2, 2009 7:52 PM
  • Agreed!
    You may want to put all your IDisposable variables in 'using' statement.
    Friday, July 3, 2009 1:20 AM
  • AS long as my observation is concered you can use SQLConnection variable in using box like below.

    using (SqlConnection conn = new SqlConnection(connString))
    {
         //write logic here
    }

    Dont forget to click “Mark as Answer” on the post that helped you.
    Saturday, July 4, 2009 11:28 AM
  • How do you know there is a memory leak?

    MM
    chanmm
    Saturday, July 4, 2009 3:56 PM
  • Hi msurratt76

    You may get forums.asp.net for ASP.NET questions.

    Thanks.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
    • Marked as answer by msurratt76 Thursday, July 30, 2009 5:42 PM
    Monday, July 6, 2009 4:00 AM