0

I used a WCF REST template to build a WCF service library to create PUT and GET calls. PUT method works fine sending my blob to a database.
On the GET, I want to be able to access the web service directly and display the results from a stored procedure as a dataset and bind this to a gridview. The stored procedure is a simple select statement, returning three of the four columns from the table. I have the following:

[WebGet(UriTemplate = "/?name={name}", ResponseFormat = WebMessageFormat.Xml)]  
public List<Object> GetCollection(string name)  
{  
        try  
        {      
                 db.OpenDbConnection();  
             // Call to SQL stored procedure  
                return db.GetCustFromName(name);  
        }  
        catch (Exception e)  
        {  
            Log.Error("Stored Proc execution failed. ", e);  
        }  
        finally  
        {  
            db.CloseDbConnection();  
        }  
        return null;  
}

I also added Linq to SQL class to include my database table and stored procedures access. I also created the Default.aspx file in addition to the other required files.

 protected void Page_Load(object sender, EventArgs e)  
 {  
        ServiceDataContext objectContext = new ServiceDataContext();            
            var source = objectContext.GetCustFromName("Tiger");  
            Menu1.DataSource = source;  
            Menu1.DataBind();  
 }  

But this gives me The entity type '' does not belong to any registered model.

Where should the data binding be done? What should be the return type for GetCollection()? I am stuck with this. Please provide help on how to do this.

Frank Hale
  • 1,886
  • 1
  • 17
  • 31
Jai
  • 319
  • 2
  • 9
  • 30

1 Answers1

0

First of all, why do you have List of Object and not custom class/type or datatable? And then, and return 0, I would rather set to return new List<Object> sp will return back dataset or datatable.

First create a class for custommer:

 class Custommer 
    {
       private int gID;
       private string gName;

       public int ID 
       {
          get 
          {
             return gID;
          }
          set
          {
             gID=value;
          }
       } 

       public string Name 
       {
          get 
          {
             return gName;
          }
          set
          {
             gName=value;
          }
       } 
       ...
    }

Then something like this:

[WebGet(UriTemplate = "/?name={name}", ResponseFormat = WebMessageFormat.Xml)]  
public List<Custommer> GetCollection(string name)  
{  
        try  
        {      
             db.OpenDbConnection();  
             // Call to SQL stored procedure        
                DataTable vDT = db.GetCustFromName(name);

                //Create a new list of custommer
                List<Custommer> vListOfCustommer = new List<Custommer>();

                //loop all row from the sp from database.
                foreach (DataRow r in vDT) {
                    Custommer vCustommer = new Custommer();
                    vCustommer.ID =r["ID"];
                    vCustommer.Name= r["Name"];

                    vListOfCustommer.add(vCustommer);
                }
                // return list of custommer
                return vListOfCustommer;
        }  
        catch (Exception e)  
        {  
            Log.Error("Stored Proc execution failed. ", e);  
        }  
        finally  
        {  
            db.CloseDbConnection();  
        }  
        return new List<Custommer>();  
}
eriksv88
  • 3,482
  • 3
  • 31
  • 50
  • Thanks for the example, appreciate it. I now can see the XML being returned when I do a GET on the WCF webservice. But I am still confused about how to bind this XML data to a gridview. What is te best approach?..should I create another ASP.NET website project as a client to consume this webservice (so I can get hold of default.aspx) or is there a way to do this in the same project as the WCF web service project? Please help.. – Jai Mar 04 '11 at 15:23