StatCounter

View My Stats

Tuesday, February 5, 2008

Reduce the Bandwidth Suffer in .NET

Reduce the Bandwidth Suffer in .NET


While using a complex control like GridView, Repeater ,DetailsView control We have to Disable the ViewState.......Otherwise it degrades the Bandwidth of your connection.

To overcome this :
Page Load
if(!Page.IsPostBack)
{
BindGrid();
}

Instead of that use:(Directly BindGrid)
Page Load
BindGrid();

The disadvantages of this Method is the Evets of the controls like RowCommand Wont Work.Check this out Properly

Use directly at the Page Load. This al so the performance issue.But for comparing the above this give some better performance.

?? Operator in .NET

?? Operator in .NET


This operator is used to check whether the string is null or not.If is it null values we can set a Default value in it.


Example:
string a;//The default value of a is null

Page Load
{
a=a?? "Hi";
Response.write(a);
}

output: Hi // we set the default value of a is null

To Add Empty Row in DataTable in ADO.NET

To Add Empty Row in DataTable in ADO.NET


if ((Ds.Tables[0].Rows.Count == 0))
{
Ds.Tables[0].Rows.Add(Ds.Tables[0].NewRow());
}


Ds-DataSet Name that containes Tables

Create a DataTime With a user Specified Time

Create a DataTime With a user Specified Time


DateTime SlotEndTime =DataTime.Now;

SlotEndTime = new DateTime(SlotEndTime.Year, SlotEndTime.Month, SlotEndTime.Day, 9, 30, 0);

SlotEndTime -Have the Date of today with the Time-9.30 AM

Create a DataTime With a user Specified Time

DateTime Time1=DataTime.Now;

Time1= new DateTime(SlotEndTime.Year, SlotEndTime.Month, SlotEndTime.Day, 9, 30, 0);


The Time1- Field have Today date with the time 9.30 am

DateTime Comparison in .NET:

DateTime Comparison in .NET:


DateTime t1 = DateTime.Now;


DateTime t2 = Convert.ToDateTime("11:00:00 AM");


int i = DateTime.Compare(t1,t2);


//if t1 is less than t2 then result is Less than zero


//if t1 equals t2 then result is Zero


//if t1 is greater than t2 then result isGreater zero

DateTime Comparison in .NET:

DateTime t1 = DateTime.Now;

DateTime t2 = Convert.ToDateTime("11:00:00 AM");

int i = DateTime.Compare(t1,t2);

//if t1 is less than t2 then result is Less than zero

//if t1 equals t2 then result is Zero

//if t1 is greater than t2 then result isGreater zero

Set Primary key to the DataTable-in .NET

Set Primary key to the DataTable-in .NET


Create primary key to the Table that present inside the DataSet:

Ds.Tables[0].PrimaryKey = new DataColumn[] { Ds.Tables[0].Columns["id1"]};

For above: id1 is DataColumn


Set Primary key to the Single Table:

Dt.PrimaryKey=new DataColumn[]{Dt.Columns["id1"]};


For above:

Dt-DataTable Name.
id1-DataColumn

Check whether the string Empty string or Not-.NET

Check whether the string Empty string or Not-.NET


Consider in ASP.NET form u r having one Textbox.you want to check out the textbox entry is empty string or not......

1. if(TextBox1.Text.Trim()==string.Empty)
{
Response.write("The string is Empty string");
}
else
{
Response.write("The string is not Empty");
}


2. if (TextBox1.Text.Trim().Length> 0)
{

Response.write("The string is not Empty");
}



Comparing to the performance the second one 370% faster than the first one.

Case Insesitive string Comparison in .NET

string a="hi";

if (a.ToLower()=="HI".ToLower())
{
Response.Write("Matching");
}
else
{
Response.Write("Not Matching");
}


The Result is: Matching
a->String Name

ASP.NET-Remove Blank Space while designing a page

Remove Blank space while designing a page. For example consider the below things

//






Save it into Notepad 1 as a textFile

//





Save it into Notepad2 as a textFile

And see the Properties of those Files.And check the Memory usage of the Blank Space

C#.NET Coding Performance

While Converting string to Integer Consider the Following things.

For Example u r reading a data from SQL datareader and convert it into integer format


while(SQldr.Read())
{
int a=Int.parse(SQLdr[0].toString());
int b=(int)SQLdr[0];
}


For Example Considering the above coding the later one gave a good performance.
int b=(int)SQLdr[0];
By comparing it into the first one the first one first convert the SQL datareader data
into String.And then only the the string is converted into integer.But the second one is not like that.
So the second one increase the performance

Dont use SP_ Prefix while creating the StoredProcedure-SQL Server

Reason:

Dont use sp_ prefix while creating the Storedprocedure.Because it degrades the system performance.while creating like this it first scan the master database...After that only it returns to check our concern database.

To Select IDENTITY column From Table-SQL Server

The query as Follows:
SELECT IDENTITYCOL FROM TableName

Purpose of SET NO COUNT ON inside the stored Procedure-SQL Server

If u dont use SET NO COUNT ON inside the stored procedure the DONE_IN_PROC Message each time send to the caller..For the Concern insert,delete,update operation taken.

This is the Performance issue.In order to overcome the use
SET NO COUNT ON inside the Stored Procedure

Create Transaction Inside the Stored Procedure-SQL

The Query as Follows:

CREATE TABLE A(ID1 INT IDENTITY,VALUE INT CONSTRAINT PK_A PRIMARY KEY NOT NULL)

CREATE TABLE B(ID2 INT IDENTITY,VALUE2 INT CONSTRAINT PK_B PRIMARY KEY NOT NULL)

CREATE TABLE C(ID3 INT IDENTITY,VALUE3 INT CONSTRAINT PK_C PRIMARY KEY NOT NULL)

Procedure:
CREATE PROCEDURE VALUE_INSERT
(
@FIRST INT=NULL,
@SECOND INT=NULL,
@THIRD INT=NULL)
AS
SET NOCOUNT ON
BEGIN TRAN T1
INSERT INTO A VALUES(@FIRST)
IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
INSERT INTO B VALUES(@SECOND)

IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
INSERT INTO C VALUES(@THIRD)

IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
COMMIT TRAN T1
For the above example I am creating 3 tables. If anywhere the primary key violation occurs then the whole transaction will be Roll backed.

Executing the Procedure:
VALUE_INSERT 1,1,1

VALUE_INSERT-Procedure Name


For the above sp the identity state changed.

In order to create a sequential identity insert u have manually insert the identity value by setting the identity_insert on to the concern table

CREATE PROCEDURE INSERT_VALUE
(
@FIRST INT=NULL,
@SECOND INT=NULL,
@THIRD INT=NULL)
AS
SET NOCOUNT ON
DECLARE @ID1 INT
DECLARE @ID2 INT
DECLARE @ID3 INT
SELECT @ID1=COUNT_BIG(ID1)+1 FROM A
SELECT @ID2=COUNT_BIG(ID2)+1 FROM B
SELECT @ID3=COUNT_BIG(ID3)+1 FROM C BEGIN TRAN T1 BEGIN SET IDENTITY_INSERT A ON
INSERT INTO A(ID1,VALUE) VALUES(@ID1,@FIRST)
IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
SET IDENTITY_INSERT A OFF
END

BEGIN
SET IDENTITY_INSERT B ON
INSERT INTO B(ID2,VALUE2) VALUES(@ID2,@SECOND)
IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
SET IDENTITY_INSERT B OFF
END
BEGIN
SET IDENTITY_INSERT C ON
INSERT INTO C(ID3,VALUE3) VALUES(@ID3,@THIRD)
IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
SET IDENTITY_INSERT C OFF
END
COMMIT TRAN T1

Types Of Cursor-SQL Server

1.Insensitive Cursor
2.Scroll Cursor
3.Forward-Only
4.Static Cursor
5.Keyset Cursor
6.Dynamic Cursor


1.Insensitive Cursor:
Insensitive cursor Example without using the looping structure

select * from table1

declare cursor_table1 insensitive cursor for select name1,salary from table1//Cursor declaration

create procedure p_table1 as
declare @name1 varchar(50)declare @salary integer
open cursor_table1 fetch next from cursor_table1 into @name1,@salary print @name1 print @salaryclose cursor_table1


exec p_table1

2.Scroll Cursor:

In This scroll cursor we can have more options method compared to insensitive cursor.
AN insensitive cursor have just a
1.Fetch next statement

But for Considering the scroll cursor have more as indicated below
1.fetch next
2.fetch first
3.fetch last
4.fetch Relative
5.fetch absolute

One important things is we cant able to update the cursor


simple example if the scroll cursor declaration as given below


declare cursor_table3 scroll cursor for select name1,salary from table1


3.Forward only cursor:

This forword only cursor is nothing but the fetching of records are moved from
first to the last.

It support fetch next function

Each time the fetch status is evaluated is (Under this type of situation we are using
Static and keyset cursor)


Delete a Cursor:

Deallocate CursorName

Inner,Outer,Cross Join-SQL Server

create table table11(id integer identity,name varchar(50),address varchar(50))

insert into table11 values('d','coimbatore')
select * from table11

create table table22(id integer identity,name varchar(50),salary integer)
insert into table22 values('q',3000)

select * from table22

outer join three types they are left,right,full outer join

Left Join:
select table11.name,table22.name from table11 left join table22 on table11.name=table22.name

Right join:
select table11.name,table11.address ,table22.name from table11 right join table22 on table11.name=table22.name

Full Outer join:
select table11.name,table11.address,table22.name,table22.salary from table11 full outer join table22 on table11.name=table22.name

// inner join it display only matched items
select table11.name ,table22.name from table11 inner join table22 on table11.name=table22.name

//cross join 0,0 0,1 1,0 1,1

select table11.name ,table22.name ,table11.id,table22.id,table11.address,table22.salary from table11 cross join table22

To Select Odd and Even number of records From Table-SQL Server

To select odd numbers of records:

select distinct id1,name1,name2 from a where (id1%2)=1

To Select even numbers of record:

select distinct id1,name1,name2 from a where (id1%2)=0

Here id1 is a Identity column of a Table a
id1-Identity Column
a-TableName

To get Current Time in SQL Server

The query as Follows:

select CONVERT(char(5), CURRENT_TIMESTAMP,8)

Update Salary Based on Particular Range-SQL Server

update table1
set salary=case when salary between 1000 and 1500 then salary+10
when salary between 2000 and 2500 then salary+56
when salary between 3000 and 3500 then salary+10
when salary between 5000 and 5500 then salary +10
end

table-TableName
salary-Column Name

Types Of Constraints-SQL Server

1.Primary Key
2.Foreign key or references integirity
3.Unique
4.Check
5.NOt null
6.Default

The unique key allows only one null values.But the primary key does,nt allows a null values
By default The primary key is clustered index.
By default the unique key is NOn Clustered index.

Clustered Index:
It alters the way in which the data is srored in the database.

Non Clustered Index:
It does not alters the way in which the data is stored in the database.

Whenever We are implementing an indexes it uses non clustered index of sorting the data

To found n'th Salary Of a Recods:SQl Server

The query as Follows:

select top 1 salary from (select distinct top 2 salary from table1 order by salary desc) a order by salary

Here
salary-Column Name
table1-Table Name
a-Concatenation..It may be any other character

DateTime Formatted Output: SQL Server

0 or 100 mon dd yyyy hh:miAM (or PM)
101 mm/dd/yy 102 yy.mm.dd
103 dd/mm/yy
104 dd.mm.yy
105 dd-mm-yy
106 dd mon yy
107 Mon dd, yy
108 hh:mm:ss
9 or 109 mon dd yyyy hh:mi:ss:mmmAM (or PM)
110 mm-dd-yy
111 yy/mm/dd
112 yymmdd
13 or 113 dd mon yyyy hh:mm:ss:mmm(24h)
114 hh:mi:ss:mmm(24h)
20 or 120 yyyy-mm-dd hh:mi:ss(24h)
21 or 121 yyyy-mm-dd hh:mi:ss.mmm(24h)
126 yyyy-mm-dd Thh:mm:ss.mmm(no spaces)
130 dd mon yyyy hh:mi:ss:mmmAM
131

DateTime Conversion in SQL Server

//get doday date alone
SELECT CONVERT(varchar(8), GETDATE(), 112)

//get tomorrow date alone
SELECT CONVERT(CHAR(8), GETDATE()+1, 112)

//date diferrence in years
select datediff(year,20/3/1983,getdate())


//date difference in months
select datediff(month,20/3/1983,getdate())


//date difference in day
select datediff(day,20/3/1983,getdate())



//to add a current date with the user specified date
SELECT DATEADD(day, 30, GETDATE())

//To get the first day of the current month:
SELECT CONVERT(CHAR(10),GETDATE()+1-DAY(GETDATE()),101)

//TO display the date output like 2007/06/20
select convert(char(100),getdate(),111)

//In the above examples i used the parameter//Here I am using without Parameter.The result is like this Jun 21 2007 2:00AM
select convert(char(100),getdate())

//TO get the number of days in a current month 30
SELECT DAY(DATEADD(MONTH, 1, 1-DAY(GETDATE())+GETDATE())-1)

//TO get the current millisecon
Ans:60SELECT DATEPART(millisecond, GETDATE())

//to get the current week days
Ans: Thursday
SELECT DATENAME(WEEKDAY, GETDATE())

//Check is it a Date or Not
SELECT ISDATE('20030713') -- 1 SELECT ISDATE('2003-07-13') -- 1
select isdate(getdate())//result is 1