Wednesday, October 13, 2010

Disable Save/Open Button in ASP.NET Download Dialog box

Code Behind

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment;filename=sample.xls");
Response.Write("Hello World");
Response.End();
}

HTML Page

To disable Save button, write this meta tag
<head runat="server">
<meta name="DownloadOptions" content="nosave" />
</head>

To disable Open button, write this meta tag
<head runat="server">
<meta name="DownloadOptions" content="noopen" />
</head>

Friday, October 8, 2010

DataKey from a GridView RowCommand Event

Method to get DataKey from a GridView RowCommand Event

protected void grdEmployees_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
int index = row.RowIndex;
int employeeID = Convert.ToInt32(grdEmployees.DataKeys[index].Value);
}

}

For this eg: There should be GridView Control with a TemplateField and that should contain a Linkbutton with CommandName="Select"

SQL Query to Get SP and Text from DB

SELECT sysobjects.name, syscomments.text
FROM sysobjects JOIN syscomments ON
sysobjects.id = syscomments.id
WHERE xtype='P'

Write to Event Viewer

/// <summary>
/// Method to write Errors to Event viewer
/// </summary>
/// <param name="message">message</param>
static void WriteToEventViewer(string message)
{
string sSource = "Sample";
string sLog = "MyApplication";

if (!EventLog.SourceExists(sSource))
{
EventLog.CreateEventSource(sSource, sLog);
}
EventLog.WriteEntry(sSource, message,

EventLogEntryType.Error);
}

for more information refer this
http://msdn.microsoft.com/en-us/library/xzwc042w.aspx

Get Client Machine IP Address

/// <summary>
/// Method to get Client IP number
/// </summary>
/// <returns>IP Address</returns>
private string GetIPNumber()
{
return Request.UserHostAddress;
}

Friday, July 9, 2010

Dynamic SQL Query Building

Create SQL Table


CREATE TABLE [dbo].[Employee](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Address] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Phone] [varchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Pin] [int] NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Insert Data in to Employee Table

INSERT INTO Employee([Name],[Address],[Phone],[Pin])VALUES('Sabu','Cochin','9876543210',682030)
INSERT INTO Employee([Name],[Address],[Phone],[Pin])VALUES('Arun','TVM','9876543210',789456)
INSERT INTO Employee([Name],[Address],[Phone],[Pin])VALUES('Rahul','BGLR','1472583690',456782)
INSERT INTO Employee([Name],[Address],[Phone],[Pin])VALUES('Ann','DELHI','1234566985',256314)
INSERT INTO Employee([Name],[Address],[Phone],[Pin])VALUES('Vipin','Cochin','5852369641',682020)

Procedure for searching for Employees
---------------------------------------------------


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE pr_SearchEmployee
-- Add the parameters for the stored procedure here
@EmployeeName varchar(20)
,@EmployeeAddress nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @searchQuery varchar(300)
SET @searchQuery = 'SELECT [ID]
,[Name]
,[Address]
,[Phone]
,[Pin]
FROM Employee WHERE '
IF len(@EmployeeName) > 0
BEGIN
SET @searchQuery = @searchQuery + '[Name] LIKE ' + char(39) + '%' + @EmployeeName + '%' + char(39)
END
ELSE IF len(@EmployeeAddress) > 0
BEGIN
SET @searchQuery = @searchQuery + '[Address] LIKE ' + char(39) + '%' + @EmployeeAddress + '%' + char(39)
END
EXEC (@searchQuery)
END
---------------------------------------------------------------------------
Execute Procedure
EXEC pr_SearchEmployee '','Cochin'

Wednesday, June 2, 2010

Embed Windows Mediaplayer in a Web Page

<HTML>
<HEAD>
<TITLE>Embed Media player</TITLE>
</HEAD>
<BODY>
<OBJECT id="media1" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-oleobject" width="300" height="300">
<PARAM NAME="URL" VALUE="intro.wmv">
<PARAM NAME="ENABLED" VALUE="True">
<PARAM NAME="AUTOSTART" VALUE="False">
<PARAM name="PLAYCOUNT" value="2">
<PARAM name="VOLUME" value="50">
<PARAM NAME="MUTE" VALUE="False">
<PARAM NAME="FULLSCREEN" VALUE="FALSE">
<PARAM name="UIMODE" value="full">
</OBJECT>
</BODY>
</HTML>