Click here to Skip to main content
15,887,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I want to display data from sql server to w2ui grid by converting data into json format but I got an error "data is not in a valid json format" ...please help me how can i dispaly remote data into w2ui grid
Thanks in advance..

code in w2uigrid.aspx in which i have declared the w2ui grid
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="databind.aspx.cs" Inherits="databind" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script type="text/javascript" src="http://w2ui.com/src/w2ui-1.4.min.js"></script>
  <link rel="stylesheet" type="text/css" href="http://w2ui.com/src/w2ui-1.4.min.css" />
</head>
<body>

    <div id="grid" style="width: 100%; height: 350px;"></div>

<script type="text/javascript">
    $(function () {
        $('#grid').w2grid({
            name: 'grid',

          url:'json.aspx/ConvertDataTabletoString',
           
           columns: [
            { field: 'recid', caption: 'recid', size: '30%' },

            { field: 'fname', caption: 'First Name', size: '30%' },
            { field: 'lname', caption: 'Last Name', size: '30%' },
            { field: 'email', caption: 'Email', size: '40%' },
           { field: 'sdate', caption: 'sdate', size: '40%' }

            ]
          
        });
    });

</script>

</body>
</html>


Code in json.aspx.cs in which i have written code for converting data into json format
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class json : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var c=ConvertDataTabletoString();

}


public DataTable ConvertDataTabletoString()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["databoundConnectionString"].ConnectionString);

SqlCommand cmd = new SqlCommand("JSONProcas2", con);
var x= cmd.CommandType=CommandType.StoredProcedure;

con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}
}
Stored procedure is
SQL
create procedure [dbo].[JSONProcas2]
as
begin
select '{records:[' + STUFF((
           select
            ',{"recid":' + cast(recid as varchar(max))
            +',"id":' + cast(stuid as varchar(max))
             + ',"name":"' + stuname + '"'
             +'}'

              from student2 t1
              for xml path(''), type

             ).value('.', 'varchar(max)'), 1, 1, '') + ']}'
end
Posted
Updated 6-Aug-14 23:28pm
v2

1 solution

Your method does not return a JSON object, it returns a datatable. You need to serialize the datatable to JSON object.

http://www.aspdotnet-suresh.com/2013/05/c-convert-datatable-to-json-string-in-c.html[^]
 
Share this answer
 
Comments
Lavanya Jujjavarapu 7-Aug-14 9:12am    
Thanks for giving me the solutions...but by this way also i got an error "data is not in json format"


public string ConvertDataTabletoString()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["databoundConnectionString"].ConnectionString);

SqlCommand cmd = new SqlCommand("select recid,stuid,stuname,stuaddr from student2", con);


con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<dictionary<string, object="">> rows = new List<dictionary<string, object="">>();
Dictionary<string, object=""> row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object="">();

foreach (DataColumn col in dt.Columns)
{

row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}


json format must be in this format as shown below

{records:[{"recid":1,"stuid":100,"stuname":"lavanya","stuaddr":"banglore"},{"recid":2,"stuid":101,"stuname":"kk","stuaddr":"chennai"}]}

but by the above code i got like this format

[{"recid":1,"stuid":100,"stuname":"lavanya","stuaddr":"banglore"},{"recid":2,"stuid":101,"stuname":"kk","stuaddr":"chennai"}]

please give me the solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900