I want to create a .NET webservice using C# with JSON. but I’ve error in my return statement on my webservice, cannot implicitly convert type 'string' to 'Model.User'
how to resolve that error line?
Here is my WebService.asmx :
[OperationContract]
public Model.User GetUser(string ccduser)
{
Model.User user = new Model.User();
string connectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString)) {
string sql = "Select * from ms_webuser where ccduser = '" + ccduser + "'";
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
user.cnmuser = reader["cnmuser"].ToString();
}
}
MemoryStream stream = new MemoryStream();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Model.User));
serializer.WriteObject(stream, user);
stream.Position = 0;
StreamReader streamReader = new StreamReader(stream);
return streamReader.ReadToEnd(); //here is the error red line
}
Here is my Model.User Class:
namespace Model
{
public class User
{
private string _ccduser;
private string _cnmuser;
public string ccduser
{
get { return _ccduser; }
set { _ccduser = value; }
}
public string cnmuser
{
get { return _cnmuser; }
set { _cnmuser = value; }
}
}
}
You get that error because the
ReadToEndmethod returns a string and you have declared your method to return aModel.UserTo resolve it you should return the user you want to return instead or you should change the method to return a string.