Jun122008

Accesibility , .NET , Section 508

If you work in the states, and do a project for the government, then your projects should be accessible according section 508 guideline.  So if you are doing a web site, what does that mean?

  • If you have an image on the page, the image tag should have an alt attribute describing what the image is about ( .net puts the alt attribute in the code but it is blank )
  • if you have complex table,you should define scope and table header (gridview supports this)
  • if you put a video, you should have subtitles
  • if you have a form and labels regarding to textboxes, you should use label for to associate labels to textboxes, in asp.net you have a label, use associtedcontrolid property
  • dont imitate the header using css
  • make sure ur site is visible when you disable the css

these are the common problems i see in the web sites but not all :)

good luck making ur site accessible



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Jun122008

Connection Pool is still full!!

Ok, i told abot our problem yesterday. For some reason, the .net framework was throwing an error to event logs, saying that connection pool is full, and it couldnt connect to the database so the site was down. We rebooted the servers, restarted iis etc, this solved the problem for only a few hours. Of course our first intention is to go to the code and try to find the connection leaks, and fix them, but in the mean time what are we going to do ? when the site isnt working, your first goal should be making the site work, not coming up with the best solution.

My first dirty, fast solution is turning off the connection pool. To do this, open your web.config file, go to connectionStrings node, and inside the connection string properties add: "Pooling=false;". This will turn off all the pooling, I did this, and site came back alive, i was suspecting a slow web site, as now there is a hit on the database everytime the page loads, a connection is created everytime. Our daily hits is like between 25-35000. The default page is hitting the database 4 times to query 4 different tables, and guess what we dont have caching. The simple calculation shows that we hit the sql server 120,000 times/day just to display the home  page. If there is a leak in the connections, it is pretty costly. So anyways, we turned off the pooling, and next thing is, i asked the server admins to monitor the sql server resources as we might be using lots of cpu and ram for not using pooling. They told me after a day that, sql server was ok, no performans problems :) sweet!!, so i turned off connection pooling, and site is working without putting a load on the sql server. Then why do we have pooling enabled ? :) probably it ll be even faster once we fixed the leaks. 

We found out that one of our programmers do not open the connections inside a try catch block, also not disposing the sqldatareader. So we are fixing those (see yesterday's blog about the solution), and also we started to implement a caching mechanism so that the first time page is loaded, we will read the data from the database, put it in the cache. I ll measure the performance, before and after and will let you know what happens :)



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Jun112008

Connection pool is full

Today, we got a call from one of students, complaining that the site doesnt work, so i tried to load the page myself, nope, it didnt work. I connected to the server using remote desktop connection, and everything was working fine. At first i really didnt understand the problem, so i opened the event viewer to see the event logs. The first thing i saw is a warning message saying "Connection pool is full, and can not connect to pool..." , as someone who is using google more than anything else during a day, i opened my browser, and typed the warning into the google. The sites i have found was talking about if you open a connection to the database, and before you close the connection an exception is thrown and u dont use try catch, u have a wasted connection now. And you have too many wasted connection, the pool will be full and you cant make another connection. so watch this :

[code:c#]

private bool DoSomethingWithMyDatabase()
{
     SqlConnection conn = new SqlConnection...
     ....
     .....
    conn.Dispose();

[/code] 

if you have a code like above, and an exception is thrown between new SqlConnection and conn.Dispose, you dispose function is never called, and you have one wasted connection now, and if this happens too much in a short time, your pool is going to be full, and your application will stop working.  Ok now how do fix it ?

The first dirty patch, is opening up iis manager, and restarting the web application pool and waiting for the next connection full error.

The real patch is, changing ur code, and putting try catch blocks or using block such as:

[code:c#]

 private bool DoSomethingWithMyDatabase()
{
     SqlConnection conn = new SqlConnection...
     try{
      ....
     .....
     }
     finally{
    conn.Dispose();
    }

[/code] 

so even if an exception is thrown, use can safely dispose ur connection, and ur connection will be returned back to the pool. Another solution is : 

 

[code:c#]

 private bool DoSomethingWithMyDatabase()
{
   using(SqlConnection conn = new SqlConnection())
    {

     }

[/code] 

 


Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Jun042008

System.Web.Caching.Cache and error on my page

I am working on this web site, that does a lot of database queries, so i decided to use some caching mechanism here.

I do have a static function which reads the database, create a collection out the result and returns it to the caller, below is the simple code :

[code:c#]

  private static List<WebSite> GetWebSites()
    {
        List<WebSite> WebSites = (List<WebSite>)Cache["WebSites"];
        if (WebSites == null)
        {
            DatabaseAccess db = new DatabaseAccess(ConfigurationManager.ConnectionStrings["CSUWebSitesConnectionString"].ConnectionString);
            WebSitesTB webSitesTB = new WebSitesTB(db);
            WebSites = webSitesTB.GetAllWebSites();
            Cache["WebSites"] = WebSites;
        }
     ......

[/code]

but i got the error:  Error    39    An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Cache.get'    D:\WORK PROJECTS\CSUSBWebSitesApp\WebSitesApp\Administration\WebSites.aspx.cs    43    49    D:\...\WebSitesApp\

Can you guess what the problem is ? 

The problem is: Cache is a property of the class Page, and i am trying to access a property of page class, from a static function ; however this is not possible. What you should do is, change Cache to HttpContext.Current.Cache

 



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Jun032008

Where does linq sit at?

I am still trying to understand what does Linq sit at?

if i have DAL,BAL, and PL, which layer will have linq quries?

it could be in the dal, and accesses the sql server and returns collection of objects, however

it could also be in bal and query the returned collection using linq

it could be in pl to sort grid etc



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

May222008

coding in my dreams?

i dont remember my dreams; no seriously i dont. I cant really say i dont dream while i am asleep, cause i dont remember if any :)

So, my wife; a few times told me that i talked to her when i was sleeping ;) probably i was dreaming. She told me yesterday i was telling her; "these benchmarks are very important for performance".  I have no idea what those benchmarks were, but it was a proof for me that i dream :), interesting that i dream about coding ; a few todays ago, my wife this time told me , i was talking about some algorithm.

I wish i could pop a usb memory in my ear (hey i said ear ok ? ), and record my dreams; that would be fun 



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 1 Responses

May212008

disable debugging in ur server

Ok we all know we should change debug mode to false in our web config when it is time to upload the site to the server; and guess what, we might forget it right ?

I just learnt the easiest way :) of disabling debug to false in the web server. Go to machine.config file in the web server, find system.web section and add <deployment retail="true"/> 

is that it or what :) 



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

May202008

I passed 70-431

i was studying for the MS 70-431 Implemantation and Administration Sql Server 2005 exam in the last 4 weeks, besides the projects @work, this kept me really busy. Even though i use sql server 2005 in work daily, you never what might microsoft ask you :), so i bough the ms press book for the exam, and went through each chapter,  and today i passed it :) 

Now as i didnt get the email from microsoft yet, i am not %100 sure about what certifications i will get by passing this exam, but according to microsoft's web site, i am now MCP, and MCTS. my next target is asp.net web development certification track, let see :)

 



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 1 Responses

May182008

XML, Linq, GridView, Label

I had a very small project, where there would be page that lists events, another page that adds events. I didnt want to have a database for this simple pages, so i decided to go on with xml file. My xml file structure is basically like below: 


Now i need a pagethat will list all the events (hopefully ordering them according to When attribute which is a datetime. so i created a page put a label on the page, and open the code behind file.

What i want to do is, open the xml file, order them, and dump the data into the label, so i decided to use linq, as i am learning this new technique. Below is the simple code for this, linq made it so easy for me to parse the xml; unbelievable:

 


BTW you could do the exact same thing with xmldatasource and xpath, however; i am not sure with ordering :), ordering wont be that easy i guess with xmldatasource, here is the page that does the above listing with xmldatasource and gridview and xpath:

 



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 2 Responses

May012008

Change DB Access to Interface

As i mentioned in my previous posting, i am trying to seperate the modules in my projects so i can test every single code. I am new to TDD so i meet a lot of challenges during the adoptation. One of them is the database access, how will i unit test a class or a function where these is a database dependency.  i decided to create a generic database access class, which implements an interface that could be mock out.

In the database side, i always use stored procedures to do actions, and my stored procedures either returns an integer value (if it doesnt return anything like delete from..., consider it returns 1),  or a DataTable such as select * from table. So i can easily write an interface such as:

[code:c#]
public interface IExecuteProcedure
    {
        DataTable GetDataTable(string StoredProcedureName, List<SqlParameter> Parameters);
        int ExecuteStoreProcedure(string StoredProcedureName, List<SqlParameter> Parameters);
    }
[/code]

So GetDataTable runs a stored procedure with a list of parameters given , and returns a DataTable, ExecuteStoreProcedure returns int.  A database access class could follow a pattern like below to implement this interface:

[code:c#]
public class DatabaseAccess:IExecuteProcedure
    {
        private readonly string ConnectionString_;

       
        public DatabaseAccess(string ConnectionString)
        {
            this.ConnectionString_ = ConnectionString;
        }
       
        public DataTable GetDataTable(string StoredProcedureName, List<SqlParameter> Parameters)
        {
            SqlConnection conn = new SqlConnection(ConnectionString_);
            SqlCommand cmd = new SqlCommand(StoredProcedureName, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter param in Parameters)
            {
                cmd.Parameters.Add(param);
            }
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
            }
            catch (Exception)
            {
                dt.Clear();
            }

            return dt;
        }

        #region IExecuteProcedure Members

        public int ExecuteStoreProcedure(string StoredProcedureName, List<SqlParameter> Parameters)
        {
            SqlConnection conn = new SqlConnection(ConnectionString_);
            SqlCommand cmd = new SqlCommand(StoredProcedureName, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter param in Parameters)
            {
                cmd.Parameters.Add(param);
            }
            int iReturn = 0;
            try
            {
                conn.Open();
                cmd.ExecuteScalar();

                for (int i = 0; i < Parameters.Count; i++)
                {
                    if (Parameters[i].Direction == ParameterDirection.Output)
                    {
                        Parameters[i].Value = cmd.Parameters[Parameters[i].ParameterName].Value;
                        iReturn = Int32.Parse(Parameters[i].Value.ToString());
                    }
                }
            }
            catch(Exception ex)
            {
                iReturn = Int32.MinValue;
            }
            finally
            {
                conn.Dispose();
            }
            return iReturn;
        }

        #endregion

       
    }
[/code]

So the database access class implements the interface, and returns the appropriate values. Now in my next posting we will see how we can use this class to access the database, but also we will see how we can inject some code, and change the behaviour without changing the structure.

 

 



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses