It is very easy to format the datetime to string in .NET and this is commonly used by the developers while displaying the date & time in desired format. But if you would like to store the formatted value in datetime variable, the value changes according to the .NET default format.

Default datetime format in .NET is

image

We will try this to format the value,

image

When we try to assign the formatted value to a datetime variable the format again changed to default format

image

To avoid this, use DateTime.ParseExact

DateTime dt = DateTime.ParseExact(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);

Where is this useful?
If you develop client server application the .NET stores the datetime value with timezone. The above syntax is timezone free, which will be useful across the application.

Also refer this link for DateTime best practices http://msdn.microsoft.com/en-us/library/ms973825.aspx


 
Categories: .NET

When there is nested master pages and when we try to access the control inside the base master page, you will get an error "NullReferenceException". The reason is ASP.NET didn't have the method to find the control in recursive.

To explain in detail, we have BaseMaster.master file

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="BaseMaster.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>XTP Site Manager</title>
    <asp:ContentPlaceHolder id="head" runat="server"></asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div id="div_body">
        <asp:ContentPlaceHolder id="body" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Another Master file which inherits BaseMaster, HomeMaster.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="HomeMaster.master.cs" Inherits="MasterPages_HomeMaster" MasterPageFile="~/MasterPages/BaseMaster.master" %>
<asp:Content runat="server" ID="Content1" ContentPlaceHolderID="body">
             
        <!-- HEADER -->
        <div id="div_header" runat="server" style="position: absolute; top: 0px; left: 0px; width: 900px; height: 100px">
            <asp:contentplaceholder id="Header" runat="server">
            </asp:contentplaceholder>
          </div>
        
        <!-- LEFT NAV -->
        <div id="div_leftnav" runat="server" style="position: absolute; top: 100px; left: 0px; width: 200px; height: 500px">
            <asp:contentplaceholder id="LeftNav" runat="server">
            </asp:contentplaceholder>

        </div>
        
        <!-- CENTER CONTENT -->
        <div id="div_content"  runat="server" style="position: absolute; top: 100px; left: 200px; width: 550px; height: 500px">
            <asp:contentplaceholder id="Center" runat="server">
            </asp:contentplaceholder>

        </div>
        
        <!-- RIGHT NAV -->
        <div id="div_rightnav" runat="server" style="position: absolute; top: 100px; left: 750px; width: 150px; height: 500px;">
            <asp:contentplaceholder id="RightNav" runat="server">
            </asp:contentplaceholder>

        </div>
        
        <!-- FOOTER -->
        <div id="div_footer" runat="server" style="position: absolute; top: 600px; left: 0px; width: 900px; height: 100px;">
            <asp:contentplaceholder id="Footer" runat="server">
            </asp:contentplaceholder>
        </div>             
  
</asp:Content> 

Now we have default page which inherits HomeMaster.master and in the codebehind we will try to add an user control to the master page.

ContentPlaceHolder cp = new ContentPlaceHolder();
cp = Page.FindControl("Header") as ContentPlaceHolder;
if (cp != null)
{
    UserControl uc = this.LoadControl("usercontrols/static_widget.ascx") as UserControl;
    cp.Controls.Add(uc);
}
When you try the above, you will get an error "NullRefernceException" is because of nested master pages. Thanks to C# 3.0 extensions, in which we can extend the methods and it will be displayed in VS Intellisense also.
public static class PageExtensionMethods
{
    public static Control FindControlRecursive(this Control ctrl, string controlID)
    {
        if (string.Compare(ctrl.ID, controlID, true) == 0)
        {
            // We found the control!
            return ctrl;
        }
        else
        {
            // Recurse through ctrl's Controls collections
            foreach (Control child in ctrl.Controls)
            {
                Control lookFor = FindControlRecursive(child, controlID);

                if (lookFor != null)
                    return lookFor;     // We found the control
            }

            // If we reach here, control was not found
            return null;
        }
    }
}
Now you get another method "FindRecursiveControl" for the page, and the below code works without any error.
ContentPlaceHolder cp = new ContentPlaceHolder();
cp = Page.FindControlRecursive("Header") as ContentPlaceHolder;
if (cp != null)
{
    UserControl uc = this.LoadControl("usercontrols/static_widget.ascx") as UserControl;
    cp.Controls.Add(uc);
}
Hope this helps.
 
Categories: ASP.NET

Everybody is enjoying with new SQL Server 2008 intellisense feature. At the same time you would have wondered that it is not working with new tables, views, stored procedures created. This is due to cache refresh issue with SQL Server 2008 and you can simply refresh the cache as explained below.

sql-intellisense1

I have added new table “Active Reports”, but it is not showing up in the SQL Intellisense.

sql-intellisense2

Even if you type the sql query analyzer shows as error, even if you execute the query it will not show error and you will be blaming the intellisense feature.

sql-intellisense3

Now, go to SQL Menu Edit -> IntelliSense -> Refresh Local Cache (Ctrl+Shift+R), the intellisense detected the table as shown below.

sql-intellisense4

Hope this helps to the new comers of SQL 2008


 
Categories: SQL Server 2008