Hi Rancho079,
Check this example. Now please take its reference and correct your code.
Using the DLL from here http://james.newtonking.com/json/help/index.html
SQL
CREATE TABLE CustomerJSON
(
Id INT PRIMARY KEY IDENTITY,
JsonData NVARCHAR(MAX)
)
HTML
<div>
<table>
<tr>
<td>
Id
</td>
<td>
<asp:TextBox ID="txtId" runat="server" />
</td>
</tr>
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td>
Country
</td>
<td>
<asp:TextBox ID="txtCountry" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button Text="Insert" runat="server" OnClick="Insert" />
</td>
</tr>
</table>
<br />
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</div>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
VB.Net
Imports System.Data
Imports Newtonsoft.Json
Imports System.Data.SqlClient
Imports System.Web.Script.Serialization
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid(GetJsonData());
}
}
private void BindGrid(string json)
{
DataTable dt = JsonConvert.DeserializeObject<DataTable>("[" + json + "]");
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
private string GetJsonData()
{
string json = "";
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("SELECT JsonData FROM CustomerJSON", con);
con.Open();
json = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
return json;
}
protected void Insert(object sender, EventArgs e)
{
Customer customer = new Customer();
customer.CustomerId = Convert.ToInt32(txtId.Text.Trim());
customer.Name = txtName.Text.Trim();
customer.Country = txtCountry.Text.Trim();
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonString = serializer.Serialize(customer);
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("INSERT INTO CustomerJSON VALUES(@JsonData)", con);
cmd.Parameters.AddWithValue("@JsonData", jsonString);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
BindGrid(GetJsonData());
}
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindGrid(GetJsonData())
End If
End Sub
Private Sub BindGrid(ByVal json As String)
Dim dt As DataTable = JsonConvert.DeserializeObject(Of DataTable)("[" & json & "]")
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Sub
Private Function GetJsonData() As String
Dim json As String = ""
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand("SELECT JsonData FROM CustomerJSON", con)
con.Open()
json = Convert.ToString(cmd.ExecuteScalar())
con.Close()
End Using
Return json
End Function
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim customer As Customer = New Customer()
customer.CustomerId = Convert.ToInt32(txtId.Text.Trim())
customer.Name = txtName.Text.Trim()
customer.Country = txtCountry.Text.Trim()
Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
Dim jsonString As String = serializer.Serialize(customer)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand("INSERT INTO CustomerJSON VALUES(@JsonData)", con)
cmd.Parameters.AddWithValue("@JsonData", jsonString)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
BindGrid(GetJsonData())
End Sub
Public Class Customer
Public Property CustomerId As Integer
Public Property Name As String
Public Property Country As String
End Class
Screenshot
