user609511
user609511

Reputation: 4261

Do CRUD on Gridview via WCF

How can I do Populate, Create, Insert, Delete in gridview via WCF ?

I have the problem that it cannot find the fieldname, but I can get the data.

On my Service code:

[DataContract]
    public class Pers_Horraire
    {
        [DataMember]
        string _ClientId;
        public string ClientId
        {
            get { return _ClientId; }
            set { _ClientId = value; }
        }

        [DataMember]
        string _Horraire;
        public string Horraire
        {
            get { return _Horraire; }
            set { _Horraire = value; }
        }       
    }

[OperationContract]
public IList<Pers_Horraire> Get_Horraire(string CodeCL)
    {
        try
        {
            using (var connectionWrapper = new Connexion())
            {
                var connectedConnection = connectionWrapper.GetConnected();
                //string sql_SelectAll = Outils.LoadFileToString(HttpContext.Current.Server.MapPath("~/SQL/Ordre_Selt.sql"));
                string sql_SelectAll = "SELECT CLIENT_ID,HORRAIRE FROM LS_CLIENTHORRAIRE WHERE CLIENT_ID = @CLIENT_ID";
                SqlCommand comm_SelectAll = new SqlCommand(sql_SelectAll, connectionWrapper.conn);
                comm_SelectAll.Parameters.AddWithValue("@CLIENT_ID", CodeCL);
                SqlDataReader readerOne = comm_SelectAll.ExecuteReader();

                List<Pers_Horraire> oListOrdr = new List<Pers_Horraire>();
                Pers_Horraire obj_Horr = new Pers_Horraire();
                while (readerOne.Read())
                {
                    obj_Horr.ClientId = readerOne["CLIENT_ID"].ToString();
                    obj_Horr.Horraire = readerOne["HORRAIRE"].ToString();
                    oListOrdr.Add(obj_Horr);
                }
                readerOne.Close();
                readerOne.Dispose();
                return oListOrdr;
            }
        }
        catch (Exception excThrown)
        {
            throw new Exception(excThrown.Message);
        }
    }

And I consume with grid:

<dx:ASPxGridView ID="ASPxGridView_Horraire" runat="server" 
                 KeyFieldName="CLIENT_ID;HORRAIRE"
                 OnRowInserting="Insrting_Horr_Obj" 
                 OnRowUpdating="Updting_Horr_Obj" 
                 OnRowDeleting="Delt_Horr_Obj" >
    <Columns>
        <dx:GridViewCommandColumn VisibleIndex="0" 
                                  ButtonType="Image">
            <DeleteButton Visible="True" 
                          Image-Url="../images/Icon/delete-icon.png">
                <Image Url="../images/Icon/delete-icon.png"></Image>
            </DeleteButton>
            <EditButton Visible="True"  
                        Image-Url="../images/Icon/page-edit-icon.png" >
                <Image Url="../images/Icon/page-edit-icon.png"></Image>
            </EditButton>
            <NewButton Visible="True" 
                       Image-Url="../images/Icon/Plus-icon.png">
                <Image Url="../images/Icon/Plus-icon.png"></Image>
            </NewButton>
            <CancelButton Image-Url="../images/Icon/Banned-icon.png">
                <Image Url="../images/Icon/Banned-icon.png"></Image>
            </CancelButton>
            <UpdateButton Image-Url="../images/Icon/Good-or-Tick-icon.png">
                <Image Url="../images/Icon/Good-or-Tick-icon.png"></Image>
            </UpdateButton>
            <ClearFilterButton Visible="True" 
                               Image-Url="../images/Icon/System-Recyclebin-Empty-icon.png" 
                               Image-ToolTip="Effacer Filtre" > 
                <Image ToolTip="Effacer 
                       Filtre" Url="../images/Icon/System-Recyclebin-Empty-icon.png"> </Image>
            </ClearFilterButton>
        </dx:GridViewCommandColumn>
        <dx:GridViewDataTextColumn FieldName="HORRAIRE" 
                                   VisibleIndex="1"  
                                   Caption="Horraire" Width="10px">
        </dx:GridViewDataTextColumn>
    </Columns>
</dx:ASPxGridView>

Code behind:

public void GetDataHorr()
{
    try
    {
        Service1Client client = new Service1Client();

        ASPxGridView_Horraire.DataSource = client.Get_Horraire(Id);  
        ASPxGridView_Horraire.DataBind();

        //just to show i get the data from WCF
        foreach (var oItem in client.Get_Horraire(Id))
        {
            ASPxLabel_err.Text += oItem + "_______";
        }
    }
    catch (Exception excThrown)
    {
        ASPxLabel_err.Text = excThrown.Message + "-->" + excThrown.InnerException;
    }
}

The problem it cannot find FieldName="HORRAIRE" so if I let FieldName="" it shows me empty column.

Upvotes: 1

Views: 678

Answers (1)

Neil Thompson
Neil Thompson

Reputation: 6425

I think you are just returning a List of strings - so there is no concept of FieldNames in the client (the asp page).

eg:

oListOrdr.Add(readerOne["HORRAIRE"].ToString());

the named filed "Horraire" becomes just a string.

Could you amend the WCF Method to return a custom type perhaps?

public IList<Horraire> Get_Horraire(string CodeCL)

where you have

public class Horraire
{
   public int Client_Id {get;set;}
   public string Horraire {get;set;}
}

Upvotes: 1

Related Questions