I am getting an error near return errmsg(ex);
:
cannot implicitly convert type string to system.collections.generic.dictionary>
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
namespace Webservice
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[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
{
public Service1()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
//public string GetEmployees(string SearchTerm)
private static Dictionary<string, Dictionary<string, object>> DatatableToDictionary(DataTable dt)
{
var cols = dt.Columns.Cast<DataColumn>();
return dt.Rows.Cast<DataRow>()
.ToDictionary(r => dt.Rows.IndexOf(r).ToString(),
r => cols.ToDictionary(c => c.ColumnName, c => r[c.ColumnName]));
}
public Dictionary<string, Dictionary<string, object>> GetEmployees()
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand();
//cmd.CommandText = "SELECT * FROM Contact e WHERE FirstName LIKE '%" + SearchTerm + "%'";
cmd.CommandText = "SELECT * FROM Contact e ";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.Connection = con;
da.Fill(ds);
con.Close();
return DatatableToDictionary(ds.Tables[0]);
}
catch (Exception ex)
{
***return errmsg(ex);***
}
}
public string errmsg(Exception ex)
{
return "[['ERROR','" + ex.Message + "']]";
}
}
}
Thanks in advance.
You cannot return a string from method with return type Dictionary<string, Dictionary<string, object>>
public Dictionary<string, Dictionary<string, object>> GetEmployees()
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Contact e ";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.Connection = con;
da.Fill(ds);
con.Close();
return DatatableToDictionary(ds.Tables[0]);
}
catch (Exception ex)
{
// Here you are returning a string, the method signature is Dictionary<string, Dictionary<string, object>>
return errmsg(ex);
}
}
Instead of catching the Exception
and returning the exception message, let the calling code handle the exception further up the call stack. So you'd have something like:
public Dictionary<string, Dictionary<string, object>> GetEmployees()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Contact e ";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.Connection = con;
da.Fill(ds);
con.Close();
return DatatableToDictionary(ds.Tables[0]);
}
The following code solve my issues:
public string GetEmployees()
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NSConstr"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Contact e ";
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand.Connection = con;
da.Fill(dt);
con.Close();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;
foreach (DataRow rs in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, rs[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
public string errmsg(Exception ex)
{
return "[['ERROR','" + ex.Message + "']]";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.