Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i try to return data in web method using linq .. and also i bind grid view in jquery

What I have tried:

UPDATED CODE

JavaScript
 <script type="text/javascript">

var strArray = "[['sfdsdfLi', 9],['Kiwsdfi', 3],['Mixesdfd nuts', 1],['Oranges', 6],['Grapes (bunch)', 1]]";
$(function () {
$('[ID*=search_data]').on('click', function () {
var from = $('[ID*=fromdate]').val();
var to = $('[ID*=todate]').val();
var reg = $('[ID*=regiondrop] option:selected')[0].value;
var obj = {};
obj.fdate = from;
obj.tdate = to;
obj.region = reg;
Getdata(obj);
return false;
});
});

$(function () {
$('[ID*=search_data]').on('click', function () {
var fromdate = $('[ID*=fromdate]').val();
var todate = $('[ID*=todate]').val();
var regiondrop = $('[ID*=regiondrop] option:selected')[0].value;
var GridView1 = $('[ID*=GridView1]');
var obj = {};
obj.fdate = from;
obj.tdate = to;
obj.region = reg;
Getdataa(obj);
});
});

function Getdataa(obj) {
//alert('1');
$.ajax({
type: "POST",
url: "WebForm1.aspx/search_data",
data: "{'fromdate':'" + fromdate + "','todate':'" + todate + "','regiondrop':'" + regiondrop + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (result) {
$("#GridView1").empty();
if (result.d.length > 0) {
$("#GridView1").append(
"<tr><th>Owner Name</th><th>Reg No</th><th>Mileage Accumlation</th><th>MaxSpeed</th>");

for (var i = 0; i < result.d.length; i++) {
$("#GridView1").append("<tr><td>" +
data.d[i].OwnerName + "</td> <td>" +
data.d[i].RegNo + "</td> <td>" +
data.d[i].MileageAccumlation + "</td> <td>" +
data.d[i].MaxSpeed + "</td></tr>");
}
}
else {
$("#GridView1").hide();
$("#Label1").text("No Data");
}
},
error: function (error) {
alert("error");

}
});
}
function Getdata(obj) {
//alert('1');
$.ajax({
type: "POST",
url: "WebForm1.aspx/GetVo",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (result) {

//alert(result.d);
//alert('2');
strarr = result.d;
var myarr = strarr;
Drewchart(myarr);
// alert(myarr);
// alert('3');

},
error: function (error) {
alert("error");

}
});
}

function Drewchart(result) {

$('#container').highcharts({
chart: {
type: 'pie',
options3d: {
enabled: true,
alpha: 45
}
},
title: {
text: 'Contents of Charts'
},
subtitle: {
text: '3D donut in Highcharts'
},
plotOptions: {
pie: {
innerSize: 100,
depth: 45
}
},
series: [{
name: 'Delivered amount',
data: result
}]

});
}

webmthod

C#
[WebMethod]
       [WebMethod]
public static DataTable search_data(DateTime fromdate, DateTime todate, string regiondrop)
{

try
{

TrackDataEntities1 ts = new TrackDataEntities1();
var dq = (from vv in ts.tblVe
join rv in ts.tblReg on vv.ID equals rv.ID
join re in ts.tblRegi on rv.RID equals re.RID
where
re.Region == regiondrop
&& re.StartDate <= fromdate
&& re.EndDate >= todate
orderby
vv.ID,
rv.OwnerName
select new
{
ID = vv.ID,
oname = rv.OName,
RegNo = rv.RegNo,
Speed = rv.Speed,
});

DataTable dt = new DataTable();
dt.Rows.Add(dq);
return dt;


}
catch (Exception)
{
throw new Exception();


}

}


this show error

WebForm1.aspx:70 Uncaught ReferenceError: regiondrop is not defined
http://localhost:33578/WebForm1.aspx/GetVo Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Posted
Updated 15-Jul-16 2:42am
v3
Comments
Karthik_Mahalingam 15-Jul-16 3:08am    
why you need to convert list to dataset?
super_user 15-Jul-16 3:29am    
because i want to display data in gridview
Karthik_Mahalingam 15-Jul-16 4:13am    
you shall display using list also, it will work.
super_user 15-Jul-16 4:49am    
howw

1 solution

There are two problems here: the first is that dq is a List<T> - thanks to the ToList call at the end of your Linq - and we have no idea what it is a List of!
It is possible to convert a List<T> to a DataTable without knowing what type of object it contains: Converting a List to a DataTable[^] does it quite nicely.
You could then create a DataSet and an add the DataTable to that.

The second is that your method expects to return a string - and there is no simple conversion from a List, DataTable, or DataSet to a string. So either you need to revise your method return type, or you probably don't want to try and do anything with a DataTable or DataSet anyway!

I don't know what you are trying to achieve here, so I can't say "do this and it'll all work" - but this should give you the tools to sort it out in your own mind.
 
Share this answer
 
Comments
super_user 15-Jul-16 3:29am    
i try to achieve display data is girdview so for this now i use datatable instead of string..
OriginalGriff 15-Jul-16 3:47am    
If all you are trying to do is set the DataSource of a GridView then you don't need a DataSet or DataTable at all - you can use the List directly, provided your class holds the values you are trying to display as properties rather than fields.
super_user 15-Jul-16 4:50am    
in dq there is json data now i try to display this data in gridview
OriginalGriff 15-Jul-16 5:09am    
Returning an anonymous type from Linq as the generic for the List would make that difficult - if you are doing that, then use the link I gave to convert it to a DataTable and return that so you can set your GridView.
But if you create a List of a defined internal class in your Linq, then just use that instead - it saves time, memory, and effort!
super_user 15-Jul-16 6:49am    
in dq when i check data is .. [0] = { ID = 1, OName = "Khan", RegNo = "AJ-24",Speed = "124" } [1] = { ID = 2, OName = "Shah", RegNo = "AL-91",Speed = "95" }

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