Get JSON Data formate In Asp.net
(Without any third party libary file)
Steps 1: Nessary Namespace __ using System.Web.Script.Serialization;
Steps 2: put the Folloing code__
[WebMethod]
public void getAllTheData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
con.Open();
List<employee> emp = new List<employee>();
SqlCommand cmd = new SqlCommand("spgetData", con);
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
emp.Add(new employee
{
name = sdr["name"].ToString(),
year = sdr["year"].ToString(),
total = Convert.ToInt32( sdr["total"].ToString())
});
}
}
con.Close();
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(emp));
}
public class employee
{
public string name { get; set; }
public string year { get; set; }
public int total { get; set; }
}
Description
Here the class is employee ... Its is a example of consume data in a webmethod
Introduction : In this article i am going to share about how to copy a datatable to a new datatable object , and how to add a new row to the datatable. Overview : Generally we used datatable object to store out table data, sometime we want to store that datatable temporally to a another datatable object that time we need to copy the datatable and store to newly created object . It's a best practice to copy our main datatable when we need that datatable multiple times and used the copyed object. We can add a new column dynamically to the newly created object. Code Example : Syntex : DataTable dest = MainDatatable; Connection String : <connectionStrings> <add name="conn" connectionString="Data Source=anil\SQLEXPRESS2012;Initial Catalog=MyDB; User ID=sa;Password=sa1" providerName="System.Data.SqlClient" /> </connectionStrings> C# Code : SqlConnection con = new SqlConnec...
Comments
Post a Comment