如何返回字符串web方法webservice错误请帮帮我? [英] How to return into string web method webservice error please help me?
本文介绍了如何返回字符串web方法webservice错误请帮帮我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
how to return into void web method webservice error please help me?
<pre>[WebMethod]
public void GetDaywiseData(string dep)
{
try
{
List<DataBind.DaywiseBind> binddata = new List<DataBind.DaywiseBind>();
string cs = ConfigurationManager.ConnectionStrings["sa"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = " select a.doctor_nm AS DOCTORNAME,[dbo].[fn_docdays](doc_id) as DAYS,b.dept_nm AS DEPARTMENT from (select * from doctor_master) as a inner join (select * from deptmaster)as b on a.dept_cd = b.dept_cd where b.dept_nm = '"+dep+"'";
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
DataBind.DaywiseBind getdata = new DataBind.DaywiseBind();
getdata.doctorname = rdr["DOCTORNAME"].ToString();
getdata.days = rdr["DAYS"].ToString();
getdata.department = rdr["DEPARTMENT"].ToString();
binddata.Add(getdata);
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(binddata));
}
catch(Exception exc)
{
// MessageBox.Show("An error happened while processing your call");
}
我尝试过:
What I have tried:
i have bind gridview when consume webservice pass parameter value(dep) Error Cannot implicitly convert type 'void' to 'object'
ServiceReference2.GMCHWebServiceSoapClient srv = new ServiceReference2.GMCHWebServiceSoapClient();
// var getdata = srv.GetDaywiseData(dep1);
grdshowdata.DataSource = srv.GetDaywiseData(dep1); //error
grdshowdata.DataBind();
推荐答案
您的方法签名不返回值,因此您无法检索字符串。
方法签名是以下部分...
Your method signature does not return a value, hence you cannot retrieve a string.
The method signature is the below part...
public void GetDaywiseData(string dep)
关键字 void 表示不返回值 - 在VB6中,这称为Sub。
要返回值,您将更改方法签名以标识返回值的类型 - 以下返回字符串值;
The keyword void means that a value will not be returned - in VB6 this is known as a Sub.
To return a value you would change your method signature to identify the type of return value - the below returns a string value;
public string GetDaywiseData(string dep)
根据上面的代码,最简单的解决方案如下:
Based on the code above, the easiest solution is as follows;
[WebMethod]
public DataTable GetDaywiseData(string dep)
{
try
{
DataTable dtReturn;
string cs = ConfigurationManager.ConnectionStrings["sa"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = " select a.doctor_nm AS DOCTORNAME,[dbo].[fn_docdays](doc_id) as DAYS,b.dept_nm AS DEPARTMENT from (select * from doctor_master) as a inner join (select * from deptmaster)as b on a.dept_cd = b.dept_cd where b.dept_nm = '"+dep+"'";
con.Open();
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(dtReturn);
}
return dtReturn;
}
数据表将从WebService返回,然后将其设置为Grid的数据源
亲切的问候
The datatable will be returned from the WebService, which is then set as the datasource of your Grid
Kind Regards
这篇关于如何返回字符串web方法webservice错误请帮帮我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文