Dataset bind to Gridview within WCF REST retrieval method and Linq to Sql

2.6k views Asked by At

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.

1

There are 1 answers

1
eriksv88 On

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>();  
}