Windows Forms: Insert Update Delete and View data in Redis using C#

How to Insert Update Delete and View data in C# using Metro Framework, Modern UI, ServiceStack.Redis

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "RedisCRUD" and then click OK

crud in redisStep 2: Right click on your project select Manage NuGet Packages -> Search metro framework, redis -> Install

download and install metro frameworkStep 3: Design your metro form as below

insert update delete with redis cache using c#

Step 4: You need to create a Phone class to map data

public class Phone
{
    public string ID { get; set; }
    public string Model { get; set; }
    public string Manufacturer { get; set; }
}

Add code to handle your form

using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace RedisCRUD
{
    public partial class Form1 : MetroFramework.Forms.MetroForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        void Edit(bool value)
        {
            txtID.ReadOnly = value;
            txtManufaturer.ReadOnly = value;
            txtModel.ReadOnly = value;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            using (RedisClient client = new RedisClient("localhost", 6379))
            {
                IRedisTypedClient<Phone> phone = client.As<Phone>();
                phoneBindingSource.DataSource = phone.GetAll();
                Edit(true);//Read only
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            phoneBindingSource.Add(new Phone());
            phoneBindingSource.MoveLast();
            Edit(false);//Allow edit
            txtID.Focus();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit(false);//Allow edit
            txtID.Focus();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Edit(true);//Read only
            phoneBindingSource.ResetBindings(false);
            ClearText();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MetroFramework.MetroMessageBox.Show(this, "Are you sure want to delete this record ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Phone p = phoneBindingSource.Current as Phone;//Get current object
                if (p != null)
                {
                    //Connect to your redis cache
                    using (RedisClient client = new RedisClient("localhost", 6379))
                    {
                        IRedisTypedClient<Phone> phone = client.As<Phone>();
                        phone.DeleteById(p.ID);//Delete data by id
                        phoneBindingSource.RemoveCurrent();
                    }
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            using (RedisClient client = new RedisClient("localhost", 6379))
            {
                phoneBindingSource.EndEdit();
                IRedisTypedClient<Phone> phone = client.As<Phone>();
                //Get data from bindingsource, then save to redis cache
                phone.StoreAll(phoneBindingSource.DataSource as List<Phone>);
                MetroFramework.MetroMessageBox.Show(this, "Your data has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Edit(true);//Readonly
            }
        }
    }
}

If you don't have install redis, you can view How to download and install redis cache

VIDEO TUTORIALS