2

I have to get Data from SQL and send it as JSON to the frontend of the app. I'm not good in C# so I was Messing around with it. At the moment I get not Valid JSON:

  {"name":"Perez","company":"test"}{"name":"Jespersen","company":"Codeparc"}

As you can see it is not coming as an Array. How can I achieve that? My Code:

 using System;
 using System.Data.SqlClient;
 using Newtonsoft.Json;

 namespace eltklt_webapp
{
//Create the Client Object
public class Client
{
    public string name;
    public string company;

}
public partial class getData : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["type"] == "clients")
        {
                // SQL Server Read Statement Sample
                using (SqlConnection conn = new SqlConnection(Gizmos.getConnectionString()))
            {
                SqlCommand command = new SqlCommand("select * from clients", conn);
                conn.Open();

                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Client Client = new Client();
                        Client.name = reader["name"].ToString();
                        Client.company = reader["company"].ToString();
                        string jsonString = JsonConvert.SerializeObject(Client);
                        Response.Write(jsonString);
                    }
                }
            }
        Response.End();
        }//end if Clients
    }
}
}

4 Answers 4

2
protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["type"] == "clients")
    {
            List<Client> clients=new List<Client>();
            // SQL Server Read Statement Sample
            using (SqlConnection conn = new SqlConnection(Gizmos.getConnectionString()))
        {
            SqlCommand command = new SqlCommand("select * from clients", conn);
            conn.Open();

            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Client Client = new Client();
                    Client.name = reader["name"].ToString();
                    Client.company = reader["company"].ToString();
                    clients.Add(Client);
                    //string jsonString =                     JsonConvert.SerializeObject(Client);

                }
            }
        }
      var jsonString = JsonConvert.SerializeObject(clients);
      Response.Write(jsonString);
    Response.End();
    }//end if Clients
}

Hope this could give you basic idea.

Sign up to request clarification or add additional context in comments.

Comments

2

Have you tried to put your Client objects into an array or list and call JsonConvert.SerializeObject on the list?

Comments

0

Thanks! adding

 List<Client> clients=new List<Client>();

Doing the while loop and pushing the while into the client list with:

                clients.Add(Client);

Then Serialize it, solved the problem! Thank you guys!

Comments

0

If you can use SQL 2016 there is a much better way.

SELECT name, company from clients FOR JSON PATH

There is also a nice framework for streamlining the new JSON functionality in SQL 2016 here: https://github.com/ahhsoftware/EzAdoVSSolution

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.