Reputation: 71
I creat new WCF REST Services but my result with out key (not in JSON Array Format) my result like this
{"email": "[email protected]",
"name": "John",
"mobile": "009854"}
And I want to make an array, called Info, where each element contains these three values.
"Info":
[
{
"email": "[email protected]",
"name": "John",
"mobile": "009854"
},
{
"email": "[email protected]",
"name": "Smith",
"mobile": "009114"
}
]
my c# code
public OrderContract GetOrderDetails(string OrderID)
{
OrderContract order = new OrderContract();
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ssd"].ConnectionString);
con.Close();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Stu where Client_id='" + OrderID + "'", con);
DataSet ds = new DataSet();
da.Fill(ds, "contacts");
order.email= ds.Tables["contacts"].Rows[0]["email"].ToString();
order.name = ds.Tables["contacts"].Rows[0]["name"].ToString();
order.mobile= ds.Tables["contacts"].Rows[0]["mobile"].ToString();
}
catch (Exception ex)
{
throw new FaultException<string>
(ex.Message);
}
return order;
}
Any one can help me?
show me error in this line "Object reference not set to an instance of an object."
infoContract.Info.Add(order );
my code like this
public InfoContract GetOrderDetails(string OrderID)
{
OrderContract order = new OrderContract();
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ssd"].ConnectionString);
con.Close();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from Stu where Client_id='" + OrderID + "'", con);
DataSet ds = new DataSet();
da.Fill(ds, "contacts");
order.email= ds.Tables["contacts"].Rows[0]["email"].ToString();
order.name = ds.Tables["contacts"].Rows[0]["name"].ToString();
order.mobile= ds.Tables["contacts"].Rows[0]["mobile"].ToString();
var infoContract = new InfoContract();
infoContract.Info.Add(order );
return infoContract;
}
catch (Exception ex)
{
throw new FaultException<string>
(ex.Message);
}
return order;
}
and my Interface is
public interface IOrderService
{
[OperationContract]
[WebGet(UriTemplate = "/GetOrderDetails/{OrderID}",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
InfoContract GetOrderDetails(string OrderID);
}
my Class is
[DataContract]
public class OrderContract
{
[DataMember]
public string email{ get; set; }
[DataMember]
public string name{ get; set; }
[DataMember]
public string mobile{ get; set; }
}
[DataContract]
public class InfoContract
{
[DataMember]
public List<OrderContract> Info { get; set; }
}
Upvotes: 1
Views: 238
Reputation: 695
You have to return an Array.
Your method returns only one object, you can try with something similar to what is given below.
public InfoContract GetOrderDetails(string OrderID){
...
var infoContract = new InfoContract();
infoContract.Info.Add(order );
return infoContract;
}
OrderContract.cs
public class OrderContract {
public string email {get;set;}
public string name {get;set;}
public string mobile{get;set;}
}
public class InfoContract {
public List<OrderContract> Info {get;set;}
}
Upvotes: 6