h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

sealed -- C# Keyword

sealed  -- C# Keyword

  •  When applied to a class, the sealed modifier prevents other classes from inheriting from it.
  •   In the following example, class B inherits from class A, but no class can inherit from class B.


class A {}  
sealed class B : A {}

readonly -- C# Keyword

readonly -- C# Keyword
  •  The readonly keyword is a modifier that you can use on fields.
  •   When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.
readonly <data Type> <var Name>;

override -- C# Keyword

override -- C# Keyword


  •  The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.
  • An override declaration cannot change the accessibility of the virtual method.
  • Both the override method and the virtual method must have the same access level modifier.
  • You cannot use the new, static, or virtual modifiers to modify an override method.

out -- C# Keyword

out -- C# Keyword

  • This keyword implemented from VS 2010
  •  The out keyword specifies that the type parameter is covariant.
  • You can use the out keyword in generic interfaces and delegates.

interface <interface Name><out R> { }

extern -- C# Keyword

extern -- C# Keyword

  •  The extern modifier is used to declare a method that is implemented externally.
  • A common use of the extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code.
  • In this case, the method must also be declared as static, 

[DllImport("<dll Name>.dll")]
private static extern void <dll Method Name>();

const -- C# Keyword

const  -- C# Keyword
  • You use the const keyword to declare a constant field or a constant local.
  •  Constant fields and locals aren't variables and may not be modified.
  •  Constants can be numbers, Boolean values, strings, or a null reference.
const <Data Type> <Var Name> = <Set Value>;

Protected -- C# Keyword

Protected  -- C# Keyword:


  • The protected keyword is a member access modifier.
  • A protected member is accessible within its class and by derived class instances.

 protected <Data Type> <Variable Name>;

async -- C# Keyword

async  -- C# Keyword


  • This Keyword is implemented from VS 2012.
  •         Use the async modifier to specify that a method, lambda expression, or anonymous method is asynchronous.
  •         If you use this modifier on a method or expression, it's referred to as an async method.

public async Task<int> ExampleMethodAsync()
{
    // . . . .

}

in -- C# Keyword

in -- C# Keyword


  •    This Keyword is implemented from VS 2010.
  •    The in keyword specifies that the type parameter is contravariant.
  •    You can use the in keyword in generic interfaces and delegates.

// Contravariant interface.

interface <Interface Name><in A> { }

Insert ,Update ,Delete and Display in Datagridview using LINQ To SQL in C#.Net

Insert ,Update ,Delete and Display in Datagridview using LINQ To SQL in C#.Net

Create tables in SQL server
tbl_department_master
depId  int -- Primary Key -- Set Identify is true
DepName -- varchar(Max)

tbl_Position_Master
PatId int -- Primary key
Patname-- varchar(Max)

Add LINQ To SQL in the your project
Go To Server Explore
Add Data Connection
Drag Tables into LIQToSQL Class
Create user Control

Add User Control in your Project.
Add Datagridview and Two buttons on the User Control
Declare Variables ,Data Context  and Events
 DataClasses1DataContext objDC = null;
 tbl_Department_Master objDM = null;
 tbl_Position_Master objPM = null;
public event EventHandler SaveClicked;
public event EventHandler CloseClicked;
/// To Display Data
 public void Displaydata(string mode)
        {
            switch (mode)
            {
                case "Department":
                    DisplayData("Department");
                    break;
                case "Position":
                    DisplayData("Position");
                    break;
            }
        }
 private void DisplayData(string mode)
        {
            objDC = new DataClasses1DataContext();
            switch (mode)
            {
                case "Department":
                    var query = from q in objDC.tbl_Department_Masters
                                select q;
                    dataGridView1.DataSource = query;
                    break;
                case "Position":
                    var Posquery = from Pq in objDC.tbl_Position_Masters
                                   select Pq;
                    dataGridView1.DataSource = Posquery;
                    break;
            }        
        }
/// Delete Data
 private void DeleteData(string mode)
        {
            objDC = new DataClasses1DataContext();
            switch (mode)
            {
                case "Department":
                    objDM = new tbl_Department_Master();
                    if (objDM != null)
                    {
                        var Delet = from d in objDC.tbl_Department_Masters
                                    select d;
                        foreach (var details in Delet)
                        {
                            objDC.tbl_Department_Masters.DeleteOnSubmit(details);
                        }
                        objDC.SubmitChanges();
                    }
                    break;
                case "Position":
                    objPM = new tbl_Position_Master();
                    if (objPM != null)
                    {
                        var Delet = from d in objDC.tbl_Position_Masters
                                    select d;
                        foreach (var details in Delet)
                        {
                            objDC.tbl_Position_Masters .DeleteOnSubmit(details);
                        }
                        objDC.SubmitChanges();
                    }
                    break;
            }
        }
/// Save Data
public void SaveData(string mode)
        {
            switch (mode)
            {
                case "Department":
                    DeleteData("Department");
                    objDC = new DataClasses1DataContext();
                    for (int index = 0; index < dataGridView1.Rows.Count - 1; index++)
                    {
                        objDM = new tbl_Department_Master();
                        objDM.depId = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value);
                        objDM.DepName = Convert.ToString(dataGridView1.Rows[index].Cells[1].Value);
                        objDC.tbl_Department_Masters.InsertOnSubmit(objDM);
                        objDC.SubmitChanges();
                    }
                    DisplayData("Department");
                    break;
                case "Position":

                    DeleteData("Position");
                    objDC = new DataClasses1DataContext();
                    for (int index = 0; index < dataGridView1.Rows.Count - 1; index++)
                    {
                        DataGridViewRow objRowIndex = new DataGridViewRow();
                        objPM = new tbl_Position_Master();
                        objPM.PatId = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value);
                        objPM.Patname = Convert.ToString(dataGridView1.Rows[index].Cells[1].Value);
                        objDC.tbl_Position_Masters.InsertOnSubmit(objPM);
                        objDC.SubmitChanges();
                    }
                    DisplayData("Position");
                    break;
            }
        }

Call this User Control in your form:

Add the code in your code Page

private void DisplayData(string mode)
        {
          switch (mode)
            {
                case "Department":
                    ucDepartment1.Displaydata("Department");
                    break;
                case "Position":
                    ucDepartment1.Displaydata("Position");
                    break;
            }

        }

        private void FrmDepartment_Load(object sender, EventArgs e)
        {
            DisplayData("Position");
        }

        private void ucDepartment1_Load(object sender, EventArgs e)
        {

        }

        private void ucDepartment1_CloseClicked(object sender, EventArgs e)
        {
            ((Form)this.TopLevelControl).Close();
        }

        private void ucDepartment1_SaveClicked(object sender, EventArgs e)
        {
            ucDepartment1.SaveData("Position");

        }