Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to return into  void web method webservice error please help me?



C#
<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' 
C#
ServiceReference2.GMCHWebServiceSoapClient srv = new ServiceReference2.GMCHWebServiceSoapClient();

        //  var getdata = srv.GetDaywiseData(dep1);


          grdshowdata.DataSource = srv.GetDaywiseData(dep1); //error
          grdshowdata.DataBind();
Posted
Updated 17-Apr-18 18:41pm
v4
Comments
Richard MacCutchan 16-Apr-18 4:14am    
At a guess, it means that variable dep1 has not been initialised. I cannot see a reference to it anywhere in your code above.
F-ES Sitecore 16-Apr-18 4:31am    
I don't see anywhere that you "return" a string from the function?
Nishant.Chauhan80 16-Apr-18 5:23am    
sorry sir void instead of string into method
Nishant.Chauhan80 16-Apr-18 5:22am    
dropdownlist value into dep1
my error "Cannot implicitly convert type 'void' to 'object' "

1 solution

Your method signature does not return a value, hence you cannot retrieve a string.
The method signature is the below part...
C#
public void GetDaywiseData(string dep)

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;
C#
public string GetDaywiseData(string dep)

Based on the code above, the easiest solution is as follows;
C#
[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;
}

The datatable will be returned from the WebService, which is then set as the datasource of your Grid

Kind Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900