Wednesday, September 29, 2010

How to call a Web Service from client-side JavaScript using ASP.Net

Today I will describe how to call a Web Service from JavaScript in ASP.Net.
Steps are as follows…
Step 1: First create a new AJAX enabled Web Application.
Step 2: Add new Web Service to the project. Let’s say WebService.asmx
Step 3: To allow this Web Service to be called from script
add the following attribute to the Web Service declaration part:
[System.Web.Script.Services.ScriptService]
Step 4: Now we need a Web Method that we will call from client-side JavaScript. Let us define it like this:
[WebMethod]
public string GetServerResponse(string text)
{
if (text == string.Empty)
throw new Exception("Web Service Exception: Enter some text");
return string.Format("Web Service Success: {0} " , DateTime.Now.ToString());
}
Step 5: Add ScriptManager in our .aspx page.
<form id="form1" runat="server" >
<asp:ScriptManager ID="ScriptManager1" runat="server" >
</asp:ScriptManager >
</form >


Step 6: Add ServiceReference to the Services collection and specify Path to the desired service.

<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Services >
<asp:ServiceReference Path="~/WebService.asmx" />
</Services >
</asp:ScriptManager >


Step 7: Now create the javascript function as follows

<script language="javascript" type="text/javascript">
function SendRequest() {
var val = document.getElementById('<%= txtName.ClientID %>').value;
WebService.GetServerResponse(val, OnComplete, OnError, OnTimeOut);
return false;
}

function OnComplete(arg)
{
alert(arg);
}

function OnTimeOut(arg)
{
alert("timeOut has occured");
}

function OnError(arg)
{
alert("error has occured: " + arg._message);
}
</script >

Step 8: To call this javascript create a textbox and a button in your aspx Page.


<asp:textbox id="txtName" runat="server" >
</asp:textbox >
<asp:button id="btnCallService" runat="server" text="Call Service" onclientclick="javascript:return SendRequest();" ></asp:button >

Sunday, September 5, 2010

How to track if any table is deleted/drop from SQL Server database by user

Today I will post one more interesting thing in sql server database.i.e. How to track if any table is deleted/drop from SQL Server database by user….

use DB_name

CREATE TABLE create_table_log
( create_time datetime
, DB_User nvarchar(100)
, Event_type nvarchar(100)
, TSQL nvarchar(2000));
use Db_name
go
create trigger trig_create_table on database for create_table
as
Declare @data xml
set @data=Eventdata()
Insert into create_table_log values
(getdate(),
@data.value('(/EVENT_INSTANCE/LOGINNAME)[1]','nvarchar(100)'),
@data.value('(/EVENT_INSTANCE/EVENTTYPE)[1]','nvarchar(100)'),
@data.value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(200)')
)

Saturday, June 26, 2010

Resizing images without loss of quality in .Net

Recently I had faced a problem regarding photos that I had captured using my digicam.
When I capture photo using my cannon digicam, the size of each photo is 2MB or more.
So it is very difficult to upload those photos in web because of their size.
And compress those photos one by one using Microsoft Office Picture Manager is difficult too.
So I wrote some code to resize those photos and to minimize the size without affecting the photo quality.
The code is as follow.........

public void ResizeFromStream(string ImageSrcPath, string ImageSavePath, int MaxSideSize)
{
try
{
int intNewWidth;
int intNewHeight;
Image imgInput = Image.FromFile(ImageSrcPath);

//Determine image format
ImageFormat fmtImageFormat = imgInput.RawFormat;

//get image original width and height
int intOldWidth = imgInput.Width;
int intOldHeight = imgInput.Height;

//determine if landscape or portrait
int intMaxSide;

if (intOldWidth >= intOldHeight)
{
intMaxSide = intOldWidth;
}
else
{
intMaxSide = intOldHeight;
}


if (intMaxSide > MaxSideSize)
{
//set new width and height
double dblCoef = MaxSideSize / (double)intMaxSide;
intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
}
else
{
intNewWidth = intOldWidth;
intNewHeight = intOldHeight;
}
//create new bitmap
Bitmap bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight);

//save bitmap to disk
bmpResized.Save(ImageSavePath, fmtImageFormat);

//release used resources
imgInput.Dispose();
bmpResized.Dispose();
}
catch (Exception ex)
{ }
}

Tuesday, May 25, 2010

Error in Web Service: The test form is only available for requests from the local machine

I have faced a problem when try to access a Web service from a remote computer I cannot see the Invoke button. And received the following error message:

The test form is only available for requests from the local machine

The reason is .NET-connected Web services support HTTP GET, HTTP POST and SOAP protocols. By default, in .NET Framework 1.0, all three protocols are enabled. By default, in .NET Framework 1.1, HTTP GET and HTTP POST are both disabled for security reasons.

Applications that use HTTP GET or HTTP POST to invoke a Web service fail when the Web service is upgraded to .NET Framework 1.1.

The .NET Framework 1.1 defines a new protocol that is named HttpPostLocalhost. By default, this new protocol is enabled. This protocol permits invoking Web services that use HTTP POST requests from applications on the same computer.

To solve this problem we have to enable HTTP GET and HTTP POST by editing the Web.config file where the Web service resides.

<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>

Thursday, April 22, 2010

How to keep session alive in your site

Some days back I faced a problem of session lose. Today I am writing an article how to keep session alive in asp.net without increasing session time out for the site.
The steps are as follows
First create an aspx Page e.g SessionAlive.aspx.
Then write one line in code behind.

protected void Page_Load(object sender, System.EventArgs e)
{
Response.AddHeader("Refresh", Convert.ToString(( Session.Timeout * 60 ) -120 ));
}

Now in your Master Page add this line
<iframe id=”MaintainSession” src="SessionAlive.aspx" width=0 height=0 runat="server"></ iframe >

Now you can maintain Session as long as the website is open.