Wednesday, March 28, 2012

Microsoft open sourcing ASP.NET MVC 4, ASP.NET Web API, ASP.NET Web Pages v2 (Razor) - under the Apache 2.0 license.


Microsoft announce that developers outside the Microsoft can now submit code snippet and patches which they may review and include in the products.

You can find more details on Scott Guthrie's blog

“We will also for the first time allow developers outside of Microsoft to submit patches and code contributions that the Microsoft development team will review for potential inclusion in the products,” Guthrie says. “We announced a similar open development approach with the Windows Azure SDK last December, and have found it to be a great way to build an even tighter feedback loop with developers – and ultimately deliver even better products as a result.”

Also you can find the source on CodePlex.

Tuesday, March 27, 2012

Query to find number of users connected in an MS SQL database


Sometimes you may get an error from your application saying "Connection pool size exceeded".

Here I am going to explain you few queries which can be used to find out the number of open connections in your MS SQL database and also the query to find the maximum connections allowed in SQL server.

First we will check, what is the maximum connections SQL server will allow you to connect.

SELECT @@MAX_CONNECTIONS AS 'Maximum Connections Allowed in the database'

Now you know that how many connections you can have in your database. So we will check how many are already used.

Below query can be used to find the number of connections in each database in your server if you execute this query against master database.

SELECT DB_NAME(dbid) as [Database Name], COUNT(dbid) as [Number of Connections] from master.dbo.sysprocesses with (nolock)
WHERE dbid > 0 GROUP BY dbid

Now we will see who all are the users connected to your database.

Below query will show you the login name and the host name of the user connected to your database. In below query you may change the database name when you execute in your system.

WHERE DB_NAME(DBID) = 'DataBaseName' AND DBID != 0

Now at the end we will see a very simple query which can be used to show all the sessions that are currently established in the database.


Friday, March 23, 2012

How to Export Gridview data to Excel with special characters in ASP.NET

Many times you may get requirement to export data to excel with special characters. When you do normal export with special characters your excel file will have weird characters in place of special character.

So in such cases what you have to do is, Unicode encoding. I have given sample code below.

private void ExportToExcel()
          string Excelfilename = "Test_Excel_Export" + DateTime.Now;
          Response.Buffer = true;
          Response.ContentType = "application/";
          Response.AppendHeader("Content-Disposition:", "attachment; filename=" + Excelfilename + ".xls");
          Response.Charset = "";
          Response.ContentEncoding = Encoding.Unicode;
          this.EnableViewState = false;
          System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
          System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

In the above code two import lines of code which does the encoding is below,

Response.ContentEncoding = Encoding.Unicode;

To use above code you have to add below namespace otherwise you will get error.

using System.Text;

I hope now you are very clear about “How to Export Gridview data to Excel with special characters in ASP.NET”.

You are always welcome to post your queries below.

Wednesday, March 21, 2012

How to list all the constraints in SQL Server Database


There are many methods to lists the constraints in SQL database. We will go through few methods one by one.

Method 1:


Above query will return all the constraints in all the database.

Method 2:


Above query will return all the constraints in the specific database.

Method 3:

OBJECT_NAME(parent_object_id) AS [TableName],
type_desc AS [TypeOfConstraint]
FROM sys.objects

This method will give bit more details about the constraints. This can be used to return all constraints in the database with Constraint Name, Table Name and Type of Constraint.

Method 4:

sp_help TableName

This query will return all the constraints in the specific table.

Tuesday, March 20, 2012

How to disable previous dates in Calendar control in ASP.NET


This is a simple code but many people got confused about this inbuilt feature in .NET Calendar control.

We will be using Calendar DayRender control to disable all the previous dates.

First we will add the Calendar control,

<asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender">

No we will see what you have to write under DayRender event,

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
            if (e.Day.Date < DateTime.Now.Date)
                e.Day.IsSelectable = false;
                e.Cell.ForeColor = System.Drawing.Color.Gray;


See the Calendar control after disabling the previous dates,


Monday, March 19, 2012

Best of Microsoft Management Summit (UK) 2012‏


82141_d6 MMS Email

Learn about 2012's exciting wave of Microsoft Private Cloud product launches and announcements – starting with the release of System Center 2012.
Get the latest insights into the Microsoft Private Cloud and learn what's new in System Center 2012. The Best of MMS 2012 brings you the highlights from MMS 2012 conference in a choice of UK-wide one-day or virtual events.

Register for the event using the link Launching System Center 2012

Wednesday, March 14, 2012

How to read web.config file using JavaScript


I have seen many people asking this question in .NET forums. So I thought I will share this small piece of code.

First we will see web.config entry which we are going to read,

                 <add name="MysqlCon"
         connectionString="server=1xx.1xx.x.x;User Id=username;password=password;database=dbName;Persist Security Info=True"
         providerName="MySql.Data.MySqlClient" />

Now we will see the JavaScript function which reads above connection string and the AppSettings key,

<script language="javascript" type ="text/javascript"> 
function ReadWebConfig()
    var strCon = '<%=ConfigurationManager.ConnectionStrings["MysqlCon"].ConnectionString %>'
    var strTemp = '<%=ConfigurationManager.AppSettings["WordTemplate"].ToString() %>'

Tuesday, March 13, 2012

Visual Studio 11 Beta in LIVE now


Happy news for all .NET developers who was waiting to have glance on Visual Studio 11 Beta.

You can download Visual Studio 11 Beta from Microsoft download centre now.

Please check this link to download the Visual Studio 11 Beta

Monday, March 12, 2012

List all Stored Procedure Names and Scripts in the database.


Below SQL query can be used to display all Stored Procedures and SQL Scripts used to create those Stored Procedure in your database.

SELECT  sysObj.Name as [Stored Procedure Name], sysM.Definition as [Stored Procedure Script]
FROM sys.sql_modules as sysM INNER JOIN sys.objects as sysObj 
ON sysM.object_id = sysObj.object_id 
WHERE sysObj.type = 'P'

If you need more sample please refer my articles: Stored Procedures

Monday, March 5, 2012

Download for Internet Information Services (IIS) 8.0 Express Beta is now live


Happy news for the developers. Microsoft announce that download for IIS 8.0 Express Beta is now live.  You can download the same using the following URL:

The IIS 8.0 version of IIS Express shares more of its foundation with the full version of IIS, so you may not face much issues when you switch between your development environment and your production environment.

More details are available in the ReadMe file for IIS Express, which is can be seen at the following URL:

Thursday, March 1, 2012

How to display alert in outlook express if subject field is empty while sending mail


This is a common mistake every one does while sending mail using outlook. You may feel bad if you send an important mail to the client or to your manager without a subject.

Since Microsoft didn't provide any option to make the subject mandatory or any warning for the empty subject I thought I will do a little research and find out the workaround.

This is a VB code which will display an alert when you click on Send Mail button in Outlook if subject field is empty.

Just follow below steps,
1. Of course first we will open outlook
2. Then we will Open Visual basic editor by pressing ALT+F11
3. Then from left pane expand the Project1 until you see "ThisOutlookSession"
4. Double Click on "ThisOutlookSession"
5. Now it's the time to copy and paste the below code in the new window opened when you double click on "ThisOutlookSession".
6. Save and Close the window. You are done!

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) 
Dim strOutlookSubject As String 
strOutlookSubject = Item.Subject 
If Len(strOutlookSubject) = 0 Then 
Prompt$ = "Are you sure you want to send the Mail without Subject?" 
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Subject is empty") = vbNo Then 
Cancel = True 
End If 
End If 
End Sub

Note: Change the alert as per your interest from above code.

Below is the screen print of the alert which you may be getting,


Now try to send mail without subject from your outlook!!!

My Weblog post