using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
#region Declarations
CustomersDataObject objData = null;
GridViewRow HeaderRow = null;
#endregion
#region Events
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
objData = new CustomersDataObject();
//DataTable dt = objData.Select().Table ;
gvStylish.DataSource = objData.Select();
gvStylish.DataBind();
}
}
protected void gvStylish_RowCreated(object sender, GridViewRowEventArgs e)
{
//http://www.codeproject.com/Articles/249155/Rows-and-Columns-Merging-in-ASP-NET-GridView-Contr
//http://sammisoft.net/mboard/mboard/mboard.asp?exe=view&board_id=pds&group_name=sammisoft&idx_num=194&page=22&category=0&search_category=&search_word=&order_c=intVote&order_da=asc
//http://www.thaicreate.com/dotnet/forum/048135.html
if (e.Row.RowType == DataControlRowType.Header)
{
GridView HeaderGrid = (GridView)sender;
HeaderRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
for (int cellCount = 0; cellCount < e.Row.Cells.Count; cellCount++)
{
if (e.Row.Cells[cellCount].Text.Trim() == "CustomerID"
|| e.Row.Cells[cellCount].Text.Trim() == "CompanyName"
|| e.Row.Cells[cellCount].Text.Trim() == "ContactName")
{
//Row Span
HeaderRow.Cells.Add(HeaderRowMerg(e, 2, cellCount, System.Drawing.Color.Gray.Name));
}
else
{
//Col Span
HeaderRow.Cells.Add(HeaderColumnMerg(e, 3, "Subjects", System.Drawing.Color.Gray.Name));
break;
}
}
gvStylish.Controls[0].Controls.AddAt(0, HeaderRow);
}
}
///
/// Merging datarow cells
///
/// /// protected void gvStylish_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
/*Merging starts*/
int _iNoOfCellsToCheck = 6;
for (int rowIndex = gvStylish.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow gvRow = gvStylish.Rows[rowIndex];
GridViewRow gvPreviousRow = gvStylish.Rows[rowIndex + 1];
//for (int cellCount = 0; cellCount < gvRow.Cells.Count; cellCount++)
for (int cellCount = 1; cellCount < _iNoOfCellsToCheck; cellCount++)
{
if (gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text)
{
gvRow.Cells[cellCount].RowSpan =
gvPreviousRow.Cells[cellCount].RowSpan < 2 ?
2 :
gvPreviousRow.Cells[cellCount].RowSpan + 1;
gvPreviousRow.Cells[cellCount].Visible = false;
}
}
}
/*Merging ends*/
//If we want to set background color for the row
//if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("TOTAL"))
if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("MUTHU"))
{
SetRowBackColor(e, System.Drawing.Color.Silver.Name);
}
else
//if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("GRAND"))
if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("VIJAY"))
{
SetRowBackColor(e, "#999999");
}
}
}
#endregion
#region Functions
private TableCell HeaderRowMerg(GridViewRowEventArgs e, int iRowSpan, int iIndex, string backcolor)
{
TableHeaderCell Cell_Header = new TableHeaderCell();
Cell_Header.Text = e.Row.Cells[iIndex].Text;
Cell_Header.HorizontalAlign = HorizontalAlign.Center;
Cell_Header.Font.Bold = true;
Cell_Header.RowSpan = iRowSpan;
Cell_Header.Style.Add("background-color", backcolor);
e.Row.Cells[iIndex].Attributes.Add("style", "display: none;");
return Cell_Header;
}
private TableCell HeaderColumnMerg(GridViewRowEventArgs e, int iColumnSpan, string _strHeaderText, string backcolor)
{
TableHeaderCell cell = new TableHeaderCell();
Cell_Header.Text = _strHeaderText;
Cell_Header.HorizontalAlign = HorizontalAlign.Center;
Cell_Header.ColumnSpan = iColumnSpan;
Cell_Header.Font.Bold = true;
Cell_Header.Style.Add("background-color", backcolor);
return Cell_Header;
}
bool IsInt(string strVal)
{
try
{
int value = int.Parse(strVal);
return true;
}
catch
{
return false;
}
}
private void SetRowBackColor(GridViewRowEventArgs e, string color)
{
e.Row.BackColor = System.Drawing.Color.FromName(color);
e.Row.Font.Bold = true;
foreach (TableCell tc in e.Row.Cells)
{
if (IsInt(tc.Text))
tc.Text = Convert.ToInt32(tc.Text).ToString("#,#");
}
}
#endregion
}
Monday, December 19, 2011
Merging Gridview with Multiple Headers
Wednesday, October 12, 2011
AddressAccessDeniedException in WCF 2010
Today when executing the service it gives the below exception.
HTTP could not register
URL http://+:8002/.../. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details). The reason for the above exception is given in the book Programming WCF Services O'REILLY by JUVAL LOWY How to instruct windows to register the URL
HTTP could not register
URL http://+:8002/.../. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details). The reason for the above exception is given in the book Programming WCF Services O'REILLY by JUVAL LOWY How to instruct windows to register the URL
- Go to programs and select "Visual Studio Tools"
- Right click on Visual Stutdio Command Prompt and select Run as administrator
Thursday, October 6, 2011
@@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT – Retrieve Last Inserted Identity of Record
| @@IDENTITY | SCOPE_IDENTITY | IDENT_CURRENT | IDENT_SEED | IDENT_INCR |
| Any table Current session Any scopes. |
Any table Current session Current scope. |
Specific table Any session Any scope. |
Returns the
original seed value If we re-seed using DBCC CHECKIDENT won't change the value. |
Returns the
increment value specified during the creation of an identity column in a table or view that has an identity column. |
| Table or View | Table or View | Table or View | ||
| If user truncates or
drop the table also, it will return the last identity value generated in the current session |
If user truncates or
drop the table also, it will return the last identity value generated in the current session |
If user truncated
the table it will return the Ident_Seed value. It return null when user drop the table. |
Each query window in Query Analyzer is a session.
How to get all the session running in the server?
EXEC sp_who 'test';
How to check the current session
SELECT @@SPID SessionID,
USER_NAME() AS [User_Name] ,
SUSER_NAME() AS [Login identification name of the user.],
CURRENT_USER AS [Name of the current user] ,
SYSTEM_USER AS [System_user],
SESSION_USER AS [Session_user],
USER AS [User]
- DBCC CHECKIDENT ('dbo.IdentityTest', RESEED, 10)
- Returns the original seed value, if we re-seed using DBCC CHECKIDENT.
- IDENT_SEED will return the seed value created for the Idetntiy column( when table creation or Alteration).
- It won't changed by DBCC CHECKIDENT ('dbo.IdentityTest', RESEED, 10)
References
RESED Identity Column
SQLAuthority
Monday, September 26, 2011
Trim Leading Or Trailing Control Characters
DECLARE @MyString VARCHAR(1000) --Control character DECLARE @Tab VARCHAR(2) DECLARE @LineFeed VARCHAR(2) DECLARE @CarriageReturn VARCHAR(2) DECLARE @SearchPattern VARCHAR(10) SET @Tab = CHAR(9) SET @LineFeed = CHAR(10) SET @CarriageReturn = CHAR(13) --Search for any single of more chars not with specified range SET @SearchPattern = '%[^ ' + @Tab + @LineFeed + @CarriageReturn + ']%' SET @MyString = CHAR(10) + ' ' + CHAR(10)+ CHAR(9) + 'My name is ' + CHAR(10) + ' babu ' + CHAR(10) + ' ' + CHAR(10) + CHAR(10) + ' ' + CHAR(10) SELECT LEN(@MyString) AS TotalLength SELECT @MyString AS MyString SELECT LEN( RTRIM( LTRIM( SUBSTRING( @MyString, PATINDEX(@SearchPattern, @MyString), LEN(@MyString) - PATINDEX(@SearchPattern, @MyString) - PATINDEX(@SearchPattern, REVERSE(@MyString)) + 2 ) ) ) ) AS AfterTrimedLength SELECT RTRIM( LTRIM( SUBSTRING( @MyString, PATINDEX(@SearchPattern, @MyString), LEN(@MyString) - PATINDEX(@SearchPattern, @MyString) - PATINDEX(@SearchPattern, REVERSE(@MyString)) + 2 ) ) ) AS AfterTrimed
Tuesday, September 13, 2011
Auto center pop up window
function PopupCenter() {
var width = 600;
var height = 480;
var top = (screen.height / 2) - (height / 2);
var left = (screen.width / 2) - (width / 2);
var parametar = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left;
var url = 'Default.aspx'
var windowName = 'Default';
var targetWin = window.open(url, windowName, parametar);
targetWin.focus();
return false;
}
Page with query string
function PopupCenter(Name, Age) {
var width = 600;
var height = 480;
var top = (screen.height / 2) - (height / 2);
var left = (screen.width / 2) - (width / 2);
var parametar = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + width + ', height=' + height + ', top=' + top + ', left=' + left;
var url = 'Default.aspx?Name=' + Name + '&Age=' + Age;
var windowName = 'Default';
var targetWin = window.open(url, windowName, parametar);
targetWin.focus();
return false;
}
References
www.javascripter.net
msdn
htmlgoodies
Friday, August 5, 2011
Creating a Windows Service in C#.net and call a method every hour
In Visual Studio Select File->New->Project Select Windows and select Windows Service | |||||
Right click on the Service1 designer and select Add Installer. This adds ProjectInstaller Will create a file ProjectInstaller.cs | |||||
To avoid Set Service Login prompt being asked about the system username and password you must change the Account for the serviceProcessInstaller to LocalSystem. ServiceAccount Enumeration ServiceAccount stackoverflow | |||||
ServiceProcessInstaller Installs an executable containing classes that extend ServiceBase. This class is called by installation utilities, such as InstallUtil.exe, when installing a service application. ServiceInstaller Installs a class that extends ServiceBase to implement a service. This class is called by the install utility when installing a service application. | |||||
Display Name is show in name column of the service. Set StartType as Automatic - To make the service to be started after restart/shutdown. | |||||
How to Start the service automatically after Installation? | |||||
Rename Service1 to to MyService | |||||
Create a folder and write the code to write to file | |||||
How to check whether the service called function is working or not? Right click on the Solution(DemoService) and select Add -> New Project This console application is used to test whether file writing works or not. | |||||
How to create Service Installer? Right click on the Solution(DemoService) and select Add -> New Project Select Other Project Types -> Setup and Deployment -> Setup Project | |||||
We will get the below screen. | |||||
Right click on the Setup project | |||||
Select the service project | |||||
Double click on the Application Folder | |||||
Press Ok. | |||||
Select the setup project and press F4 Manufacturer will come in path as "C:\Program Files\ManufacturerValue" Title will come in Setup Wizard | |||||
Click on Configuration Manager | |||||
Change Debug mode to Release for the Service and Setup projects | |||||
| |||||
Type control in run and select Administrative Tools -> Services | |||||
Make the service to call every 10 minutes Create a file as the below image | |||||
public class WriteToFile
{
public WriteToFile()
{
string strDateTime = DateTime.Now.ToString();
StreamWriter sw = new StreamWriter(Path.Combine(System.IO.Path.GetTempPath(), "babu.txt"), true);
sw.WriteLine(strDateTime);
sw.Close();
}
}
Update the MyService.cs fileusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using DemoService.Functions;
using System.Timers;
namespace DemoService
{
public partial class MyService : ServiceBase
{
//Make the service to call the for every 5 min
System.Timers.Timer TimerObj = new System.Timers.Timer(ConvertMinutesToMilliseconds(5));
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
TimerObj.Elapsed += new ElapsedEventHandler(OnElapsedTime);
TimerObj.Enabled = true;
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
TimerObj.Enabled = false;
}
/// |
Friday, July 8, 2011
Switch Theme Without Postback
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 ChangeTheme : System.Web.UI.Page, ICallbackEventHandler
{
protected void Page_PreInit(object sender, EventArgs e)
{
//If theme is null or not changed by user use Default theme
if (String.IsNullOrEmpty(Convert.ToString(Session["Theme"])))
Page.Theme = "Default";
else
Page.Theme = Convert.ToString(Session["Theme"]);
}
protected void Page_Load(object sender, EventArgs e)
{
//JavaScript Callbacks
ClientScriptManager scriptManager = Page.ClientScript;
String cbReference = scriptManager.GetCallbackEventReference(this, "returnValue", "ReceiveServerData", "context");
String callbackScript = "function CallServer(returnValue, context) {" + cbReference + "; }";
scriptManager.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
TextBox1.Text = DateTime.Now.ToString();
}
#region ICallbackEventHandler Members
//Return value from the server to the client
string ICallbackEventHandler.GetCallbackResult()
{
return Convert.ToString(Session["Theme"]);
}
//Get the theme name from the javascript function
public void RaiseCallbackEvent(string eventArgument)
{
Session["Theme"] = eventArgument.Trim();
}
#endregion
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChangeTheme.aspx.cs"
Inherits="ChangeTheme" %>
Theme Switcher
The style set for the background body and the textbox.
You can see that, the time is not changed when we select the color.
This is done from the javascript.
To make apply the theme press the button Apply Theme.
Source Code
Subscribe to:
Posts (Atom)





