Thursday, 26 July 2012

ASP.NET Menu Control Issue In Chrome and Safari


INTRODUCTION

While using ASP.NET menu control at that time Safari as well as Chrome is not render that control properly. That’s the reason, why mouse over is not work properly in Chrome and Safari with ASP.NET menu control as shown in below images.


Safari
Chrome

While it's working perfectly with Mozilla FireFox and IE


IE9
Mozilla FireFox

SOLUTIONS

As you can see the look and feel of the main menu is different. Submenu does not show up.So what is the solution for this. I search over Google and found these 3 solutions working perfectly.

Method 1

  • 1) Go to Solution Explorer in Visual Studio and add “ASP.NET Folder” named “APP_Broswers”.
  • 2) Add new item “Browser File” to this special folder and name it “safari.browser” for both browser Chrome as well as Safari.
  • 3) Now Delete all pre-added data from safari.browser file.
  • 4) Now add the following tags to that browser file.
  • 5) Now only you need is to save that file.
setps:

Method 2

The other solution is using Server side code. For fixing bug we need to write this two line of code to each and every Page's Page_PreInit event, Or if MasterPage file is used then no need to add this lines to each and every page, just copy and paste this lines to Only MasterPage's Page_PreInit event. This code is working for both Safari as well as Chrome.

protected void Page_PreInit(object sender, EventArgs e)
{
   if(Request.ServerVariables["http_user_agent"].IndexOf("Safari",StringComparison.CurrentCultureIgnoreCase) != -1)
      Page.ClientTarget = "uplevel";
}

OR Use this alternate code

protected void Page_PreInit(object sender, EventArgs e)
{
  if (Request.UserAgent.Contains("AppleWebKit")) 
     Request.Browser.Adapters.Clear();
}

After applying any of these changes ASP.NET menu is working properly in all browser like Mozilla Firefox,IE,Safari and Chrome.


Chrome
Safari

Enjoy the trick!!!

Wednesday, 4 July 2012

AUTO INCREMENTED ID WITH NVARCHAR/VARCHAR DATATYPE


INTRODUNCTION

As we all know SQL provide functionality for Auto Incremented Column and we (mostly) use it for creating primary keys. But there is one limitation for it, it will Auto Increment only NUMERIC value like 1,2,3……up to N. But sometimes we need something like this. Suppose I have one table which will hold the data from many department like Marketing ,HR ,Networking etc. and my requirement is something like to insert a PK like M001,M002,M003…. if Marketing Department Record, and N001,N002,N003…. If Networking Department record or same as for HR likes H001, H002, H003... So how to manage this automatically.

Logic for creating Auto Incremented ID with VARCHAR/NVARCHAR data type is modified as per requirement.

SOLUTIONS

/* 
 HERE I CREATED DUMMY TABLE FOR DEMONSTRATING YOU
*/

CREATE TABLE TESTING(
 ID VARCHAR(5),
 NAME VARCHAR(15),
 DESCP VARCHAR(50)
);
GO


/*
 CREATING PROCEDURE FOR INSERTING RECORD WITH NVARCHAR AUTOINCREMENTED ID
*/
CREATE PROCEDURE SP_INSERT
 @NAME VARCHAR(MAX),
 @DESCP VARCHAR(MAX)
AS
BEGIN
 /* Logic for Getting New ID as Per the NAME with PRE FIX */
 DECLARE @NEWID VARCHAR(5);
 DECLARE @PREFIX VARCHAR(1);
 SET @PREFIX = UPPER(SUBSTRING(@NAME, 1, 1))
 SELECT @NEWID = (@PREFIX + replicate('0', 3 - len(CONVERT(VARCHAR,N.OID + 1))) + CONVERT(VARCHAR,N.OID + 1)) FROM (
  SELECT CASE WHEN MAX(T.TID) IS null then 0 else MAX(T.TID) end as OID FROM (
   SELECT SUBSTRING(ID, 1, 1) as PRE_FIX,SUBSTRING(ID, 2, LEN(ID)) as TID FROM Testing
  ) AS T WHERE T.PRE_FIX = @PREFIX
 ) AS N

 /* INSERT QUERY FOR NEW RECORD */
 INSERT INTO Testing VALUES (@NEWID,@NAME,@DESCP)
END
GO

/* HERE YOU NEED TO PASS A VALUE TO THIS SP LIKE THIS
 @NAME = 'MANAGEMENT'
 @DESCP = 'YOUR MANAGEMENT DESCRIPTION'
 
 @NAME = 'NETWORK'
 @DESCP = 'YOUR NETWORK DESCRIPTION'
 
 IT WILL AUTOMATICALLY INSERT THE RECORD
 IF YOU GO FOR MANAGEMENT THEN THE ID WILL BE
 M001 FOR FIRST TIME then NEXT TIME IT WILL M002
 AND THEN IF YOU GO FOR NETWORK THEN THE ID WILL BE
 N001 FOR FIRST NETWORK RECORD THEN N002...
*/  
SP_INSERT 'MANAGEMENT','YOUR MANAGEMENT DESCRIPTION';

SP_INSERT 'NETWORK','YOUR NETWORK DESCRIPTION';

SP_INSERT 'HR','YOUR HR DESCRIPTION';

SP_INSERT 'MANAGEMENT','YOUR MANAGEMENT DESCRIPTION 2';

SP_INSERT 'NETWORK','YOUR NETWORK DESCRIPTION 2';

SP_INSERT 'HR','YOUR HR DESCRIPTION 2';


/* 
 SELECT QUERY FOR SELECTING RECORD INSERTED UISNG SP_INSERT 
*/
SELECT * FROM TESTING

RESULT

ID    NAME            DESCP
----- --------------- ------------------------------
M001  MANAGEMENT      YOUR MANAGEMENT DESCRIPTION
N001  NETWORK         YOUR NETWORK DESCRIPTION
H001  HR              YOUR HR DESCRIPTION
M002  MANAGEMENT      YOUR MANAGEMENT DESCRIPTION 2
N002  NETWORK         YOUR NETWORK DESCRIPTION 2
H002  HR              YOUR HR DESCRIPTION 2

Thursday, 7 June 2012

Table Variable V/S Temporary Table

Difference between Table Variable and temporary table

Feature Table Variables Temporary Tables
Scope Current batch Current session, nested stored procedures. Global: all sessions.
Usage UDFs, Stored Procedures, Triggers, Batches. Stored Procedures, Triggers, Batches.
Creation DECLARE statement only.

CREATE TABLE statement.

SELECT INTO statement.

Table name Maximum 128 characters. Maximum 116 characters.
Column data types

Can use user-defined data types.

Can use XML collections.

User-defined data types and XML collections must be in tempdb to use.
Collation String columns inherit collation from current database. String columns inherit collation from tempdb database.
Indexes Can only have indexes that are automatically created with PRIMARY KEY & UNIQUE constraints as part of the DECLARE statement. Indexes can be added after the table has been created.
Constraints PRIMARY KEY, UNIQUE, NULL, CHECK, but they must be incorporated with the creation of the table in the DECLARE statement. FOREIGN KEY not allowed. PRIMARY KEY, UNIQUE, NULL, CHECK. Can be part of the CREATE TABLE statement, or can be added after the table has been created. FOREIGN KEY not allowed.
Post-creation DDL (indexes, columns) Statements are not allowed. Statements are allowed.
Data insertion INSERT statement (SQL 2000: cannot use INSERT/EXEC).

INSERT statement, including INSERT/EXEC.

SELECT INTO statement.

Insert explicit values into identity columns (SET IDENTITY_INSERT). The SET IDENTITY_INSERT statement is not supported. The SET IDENTITY_INSERT statement is supported.
Truncate table Not allowed. Allowed.
Destruction Automatically at the end of the batch. Explicitly with DROP TABLE statement. Automatically when session ends. (Global: also when other sessions have no statements using table.)
Transactions Last only for length of update against the table variable. Uses less than temporary tables. Last for the length of the transaction. Uses more than table variables.
Stored procedure recompilations Not applicable. Creating temp table and data inserts cause procedure recompilations.
Rollbacks Not affected (Data not rolled back). Affected (Data is rolled back).
Statistics Optimizer cannot create any statistics on columns, so it treats table variable has having 1 record when creating execution plans. Optimizer can create statistics on columns. Uses actual row count for generation execution plan.
Pass to stored procedures SQL 2008 only, with predefined user-defined table type. Not allowed to pass, but they are still in scope to nested procedures.
Explicitly named objects (indexes, constraints). Not allowed. Allowed, but be aware of multi-user issues.
Dynamic SQL Must declare table variable inside the dynamic SQL. Can use temporary tables created prior to calling the dynamic sql.

Friday, 18 November 2011

Adding SYNTAXHIGHLIGHTER in Blog..

How to add SYNTAXHIGHLIGHTER to your blog.?
for adding syntaxhighlighter in our blog we need to apply some changes to our blog template...
1) Frist of all select your blog and go to design.
2) there select Template it will show your selected template.
3) then go to edit html of your template


4) it will ask for confirmation for editing html directly select proceed
5) it will show one text area contain design of your template there you need to search this tag
</head> use Ctrl + F to find this tag



6) now use this block of javascript and link and past it just above the </head>



7) now select save template and it will close the edit html window.


8) now syntax highlighter will added to your blog..
9) for using syntax highlighter to highlight your syntax use pre tab like this...
<pre class="brush:c-sharp"> 
  // your code will be placed between this pre tag 
</pre> 
for more information on brush type please see this link...
http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/
10) Now its done enjoy...!!!!!


Saturday, 12 November 2011

Debug or Test your Windows Service Without Installing it...!!!

Introduction

When you develop a Windows Service and want to run or debug it, you get a message box with this message:

Cannot start service from the command line or a debugger.
A Windows Service must first be installed (using installutil.exe)
and then started with the ServerExplorer, Windows Services 
Administrative tool or the NET START command.

So for testing you have to first install it on your computer, but it is a long process and also boring because every time you make changes, you have to reinstall your service and test it again.

Solution

For debugging or testing your service without installing it, make changes in Program.cs
static class Program
{
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new MyService() 
        };
        ServiceBase.Run(ServicesToRun);
    }
}
Change it to: 

static class Program
{
    static void Main()
    {
        #if(!DEBUG)
           ServiceBase[] ServicesToRun;
           ServicesToRun = new ServiceBase[] 
           { 
               new MyService() 
           };
           ServiceBase.Run(ServicesToRun);
         #else
           MyService myServ = new MyService();
           myServ.Process();
           // here Process is my Service function
           // that will run when my service onstart is call
           // you need to call your own method or function name here instead of Process();
         #endif
    }
}

After adding #if and #else to your main fuction, now when you press F5 or run your service, it will not show you the previous message and simply run, so attach a break point to your method which will be called by the service when it will start. Using this, you can simply debug your service without installing it.

For this no need to add any extra using directive (like using System.Data or using System.IO) to your class file. It will simply as it is.

Enjoy!!!