Windows Forms: Insert Update Delete and View data in Redis using C#
By FoxLearn 5/26/2017 9:31:53 PM 7.71K
How to Insert Update Delete and View data in C# using Metro Framework, Modern UI, ServiceStack.Redis
Step 1: Click 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
Step 2: Right click on your project select Manage NuGet Packages -> Search metro framework, redis -> Install
Step 3: Design your metro form as below
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
Categories
Popular Posts
Material Admin Dashboard Template
11/15/2024
Responsive Admin Dashboard Template
11/11/2024
Freedash bootstrap lite
11/13/2024
Horizon MUI Admin Dashboard Template
11/18/2024
Modular Admin Template
11/14/2024