Sunday, April 3, 2016

What is event in c#?



Events are the members of the class that rises when something happens a class, which have a message that contain the information about the event, Event handler is a method that contain the same signature as the event and this method is execute when the event occurs.

When we define an event then first define a delegate that contains the methods that will be called when the event raised , the example of the event is given below : 

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

namespace WindowsFormsApplication11
{
    public delegate void mydel();
   public  class Class1
    {
        public event mydel myevent;
        public void fire()
    {
        myevent();
    }
    }
}

.cs 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;

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

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            obj.myevent+=new mydel(obj_myevent);
            obj.fire();
        }
        void obj_myevent();
    {
        MessageBox.show("work done");
    }
    }

Output :
work done



Saturday, April 2, 2016

Accessibility of Modifier in C#


within assembly
                   
                  
                
    outside assembly
with inheritance
without inheritance
    with inheritance
without inheritance
public
Yes
Yes
Yes
Yes
private
No
No
No
No
protected
Yes
No
yes
No
internal
yes
Yes
No
No
protected internal
Yes
Yes
Yes
No
         



































Friday, April 1, 2016

Difference between Data Reader, Data Adapter, Dataset in C #



Data Reader 

Data Reader is used to read the data from Data Base in forward direction only Data Reader read the data in connected environment it will fetch the data very fast as comparison to Data set.

C# code 

Sqlconnection con=new Sqlconnection (“Data Source=.; integrated security=true; initial catalog=Master”)
{
con.open( );
Sqlcommand cmd=new Sqlcommand(“select * from emp”, con);
SqlDataReader dr= cmd.ExecuteReader ();
con.close();
}

Data Adapter

Data Adapter will work as a bridge between Data Reader and Data set, it is worked in disconnected architecture. Data Adapter is used to read the data from database and bind the data from data base.

Data set 

Data set work in the disconnected environment is the collection of Data tables and relations between tables .It is used to hold multiple tables with data. Data set also provides features like saving data as XML and loading XML data.

C# code 

Sqlconnection con=new Sqlconnection (“Data Source=.; integrated security=true; initial catalog=Master”)
{
con.open( );
Sqlcommand cmd=new Sqlcommand(“select * from emp”, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
 DataSet ds = new DataSet();
 da.Fill(ds);
}