Code Heaven For C# Developer

Friday, July 23, 2010

Insert,Update,Delete with LINQ.....

Here i am going to do simple INSERT,DELETE and UPDATE with LINQ....




Add DataClasses1.dbml in ur solution.
and then write the following code ...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;


namespace LinqOperation
{
public partial class Form1 : Form
{
DataClasses1DataContext dc = null;
public Form1()
{
InitializeComponent();
dc = new DataClasses1DataContext();
}

private void Form1_Load(object sender, EventArgs e)
{

RefreshGrid();


}
public void RefreshGrid()
{
Table XPC = dc.GetTable();

var q = from x in XPC
select x;

var data = dc.GetTable();
dataGridView1.DataSource = q;


}

private void Add_Click(object sender, EventArgs e)
{

if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text) ||string.IsNullOrEmpty(textBox3.Text))
else
{
Table XPC = dc.GetTable();
Customer cs = new Customer { id = int.Parse(textBox1.Text), Names = textBox2.Text, Grade = textBox3.Text };

XPC.InsertOnSubmit(cs);
XPC.Context.SubmitChanges();
}
RefreshGrid();
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;

}

private void Update_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text))
MessageBox.Show("Plz enter all the entries");
else
{
Table XPC = dc.GetTable();
Customer w = XPC.Where(a => a.id == int.Parse(textBox1.Text)).FirstOrDefault();
if (w == null)
MessageBox.Show("Record not found");
else
{
w.Grade = textBox3.Text;
w.Names = textBox2.Text;
XPC.Context.SubmitChanges();
}
}
RefreshGrid();
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;
}

private void Delete_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
Table XPC = dc.GetTable();
XPC.DeleteOnSubmit(XPC.Where(a => a.id == int.Parse(textBox1.Text)).FirstOrDefault());
XPC.Context.SubmitChanges();
RefreshGrid();
}
else
{
MessageBox.Show("Enter the Id");
}
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;
}
}
}

Working with events in C#

First create a EventClass class.Define ur delegate and event here.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fireevent
{
public class EventClass
{

here is ur delegate
public delegate void MYDelegate();

here is ur event, named same as ur delegate
public event MYDelegate Fire;


method for checking that event is null or not and then call the event
public void show()
{

if (Fire != null)
Fire();

}

}
}



After this create another class in which u want to fire ur event.Here iam taking Form1 class and i am going to fire my event on page load.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fireevent
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

create object of ur class EventClass and then call ur fire event and function show();
EventClass ec = new EventClass();
ec.Fire += new EventClass.MYDelegate(ec_Fire);
ec.show();
After this controller will go to void ec_Fire()
}

void ec_Fire()
{


}

}
}



Enjoy N FIRE EVENT...................

Labels: