ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Return JSON string from Database in ASP.Net MVC<p>Hi&nbsp;<a class="username" rel="Sarasalvi">Sarasalvi,</a></p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>For this example I have used of Northwind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-Microsoft-Northwind-and-Pubs-Sample-databases-in-SQL-Server-Management-Studio.aspx" target="_blank">Download Northwind Database</a></p> <p><strong><span style="text-decoration: underline;">Namespaces </span></strong></p> <pre class="brush: csharp">using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Web.Script.Serialization;</pre> <p><strong><span style="text-decoration: underline;">Controller</span></strong></p> <pre class="brush: csharp">public class HomeController : Controller { // GET: /Home/ public ActionResult Index() { TempData[&#34;Single&#34;] = superadminlogin(&#34;Tacoma&#34;); TempData[&#34;Multiple&#34;] = superadminlogin(&#34;Seattle&#34;); return View(); } public string superadminlogin(string city) { RootObject root = new RootObject(); using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString)) { List&lt;Response&gt; responses = new List&lt;Response&gt;(); Response response = new Response(); using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = &#34;SELECT EmployeeId,FirstName + &#39; &#39; + LastName Name FROM Employees WHERE City = @City&#34;; cmd.Parameters.AddWithValue(&#34;@City&#34;, city); cmd.Connection = con; con.Open(); SqlDataReader returnreader = cmd.ExecuteReader(); if (returnreader != null) { List&lt;datalist&gt; datalists = new List&lt;datalist&gt;(); while (returnreader.Read()) { datalists.Add(new datalist() { id = Convert.ToInt32(returnreader[&#34;EmployeeId&#34;]), name = returnreader[&#34;Name&#34;].ToString() }); } response.more = new More { status = &#34;Ok&#34;, message = &#34;Data Found&#34; }; 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&lt;datalist&gt; data { get; set; } } public class RootObject { public List&lt;Response&gt; Response { get; set; } } } </pre> <p><strong><span style="text-decoration: underline;">View</span></strong></p> <pre class="brush: html">&lt;html&gt; &lt;head&gt; &lt;title&gt;Index&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;%: TempData[&#34;Single&#34;]%&gt; &lt;br /&gt; &lt;%: TempData[&#34;Multiple&#34;]%&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong><span style="text-decoration: underline;">Output</span></strong></p> <p>{"Response":[{"more":{"status":"Ok","message":"Data Found"},"data":[{"id":2,"name":"Andrew Fuller"}]}]} </p> <p>{"Response":[{"more":{"status":"Ok","message":"Data Found"},"data":[{"id":1,"name":"Nancy Davolio"},{"id":8,"name":"Laura Callahan"}]}]}&nbsp;</p>https://www.aspforums.net:443/Threads/167050/Return-JSON-string-from-Database-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/167050/Return-JSON-string-from-Database-in-ASPNet-MVC/Mon, 25 Feb 2019 03:20:51 GMT