Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi belo is my code.
I m returning data from database in data table. and getting an excetion of while converting data table to list



XML
Unable to cast object of type 'System.Data.DataRow' to type 'JSONWebService.t_country_cmn'.




[WebMethod]
        public List<t_country_cmn> getcountrybyid()
        {
            DataTable dt = _obj.CountryList();
            List<t_country_cmn> rows = dt.Rows.Cast<t_country_cmn>().ToList();
            return rows;
        }</pre>




Following is my sample json data returned from web service:

{
"getsearchresultdataResult": [
{
"address_text": "indore54 vijaynagar",
"city_sk": 19,
"country_name": "India",
"email_id": "dheerajgoojar31@gmail.com",
"phone_no": "",
"postal_code": "2342",
"service_provider_name": "skin glow ",
"service_provider_sk": 22759,
"state_sk": 13
},
{
"address_text": "lkjlksjd lkjlksdf ",
"city_sk": 19,
"country_name": "India",
"email_id": "dheeraj_goojar31@yahoo.com",
"phone_no": "",
"postal_code": "4660021",
"service_provider_name": "skinrise",
"service_provider_sk": 22769,
"state_sk": 13
}
]
}
Posted
Updated 20-Jun-13 16:44pm
v2

1 solution

Hi Avinash,

Looks like you are converting the datatable to a list.

So, here List<t_country> is a list of type t_country_cmn.

Now, t_country_cmnhas to be created.

Generally, List<object> is a dynamic array. With List, you do not need to manage the size on your own. Lists are considered as generics and constructed types. You need to use < and > in the List declaration. Lists handle any element type (starting from a string to a class).

So, depending upon the datatable columns, you need to create the t_country_cmnclass.

Then add the number of rows as list size.

C#
var lstGridData = new List<t_country>(dt.Rows.Count);


Then, you need to pass on the data in the "dt" into the list, like bellow code:

VB
lstGridData.AddRange(from DataRow row in dt.Rows
                                     select
                                         new t_country_cmn(row[column0], row[column1], row[column2], ....);


Depending upon the number of columns, the t_country class should be created here.

now, you have to return this list object, instead of the return rows.


Let me know if you want more info. Do please provide what are the columns in dt for better explanation.

Thank you,
RelicV
 
Share this answer
 
v3
Comments
Avinash8 17-Jun-13 0:22am    
thnks RelicV, I tried ur code

[WebMethod]
public List<t_country_cmn> getcountrybyid()
{
DataTable dt = _obj.CountryList();
var lstGridData = new List<t_country_cmn>(dt.Rows.Count);
lstGridData.AddRange(from DataRow in dt.Rows
select new t_country_cmn(row[country_name],
row[country_desc],
row[country_telecom_code],
row[date_format],
row[fnb_type_sk],
row[country_code],
row[is_sms_notification],
row[distance_measurement_unit]));
// List<t_country_cmn> rows = dt.Rows.Cast<t_country_cmn>().ToList();
return lstGridData;
}


I am getting following error, suggest me what is wrong in this:

Error 1 Could not find an implementation of the query pattern for source type 'System.Data.DataRowCollection'. 'Select' not found. Consider explicitly specifying the type of the range variable 'DataRow'. /Service1.svc.cs

Also I would like to ask u if there is a way to get data from database directly into the list type or any other type to return json?? At present I m getting data from database into data table and then... as u can see in the above code I have pasted. My priority is to make the web service as fast as possible.
RelicV 17-Jun-13 1:28am    
Avinash, you've created a generic List, but not a specific type (t_country_cmn) of List.

It should be
var lstGridData = new List<t_country_cmn> (dt.Rows.Count);
not
var lstGridData = new List(dt.Rows.Count);

And, return typr should also be
List<t_country_cmn>
not just any
List.

I dont see the class declaration of " t_country_cmn".

Let me know if you still find it difficlut.

Thnx
Avinash8 17-Jun-13 2:01am    
ok pls chek this, I m getting the same error:

service1.svc.cs code is:

public class Service1 : IService1
{
BussinessMobileWeb _obj = new BussinessMobileWeb();
[WebMethod]
public List<t_country_cmn> getcountrybyid()
{
DataTable dt = _obj.CountryList();
var lstGridData = new List<t_country_cmn>(dt.Rows.Count);
lstGridData.AddRange(from DataRow in dt.Rows
select new t_country_cmn(row[country_name],
row[country_desc],
row[country_telecom_code],
row[date_format],
row[fnb_type_sk],
row[country_code],
row[is_sms_notification],
row[distance_measurement_unit]));
// List rows = dt.Rows.Cast().ToList();
return lstGridData.ToList();
}
}


my iservice.cs code is:

namespace JSONWebService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetAllCountries")]
List<t_country_cmn> getcountrybyid();
}


my customer.cs file where I have declared t_country_cmn class

namespace JSONWebService
{
[DataContract]
public class t_country_cmn
{

//t_country_cmn(int country_sk, int currency_sk, string country_name, string country_desc, string country_telecom_code, string date_format, string fnb_type_sk, string country_code, string is_sms_notification, string distance_measurement_unit);

[DataMember]
public int country_sk { get; set; }

[DataMember]
public int currency_sk { get; set; }

[DataMember]
public string country_name { get; set; }

[DataMember]
public string country_desc { get; set; }

[DataMember]
public string country_telecom_code { get; set; }

[DataMember]
public string date_format { get; set; }

[DataMember]
public string fnb_type_sk { get; set; }

[DataMember]
public string country_code { get; set; }

[DataMember]
public string is_sms_notification { get; set; }

[DataMember]
public string distance_measurement_unit { get; set; }
}
}
RelicV 17-Jun-13 2:14am    
Could you please add the List type as t_country_cmn...? See my answer. Or give me ur gmail ID. I could help u.

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