elpeyotl
elpeyotl

Reputation: 372

Create Json Array in C# with data from SQL

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
    }
}
}

Upvotes: 2

Views: 3190

Answers (4)

Vincent
Vincent

Reputation: 882

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

Upvotes: 0

elpeyotl
elpeyotl

Reputation: 372

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!

Upvotes: 0

Nagaraj
Nagaraj

Reputation: 231

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.

Upvotes: 2

Oofpez
Oofpez

Reputation: 514

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

Upvotes: 2

Related Questions