Click here to Skip to main content
15,886,783 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

I have a WPF Project which is related to Customer Tracking System.
I need a method for updating my datagrid for Category table as follows:

In my User Interface, i have 3 textboxes respectively, CategoryID, CategoryName and Description and Save, Delete and Update button.

I am developing this Project via ADO.NET, so my methods need to be written considering ADO.NET.

I have a cs file named CategoryManager and i am developing my Save, Delete and Update methods in there. Please find my CategoryManager.cs file below. I need an update method in CategoryManager.cs for the update button in my user interface. I prefer, when i click the related row in datagrid, the values appear in the textboxes and ready to update.

I kindly request for your helps.

Kind Regards
Gokhan


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyProjectSatis
{
    class CategoryManager
    {
        public static void AddNewItem(Category category)
        {
            FaturaEntities ent = new FaturaEntities();
            ent.Categories.Add(category);
            ent.SaveChanges();
        }

        public static List<Category> GetAllCategories()
        {
            FaturaEntities ent = new FaturaEntities();
            return ent.Categories.ToList();
        }

        public static void DeleteSelectedItem(Category category)
        {
            FaturaEntities ent = new FaturaEntities();
            Category c = ent.Categories.FirstOrDefault(x => x.CategoryID == category.CategoryID);

            ent.Categories.Remove(c);
            ent.SaveChanges();
        }
    }
}
Posted
Comments
Krunal Rohit 16-Nov-15 3:02am    
So your question is: How to update the entity ?

-KR
gokhan572 16-Nov-15 3:33am    
Yes. Something like that. But need a method for it then i call it from the ui page if it is possible.
Thanks.

1 solution

First you need to Get row data from grid or from method and bind('show') this data in your textboxs, dont forget to save ID in variable or disabled textbox

C#
public static Category Get(int id )
       {
           FaturaEntities ent = new FaturaEntities();
           return ent.Categories.Where(x=>x.id==id).FirstOrDefault();
       }


then to update you need this method
C#
public static void Update(Category category)
        {
            using (var dbCtx = new FaturaEntities ())
           {

              dbCtx.Entry(category).State = System.Data.Entity.EntityState.Modified;
              dbCtx.SaveChanges();
           }
        }
 
Share this answer
 
Comments
gokhan572 16-Nov-15 6:57am    
Dear Mostafa,

Thank you for your quick answer.
I understand the method. Would it be possible to tell me how i can use this method on my user interface for an update button? I tried it out but could not succeed.
Please note that i have a filled datagrid in UI and when i click one of the row, values should appear in the textboxes ready to update.

Thanks in advance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900