I have created one wcf webservice and i can insert data through wevice but still i facing one issue on get the data from the webservice.
my get data web service returning a data table , after consuming it and showing to data grid view.but i could'nt get it properly below code showing the method and error messge.if any one have idea please .
Here is my Service1.svc.cs Code:
public DataTable GetUserDetails(string Username)
{
DataTable dtresult = new DataTable();
using (SqlConnection con = new SqlConnection(strConnection))
{
SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName Like
'%'+@Name+'%'", con);
cmd.Parameters.AddWithValue("@Name", Username);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtresult);
}
return dtresult;
}
I think so the method _objectServiceClient.GetUserDetails("1").Clone() does not return datatable.
Please check whtat it returns and then you can create data table from the return type by converting it manually or create table and then add the data from that method.
Please post more details about what _objectServiceClient.GetUserDetails("1").Clone() return.
I think your service GetUserDetails("1") returns UserDetail type value, and you are trying to convert it in datatable, you can return Data Table type value from web service, so it would easy to bind it in gridview.
Don't forget to mark this post as "Answer", if it helped you.
public DataTable GetUserDetails(string Username)
{
DataTable dtresult = new DataTable();
using (SqlConnection con = new SqlConnection(strConnection))
{
SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName Like
'%'+@Name+'%'", con);
cmd.Parameters.AddWithValue("@Name", Username);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtresult);
}
return dtresult;
}
Thanks for the repply, unfortunalty this is not my issue now i getting the result set to my corresponding datat table geting error when i try to set the data source.,
Since the return type is "DataTable", so you should not required to cast it when the result returned. Can you check the setting when you generate services reference?
You should able to see "Advanced ..." button when you want to generate/update services reference. Click that button then make sure you have checked "Reuse types in referenced assemblies" and use "Reuse types in all
referenced assemblies" option. By this way, you can no need to cast the return result and can use it for binding directly.
No matter how much time you spend on coding. Wish you happy coding.
My Technical Blog
public DataTable GetUserDetails(string Username) { DataTable dtresult = new DataTable("someName"); using (SqlConnection con = new SqlConnection(strConnection)) { SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName Like '%'+@Name+'%'", con); cmd.Parameters.AddWithValue("@Name", Username); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dtresult); } return dtresult; }
sujilkumar
Member
115 Points
134 Posts
Type casting Issue while consume the webservice.
Jan 29, 2013 08:08 AM|LINK
Hi all,
I have created one wcf webservice and i can insert data through wevice but still i facing one issue on get the data from the webservice.

my get data web service returning a data table , after consuming it and showing to data grid view.but i could'nt get it properly below code showing the method and error messge.if any one have idea please .
Here is my Service1.svc.cs Code:
public DataTable GetUserDetails(string Username)
{
DataTable dtresult = new DataTable();
using (SqlConnection con = new SqlConnection(strConnection))
{
SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName Like '%'+@Name+'%'", con);
cmd.Parameters.AddWithValue("@Name", Username);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtresult);
}
return dtresult;
}
And also here IService1.cs Code:
public interface IService1
{
[OperationContract]
DataTable GetUserDetails(string Username);
[OperationContract]
string InsertUserDetails(UserDetails userInfo);
}
Here insertMethod is working perfectly.
michaelalex7
Member
170 Points
35 Posts
Re: Type casting Issue while consume the webservice.
Jan 29, 2013 10:08 AM|LINK
What is the return type of the GetUserDetails method?
kushalrdalal
Contributor
7130 Points
1273 Posts
Re: Type casting Issue while consume the webservice.
Jan 29, 2013 01:00 PM|LINK
I think so the method _objectServiceClient.GetUserDetails("1").Clone() does not return datatable.
Please check whtat it returns and then you can create data table from the return type by converting it manually or create table and then add the data from that method.
Please post more details about what _objectServiceClient.GetUserDetails("1").Clone() return.
My Blog
LinkedIn Profile
CruzerB
Contributor
5399 Points
1098 Posts
Re: Type casting Issue while consume the webservice.
Jan 29, 2013 01:13 PM|LINK
Hi,
You can actually use these code to resolve the issue.
var data = _objServiceClient.GetUserDetails("1");
gvDetails.DataSource = data;
No explicitely type declaration required and it also can prevent too many of boxing and unboxing.
My Technical Blog
vdtrip
Member
70 Points
77 Posts
Re: Type casting Issue while consume the webservice.
Jan 29, 2013 07:31 PM|LINK
I think your service GetUserDetails("1") returns UserDetail type value, and you are trying to convert it in datatable, you can return Data Table type value from web service, so it would easy to bind it in gridview.
sujilkumar
Member
115 Points
134 Posts
Re: Type casting Issue while consume the webservice.
Feb 04, 2013 08:19 AM|LINK
Here is my Service1.svc.cs Code:
public DataTable GetUserDetails(string Username)
{
DataTable dtresult = new DataTable();
using (SqlConnection con = new SqlConnection(strConnection))
{
SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName Like '%'+@Name+'%'", con);
cmd.Parameters.AddWithValue("@Name", Username);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtresult);
}
return dtresult;
}
And also here IService1.cs Code:
public interface IService1
{
[OperationContract]
DataTable GetUserDetails(string Username);
[OperationContract]
string InsertUserDetails(UserDetails userInfo);
}
Here insertMethod is working perfectly.
CruzerB
Contributor
5399 Points
1098 Posts
Re: Type casting Issue while consume the webservice.
Feb 04, 2013 11:04 PM|LINK
http://evonet.com.au/using-parameters-addwithvalue-with-a-like-sql-query/
Try this.
My Technical Blog
sujilkumar
Member
115 Points
134 Posts
Re: Type casting Issue while consume the webservice.
Feb 05, 2013 04:25 AM|LINK
hi CruzerB,
Thanks for the repply, unfortunalty this is not my issue now i getting the result set to my corresponding datat table geting error when i try to set the data source.,
Thanks & Regards
SUJIL T
CruzerB
Contributor
5399 Points
1098 Posts
Re: Type casting Issue while consume the webservice.
Feb 09, 2013 03:27 AM|LINK
Hi,
Since the return type is "DataTable", so you should not required to cast it when the result returned. Can you check the setting when you generate services reference?
You should able to see "Advanced ..." button when you want to generate/update services reference. Click that button then make sure you have checked "Reuse types in referenced assemblies" and use "Reuse types in all referenced assemblies" option. By this way, you can no need to cast the return result and can use it for binding directly.
My Technical Blog
michaelalex7
Member
170 Points
35 Posts
Re: Type casting Issue while consume the webservice.
Feb 27, 2013 01:29 PM|LINK
Try setting the name of the datatable first before returning it in the service method:
http://stackoverflow.com/questions/12702/net-returning-datatables-in-wcf
e.g.
public DataTable GetUserDetails(string Username)
{
DataTable dtresult = new DataTable("someName");
using (SqlConnection con = new SqlConnection(strConnection))
{
SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName Like '%'+@Name+'%'", con);
cmd.Parameters.AddWithValue("@Name", Username);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtresult);
}
return dtresult;
}