Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting no errors but the [WebMethod] is returning all "Nulls". This means my Serialization is not right.

My goal is to produce a return json2:
C#
Json2 = {ID:someID, datetime:somedatetime, col1:somecol1, col2:somecol2, col3:somecol3};

Here is my code:
C#
[System.Web.Services.WebMethod]
public static string GetJSONdata(string ID)
{
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    string connStr = ConfigurationManager.ConnectionStrings["jsonobject"].ConnectionString;
    string cmdStr = "SELECT ([idd],[datetime],[col1],[col2],[col3]) FROM [jsondata] WHERE [idd]=@idd;";
    try
    {
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
            {
                conn.Open();
                cmd.Parameters.AddWithValue("@idd", ID);
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                    dt = ds.Tables[0];
                }
            }
        }
    }
    catch (Exception ex)
    {

    }
    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
    Dictionary<string, object> row;
    foreach (DataRow dr in dt.Rows)
    {
        row = new Dictionary<string, object>();
        foreach (DataColumn col in dt.Columns)
        {
            row.Add(col.ColumnName, dr[col]);
        }
        rows.Add(row);
    }
    var json2 = serializer.Serialize(rows);
    return json2;
}
Posted

1 solution

since you are swallowing exception you can't say the issue is on serialization. I have tested you code with dummy data table and it was successfully returned json string.
Remove the try catch block or debug the web service method and check the error you received, then only you can identify the issue correctly but unfortunately we can't debug your code.
 
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