Hi Sarasalvi,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespaces
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
TempData["Single"] = superadminlogin("Tacoma");
TempData["Multiple"] = superadminlogin("Seattle");
return View();
}
public string superadminlogin(string city)
{
RootObject root = new RootObject();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
List<Response> responses = new List<Response>();
Response response = new Response();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT EmployeeId,FirstName + ' ' + LastName Name FROM Employees WHERE City = @City";
cmd.Parameters.AddWithValue("@City", city);
cmd.Connection = con;
con.Open();
SqlDataReader returnreader = cmd.ExecuteReader();
if (returnreader != null)
{
List<datalist> datalists = new List<datalist>();
while (returnreader.Read())
{
datalists.Add(new datalist()
{
id = Convert.ToInt32(returnreader["EmployeeId"]),
name = returnreader["Name"].ToString()
});
}
response.more = new More { status = "Ok", message = "Data Found" };
response.data = datalists;
}
con.Close();
}
responses.Add(response);
root.Response = responses;
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(root);
}
}
public class datalist
{
public int id { get; set; }
public string name { get; set; }
}
public class More
{
public string status { get; set; }
public string message { get; set; }
}
public class Response
{
public More more { get; set; }
public List<datalist> data { get; set; }
}
public class RootObject
{
public List<Response> Response { get; set; }
}
}
View
<html>
<head>
<title>Index</title>
</head>
<body>
<%: TempData["Single"]%> <br />
<%: TempData["Multiple"]%>
</body>
</html>
Output
{"Response":[{"more":{"status":"Ok","message":"Data Found"},"data":[{"id":2,"name":"Andrew Fuller"}]}]}
{"Response":[{"more":{"status":"Ok","message":"Data Found"},"data":[{"id":1,"name":"Nancy Davolio"},{"id":8,"name":"Laura Callahan"}]}]}