Hi,
I am trying to call webservice method to get the string, but nothing happening, it wont display it in the label.
Can u help?? plz
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
namespace WebService
{
/// Summary description for Service1
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
//[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string GetCurrencyRate(string FromCurrencyCode, string ToCurrencyCode)
{
string Rate = "";
//Create connection
SqlConnection con = new SqlConnection(@"Data Source=Comp1;Initial Catalog=CurrencyExchange;Integrated Security=True;");
//Instantiate connection
SqlDataReader rdr = null;
try
{
con.Open();
SqlCommand cmd = new SqlCommand("spGetRate", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@FromCurrencyCode", SqlDbType.Char)).Value = FromCurrencyCode;
cmd.Parameters.Add(new SqlParameter("@ToCurrencyCode", SqlDbType.Char)).Value = ToCurrencyCode;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Rate = rdr["Rate"].ToString();
}
//Close Connection
rdr.Close();
con.Close();
return Rate;
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (con != null)
{
con.Close();
}
}
}
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebService
{
public partial class Dafault : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
localhost.Service1 CallWebService = new localhost.Service1();
string sGetValue = CallWebService.GetCurrencyRate(ddlFromCurrency.SelectedItem.Text, ddlToCurrency.SelectedItem.Text);
lbDisplay.Text = sGetValue;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebService.Default" %>
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
namespace WebService
{
/// Summary description for Service1
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
//[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string GetCurrencyRate(string FromCurrencyCode, string ToCurrencyCode)
{
string Rate = "";
//Create connection
SqlConnection con = new SqlConnection(@"Data Source=Comp1;Initial Catalog=CurrencyExchange;Integrated Security=True;");
//Instantiate connection
SqlDataReader rdr = null;
try
{
con.Open();
SqlCommand cmd = new SqlCommand("spGetRate", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@FromCurrencyCode", SqlDbType.Char)).Value = FromCurrencyCode;
cmd.Parameters.Add(new SqlParameter("@ToCurrencyCode", SqlDbType.Char)).Value = ToCurrencyCode;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Rate = rdr["Rate"].ToString();
}
//Close Connection
rdr.Close();
con.Close();
return Rate;
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (con != null)
{
con.Close();
}
}
}
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebService
{
public partial class Dafault : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
localhost.Service1 CallWebService = new localhost.Service1();
string sGetValue = CallWebService.GetCurrencyRate(ddlFromCurrency.SelectedItem.Text, ddlToCurrency.SelectedItem.Text);
lbDisplay.Text = sGetValue;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebService.Default" %>
Guest UserPosted May 5, 2015, 4:19 PM
I have it solved, my webservice was expecting char(3) string for its method and I was sending selected value from ddlist which was different string.
thanks
Sakthikumar ThanavelPosted May 5, 2015, 8:48 AM
Rate="Error". you can find problem.
Guest UserPosted May 4, 2015, 3:38 PM
Sakthikumar ThanavelPosted May 4, 2015, 12:12 PM
You only return the value inside try block.
1.try to put the return statement outside of try catch or both try and catch.
try
{
return value;
}
catch(Exception ex)
{
return value;
}
2. put return statement in finally block.
[WebMethod]
public string GetCurrencyRate(string FromCurrencyCode, string ToCurrencyCode)
{
string Rate = "";
//Create connection
SqlConnection con = new SqlConnection(@"Data Source=Comp1;Initial Catalog=CurrencyExchange;Integrated Security=True;");
//Instantiate connection
SqlDataReader rdr = null;
try
{
con.Open();
SqlCommand cmd = new SqlCommand("spGetRate", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@FromCurrencyCode", SqlDbType.Char)).Value = FromCurrencyCode;
cmd.Parameters.Add(new SqlParameter("@ToCurrencyCode", SqlDbType.Char)).Value = ToCurrencyCode;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Rate = rdr["Rate"].ToString();
}
//Close Connection
rdr.Close();
con.Close();
return Rate;
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (con != null)
{
con.Close();
}
}
Note: Your method will throw compile time error. because the method did not return string except the try block.
Anubhav ChaudharyPosted May 4, 2015, 12:53 AM