Calling WebService using Javascript :-)

S

smartwebcoder

Again I have Implemented a little thing but useful , this time I have
implemented how to save data using webService through javascript its
very fast :)

/
*****************************************************************************************/
Create Table to save info...

CREATE TABLE [dbo].[tblfav](
[fpid] [bigint] IDENTITY(1,1) NOT NULL,
[postid] [bigint] NULL,
CONSTRAINT [PK_tblfav] PRIMARY KEY CLUSTERED
(
[fpid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY
= OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Create SP to Save Info...

create proc aspx_sp_SaveFav
(
@postid bigint
)
as begin
Insert into tblfav (postid) values (@postid)
select @@IDENTITY
end
/
*******************************************************************************/
Code behind file that contains your ouwn Datasaving Logic... Change as
per your requirements

public static Int64 SaveFav(Int64 PostId)
{
Int64 result;
try
{
DbCommand dbComm =
DManager.DB.GetStoredProcCommand("aspx_sp_SaveFav");
DBManager.DB.AddInParameter(dbComm, "@postid",
DbType.Int64, PostId);
result =
Convert.ToInt64(DBManager.DB.ExecuteScalar(dbComm).ToString());
}
catch (Exception ex)
{
throw ex; ;
}
return result;
}
above function will return latest inserted ID return by SP we have
return above and we will use that no to chk wether the data saved or
not and will display simple alert message :)

/
******************************************************************************************/
Javascript function that will call webservice...

<script language="javascript" type="text/javascript">

function Add(postid)
{
aspx.SaveFav(postid,onComplete,onError);

}
function onComplete(result)
{
alert("successful");
}
function onError(result)
{
alert("Error");
}
</script>

/
************************************************************************************/
Now we will creatw a WebServise that will be called....

using System.Web.Script.Services;
[ScriptService]
public class aspx : System.Web.Services.WebService
{}

[WebMethod]
public Int64 SaveFav(Int64 postid)
{
try
{
Int64 i = Category.SaveFav(postid);
if (i > 0)
{
return 1;
}
else
{
return 0;
}
}
catch (Exception ex)
{
return -1;
}

}
/
**************************************************************************************/

Please modify above code as per your requirements................

Please feel free to comment on this Little post :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top