NEWS

Friday, May 21, 2010

rting GridView to Excel Problem When Using Web UserControl

This article explains how you can export your GridView control to Excel files. If you are working with web user control. Suppose if you are having your gridview in your User Control and you are trying to export the gridview data to some other format some thing like this

protected void _btnReports_Click(object sender, EventArgs e)

{

Response.Clear();

Response.Write(@”<!DOCTYPE HTML PUBLIC “”-//W3C//DTD HTML 4.0 Transitional//EN”">”);

Response.AddHeader(“content-disposition”, “inline;filename=search_topicarea.doc”);


Response.Charset = “”;

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = “application/vnd.doc”;

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

//System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

//Render(htmlWrite);

_gvViewresults.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

}

And adding a little piece of code like override the Page.VerifyRenderingInServerForm method as shown below

public override void VerifyRenderingInServerForm(Control control)

{

// Confirms that an HtmlForm control is rendered for the

//specified ASP.NET server control at run time.

}

But this works in aspx page but you cannot override the Page.VerifyRenderingInServerForm method in the web user control.

In order to solve the above problem:

1. Create one aspx page

2. Then drag the web user control in to this aspx page but your grid view and the button click events will be in the web user control only.

3. Then in the aspx page in the Page directives tag just add EnableEventValidation=”false”.

4. In the code behind file i.e. aspx.cs file now override the Page.VerifyRenderingInServerForm method.

5. This works.


Sample code is given below:

<%@ Page Language=”C#” MasterPageFile=”~/Masters/abc.master” AutoEventWireup=”true”

CodeFile=”create.aspx.cs” Inherits=”abc_ create “ EnableEventValidation=”false”

Title=”create” %>


<%@ Register Src=”../UserControls/View.ascx” TagName=”View” TagPrefix=”uc1″ %>


<%@ Register Assembly=”AjaxControlToolkit” Namespace=”AjaxControlToolkit” TagPrefix=”EA” %>


:Content ID=”Content1″ ContentPlaceHolderID=”MainContent” runat=”Server”>


h2 class=”blue”>


View Pending Enquiries


h2>


p>


uc1: View id=”_View” runat=”server”>


uc1: View ></p>

asp:Content>

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using EA.SEDB.BO.ScienceEnquiriesDBTableAdapters;

using EA.SEDB.BO;

using EA.SEDB.BS;

public partial class abc_view : System.Web.UI.Page

{


#region Declare Variables Start


Abc.UsersDataTable UsersDT = null;


string userName = string.Empty;


int loggedInUserId;


#endregion Declare Variables End


#region PageLoad Start


protected void Page_Load(object sender, EventArgs e)


{


userName = HttpContext.Current.User.Identity.Name;


if (!Page.IsPostBack)


{


if (!string.IsNullOrEmpty(userName))


{


UsersDT = Administration.GetUserDetailsByUsername(userName);


if (UsersDT.Rows.Count > 0)


{


loggedInUserId = UsersDT[0].UserId;


}


}


if (HttpContext.Current.User.Identity.IsAuthenticated)


{


if (Roles.IsUserInRole(ConfigurationManager.AppSettings["Managers"]))


{


_ViewEnquiries.ViewEnquiries_ForManager(loggedInUserId, ConfigurationManager.AppSettings["AssignTo"].ToString());


}


else if (Roles.IsUserInRole(ConfigurationManager.AppSettings["TeamMembers"]))


{


_ViewEnquiries.ViewEnquiries_ForTeamMember(loggedInUserId, ConfigurationManager.AppSettings["Assign"].ToString());


}


else


{


Response.Redirect(“~/UnauthorisedPage.aspx”);


}


}


else


{


Response.Redirect(“~/UnauthorisedPage.aspx”);


}


}


}


public override void VerifyRenderingInServerForm(Control control)


{


// Confirms that an HtmlForm control is rendered for the


//specified ASP.NET server control at run time.


}


#endregion PageLoad End


}


Happy Coding…….

reference: http://www.trekinnovations.com/2008/12/exporting-gridview-to-excel-problem-when-using-web-usercontrol/

Tuesday, May 18, 2010

GoF patterns summary for reference






























































































































Creational


Abstract Factory


Provide an interface for creating families of related or dependent objects without specifying their concrete classes.


 


Builder


Separate the construction of a complex object from its representation so that the same construction process can create different representations.


 


Factory Method


Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.


 


Prototype


Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.


 


Singleton


Ensure a class only has one instance, and provide a global point of access to it.


 


Structural


Adapter


Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.


 


Bridge


Decouple an abstraction from its implementation so that the two can vary independently.


 


Composite


Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.


 


Decorator


Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.


 


Façade


Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.


 


Flyweight


Use sharing to support large numbers of fine-grained objects efficiently.


 


Proxy


Provide a surrogate or placeholder for another object to control access to it.


 


 


Behavioural


Chain Of Responsibility


Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.


 


Command


Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.


 


Interpreter


Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.


 


Iterator


Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.


 


Mediator


Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.


 


Memento


Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.


 


Observer


Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.


 


State


Allow an object to alter its behaviour when it’s internal state changes. The object will appear to change its class.


 


Strategy


Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.


 


Template Method


Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.


 


Visitor


Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.


 


 


 

Monday, May 17, 2010

Top 5 SQL System stored procedures every developer should know

There are a number of documented and undocumented system stored procedures in MS SQL Server that are helpful for every web developer working with databases. From the developer’s perspective, here’s a list of 5 System stored procedures that are my favorite.

1. sp_help

Purpose

sp_help gives information about database objects. If you wanted to quickly know the structure of a table but are too lazy to look for the schema diagram or to dig for the table you are interested in within the Object explorer, sp_help is here to help

Syntax

It can be used without parameters to get the information of objects in the database.
sp_help

2. sp_helptext

Purpose

sp_help gives definition information of objects such as system stored procedures, user defined stored procedures, user defined functions, triggers etc.

Syntax

sp_helptext

3. sp_MSforeachtable

Purpose

Caution – This is an undocument stored procedure and should not be relied on. It is not listed in SQL BOL and should be used at your own risk.

This is a very useful stored procedure for executing a command for ALL the tables in the database. Say you wanted to get the number of rows in all the tables in your database, you could write:

EXEC sp_MSforeachtable 'SELECT ''?'', COUNT(*) FROM ?'

The literal ? is used as a token to replace the table name.
For each table in the database, it would list the table name and the number of rows in that table.

4. sp_depends

Purpose

Ever wanted to make a change to a table but were not sure what other objects are dependent on this table? There could be views or stored procedures that could break due to this change. In situations like this, sp_depends come to the rescue.

Syntax

sp_depends

5. sp_spaceused

Purpose

This is a simple stored procedure that gives information on the size of the database or the database objects

Syntax

If it is used without parameters, it would return the database information
sp_spaceused

Monday, May 10, 2010

Design Patterns vs Design Principles (SOLID)

I was reading the other day a discussion on Design Patterns versus Design Principles called SOLID.

For those who are not aware of this, SOLID is an acronym for the first 5 principles of object-oriented design:


SRP The Single Responsibility Principle: -- a class should have one, and only one, reason to change.
OCP The Open Closed Principle: -- you should be able to extend a class's behavior, without modifying it.
LSP The Liskov Substitution Principle: -- derived classes must be substitutable for their base classes.
ISP The Interface Segregation Principle: -- make fine grained interfaces that are client specific.
DIP The Dependency Inversion Principle -- depend on abstractions not on concrete implementations.
You'll find a fair amount of information on the web on these principles.

It seems to me that you need a good grasp of the SOLID principles before you're ready to tackle Design Patterns (in more of an Architect role).

http://www.davesquared.net/2009/01/introduction-to-solid-principles-of-oo.html