public partial class GridView : System.Web.UI.Page
{
SqlConnection Con = new SqlConnection(@"Server=yourservername;Database=pubs;uid=userid;password=pwd;");
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
gv.DataSource = GetData();
gv.DataBind();
}public DataTable GetData()
{
da = new SqlDataAdapter("Select * from authors", Con);
da.Fill(dt);
return dt;
}protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
string stExp = e.SortExpression;
string stDir = string.Empty;
if (StDir == SortDirection.Ascending)
{
StDir = SortDirection.Descending;
stDir= "DESC";
}
else
{
StDir = SortDirection.Ascending;
stDir="ASC";
}
DataTable dt = GetData();
DataView dv = new DataView(dt);
dv.Sort = e.SortExpression + " " + stDir;
gv.DataSource = dv;
gv.DataBind();
}
public SortDirection StDir
{
get
{
if (ViewState["sortDirection"] == null)
{
ViewState["sortDirection"] = SortDirection.Ascending;
}return (SortDirection)ViewState["sortDirection"];
}
set
{
ViewState["sortDirection"] = value;
}
}
}
Wednesday, February 27, 2008
How To Sort GridView Columns
Tuesday, February 26, 2008
How To Create A Simple Shared Assembly in .NET
STEP 1 :
Start - > Microsoft Visual Studio 2005 - > Create Project - >New Project DialogBox - >Select
Class Library (Visual C#) add code in Your class file then complie it.
STEP 2:
Start - > Microsoft Visual Studio 2005 - > Visual Studio Tools -> Visual Studio 2005
Command Prompt
move on to project folder location
For Example:
If You Create A Project By Name Example, Your Project Saved in the following location
E:\Example\Example\bin\Debug> ,in Visual Studio 2005 Command Prompt type the following
E:>Cd E:\Example\Example\bin\Debug
create shared name file for your dll by typing the follwing line
E:\Example\Example\bin\Debug> sn -k userdefinedname.key
Example=E:\Example\Example\bin\Debug> sn -k Example.key
The Key Will Be Created Within Debug Folder in the name Example.key
STEP 3:
Goto project solution properties
Click "Signing" Tab (First From Buttom).
Check Sign the assembly
Choose a Strong Name Key File dropdown , then select browse "Example.key", after going to
Debug Folder,in the Select File Dialog Select All Files(File TYpe).
save and then Compile the project
STEP 4:
Start - > Microsoft Visual Studio 2005 - > Visual Studio Tools -> Visual Studio 2005
Command Prompt Example=E:\Example\Example\bin\Debug> gacutil /i .dll
STEP 5 :
After hosting the assembly just go to WINNT\Assembly folder and you will find your
assembly listed there
Thursday, February 21, 2008
Create Stored Procedure - What Is Stored Procedure Part - II
CREATE TABLE BOOK
(
iROWID INT IDENTITY(1,1) PRIMARY KEY,
vBOOKNAME VARCHAR(20),
vAUTHOR VARCHAR(30),
vPUBLICATION VARCHAR(50),
vEDITION VARCHAR(20),
vPRICE NUMERIC(5,2)
)
CreatingProcedure For Inserting Data Into The Table
CREATE PROC PROC_ADD_BOOK
(
//declaration Part
@vBookName VARCHAR(20),
@vAUTHOR VARCHAR(30),
@vPUBLICATION VARCHAR(50),
@vEDITION VARCHAR(20),
@vPRICE NUMERIC(5,2)
)AS
BEGIN
INSERT INTO BOOK(vBOOKNAME,vAUTHOR,vPUBLICATION,vEDITION,vPRICE)
VALUES (@vBookName,@vAUTHOR,@vPUBLICATION,@vEDITION,@vPRICE)
END
Executing The Procedure
EXEC PROC_ADD_BOOK 'XML & ASP.NET','KRIK ALLAN EVANS','PEARSON EDUCATION','2002','555'
Wednesday, February 20, 2008
What Is Stored Procedure Part - I
Stored procedures have a segment of code which contains declarative or procedural SQL statement. A stored procedure is resided in the catalog of the database, we can invoke (call) it from a program, stored procedure or even from a trigger.
Stored Procedure contains sql statements like insert, update and delete. In addition stored procedure supports If and while statements.
Advantages:
Once we created Stored Procedure then it can be reused over and over again by multiple applications.
Stored Procedure increase the performance of the application because once it compiled successfully then it’s stored in database catalog. When applications call them, they generally execute faster when compared with un-compiled SQL Statements which are sent from the applications.
Network Traffic between application server and database server is also signification reduced when compared with un-compiled SQL Statements which are sent from the applications.
SyntaxCREATE PROC [ EDURE ] [ owner. ] procedure_name [ ; number ]
[ { @parameter data_type }
[ VARYING ] [ = default ] [ OUTPUT ]
] [ ,...n ]
[ WITH
{ RECOMPILE ENCRYPTION RECOMPILE , ENCRYPTION } ]
[ FOR REPLICATION ]
AS sql_statement [ ...n ]
owner:
Is the name of the user ID that owns the stored procedure. owner must be either the name of the current user or the name of a role that a current user is a member.
Is the name of the new stored procedure, procedure name cannot exceed 128 characters.
Is a parameter in the procedure. One or more parameters can be declared in a CREATE PROCEDURE statement. The value of each declared parameter must be supplied by the user when the procedure is .A stored procedure can have a maximum of 2,100 parameters. Specify a parameter name using an at sign (@) as the first character.
OUTPUT:
Indicates that the parameter is a return parameter.
;number:
Is an optional integer used to group procedures of the same name so they can be dropped together with a single DROP PROCEDURE statement.