Friday, June 5, 2009

ASP.NET Master page Set Background Image to Table

Problem :When we set ASP Net - Master Page background image it only visible from pages in root directory
Solution : The images which are used in Masterpage should be set in the code behind file as fallow .
The runat='Server" attribute should be added to the controller which are going to set background image and ID should be set to that controller.
<td runat="server" id="tb1">set back ground image with code</td>

In the code behind page image should be set using ResolveClientUrl() method
protected void Page_Load(object sender, EventArgs e)
{
tb1.Style[HtmlTextWriterStyle.BackgroundImage] = ResolveClientUrl("~/App_Themes/Images/1-sri-lanka.JPG");
}


Tuesday, June 2, 2009

Sql Date Time Formatting

Remove Time Part From DateTime
---------------------------------
SELECT GETDATE() /// 2009-06-25 12:03:56.640
Declare @mydate DATETIME
SET @mydate=GETDATE()

-------------------------------------

1.Get Out put as Type of DateTime with zero time
SELECT DATEADD(day, DATEDIFF(day, '20000101',@mydate), '20000101')
or
SELECT DATEADD(DAY, 0, DATEDIFF(DAY,0,@mydate))
or
SELECT DATEADD(dd, DATEDIFF(dd, 0, @mydate), 0)

out put :2009-06-25 00:00:00.000

2.Get Out put as Type of Varchar without time

SELECT CONVERT(VARCHAR(10),@mydate, 103)
out put :25/06/2009

Or
SELECT CONVERT(VARCHAR(10), @mydate, 120)
out put :2009-06-25

Sunday, May 10, 2009

Unique Identifier In .Net & SQL

A GUID (Globally Unique Identifiers) is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated. In the .NET framework the System.Guid.NewGuid method is used to generate the unique identifiers.

System.Guid desiredGuid = System.Guid.NewGuid();

This desiredGuid can be directly save to DB. The data type of the filed should be “uniqueidentifier” or we can cast it as string …. And also we can store GUID in “System.Guid” data type variable(C#).

convert GUID to Sting

System.Guid desiredGuid = System.Guid.NewGuid();
string a=desiredGuid.ToString();

convert a string to a GUID

Guid MyGuid = new Guid(stringValue);

In SQL Server, the “uniqueidentifier” data type is used to store the GUIDs and newid() function will generate GUID.

Thursday, March 19, 2009

TypeForwardedTo (.NET)

We can use this class to move type from one assembly to another while not disrupting the callers compile against the original assembly .
Example : we have a source library (say lib.dll) which included lot of classes and we have use that class library for our application development. After some period of time the we refastening the source library (lib.dll) and it splits in to two source library to (say lib.dll and lib1.dll) .Then what happen to our application will it work? No because our application point to the lib.dll but some of the classes have move to lib1.dll to. So our application will generate error .To prevent this we can use “TypeForwardedTo”

Following 2 classes define in lib.dll
using System;
using System.Collections.Generic;
using System.Text;
namespace lib
{
public class car
{
public static void Do()
{
System.Console.WriteLine("car");

}
}
public class van
{
public static void Do()
{
System.Console.WriteLine("van");
}
}
}

Then the develop application using above lib.dll as following

using System;
using System.Collections.Generic;
using System.Text;
using lib;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
car.Do();
van.Do();
Console.ReadLine();

}
}
}

The Out put is :
car
van


Now we split above lib.dll to 2 dll to as following

lib.dll
using System;
using System.Collections.Generic;
using System.Text;
namespace lib
{
public class car
{
public static void Do()
{
System.Console.WriteLine("car");

}
}
}

lib1.dll
using System;
using System.Collections.Generic;
using System.Text;
namespace lib1
{
public class van
{
public static void Do()
{
System.Console.WriteLine("van");

}
}
}

Then replace the above lib.dll with new two dlls. Now what happen, the van class can’t find in new lib.dll so it generate and error.
So prevent such a error we ca use
“System.Runtime.CompilerServices.TypeForwardedTo” as following
Open the lib project and Open the AssemblyInfo.cs file and add the following line right after the assembly directives.

[assembly: TypeForwardedTo(typeof(lib1.van))]


now build projecj.
Now we have 2dlls, lib.dll and lib1.dll but we can still run oure previouse application without any error .....

Wednesday, February 11, 2009

SQL Select First and Last 100 Record

Select first 100 Records
SELECT RecordId
FROM Table
LIMIT 100


Select Last 100 Records
SELECT RecordId
FROM Table
ORDER BY RecordId DESC
LIMIT 100

Thursday, July 3, 2008

Event and Delegates -Capture Master Page Events in Content Pages

In This article, I explain how to use event and delegate to capture Master page event at content page.
Case :The master page (MasterPage.master.cs) Content drop down list .When The dropdown selected index change the selected value must be display in content page labels.
How to Do:
1. we develop master page as usual .Then top of code behind page of the master page, we have to declare Delegate and also have to declare public event in the class which type of previous delegate .Next step is declaring virtual method that used to handle the event ,Then we have to call the above method at the event of controller(drop down list ).
2.Develop content page and put following code at the top of .aspx page
3. In the code behind file of content page we have to write method capture to the master page event and in the page load event we have to add event handaler to call .
MasterPage.master.cs
This is Master Page with content Drop down list

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.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>Master test</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 158px; height: 414px; background-color: gray">
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="150px">
<asp:ListItem Value="a"></asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList>
</div>
<div style="left: 168px; width: 638px; position: absolute; top: 17px; height: 410px">
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>

class MasterPage
Then Put following codes in page behined file of the master page

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//declare delegates for Master Page Events
public delegate void mstDelegate(object sender, System.EventArgs e);
public partial class MasterPage : System.Web.UI.MasterPage
{

private string slectedValue;
public string SlectedValue
{
get { return slectedValue; }
set { slectedValue = value; }
}
// declare Public Events type of above delegate
public event mstDelegate DropDownEvent;
protected void Page_Load(object sender, EventArgs e)
{

}
// impliment virtual method to handel above event
protected virtual void OnDropDownClick(EventArgs e)
{
if (DropDownEvent != null)
{
//Invokes the delegates.
DropDownEvent(this, e);
}
}
// DropDownList1_SelectedIndexChanged event
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SlectedValue = DropDownList1.SelectedValue;
OnDropDownClick(e);
}

}

Create Test page
testevent.aspx

<%@ MasterType virtualPath="~/MasterPage.master"%>
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="testevent.aspx.cs" Inherits="testevent" Title="test Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="lblOutput" runat="server" Text="Label" Width="244px"></asp:Label>
</asp:Content>
Code behind file of test page
Testevent.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class testevent : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
// create instant of Master event
Master.DropDownEvent += new mstDelegate(mstDDLClick);

}
/// method for catch Master Page menu click event
void mstDDLClick(object sender, EventArgs e)
{
lblOutput.Text = Master.SlectedValue;
}

}

Monday, June 2, 2008

Set a Date Format In GridView Using ASP.NET

Following code explain How to format date in ASP.Net Gride view


< asp :GridView ID=“GridView1″ runat=“server” >
< columns >
< asp :BoundField DataField=“CreationDate”
DataFormatString=“{0:dd-MM-yyyy}”
HtmlEncode=“false”
HeaderText=“CreationDate” />
< /columns >
< /asp >