Thursday, May 31, 2012

Insert, Update and Detele in Entity Framework using C#.Net

Hi this post is regarding EntityFramework where we can can do simple queries like Select, Edit, Update and Delete using EF (EntityFramework). EF is an Object-Relational Mapper.

     First create a database, Don't worry i am uploading the database file also just attach the db file and use it. 
How to attach DB can be seen HERE

1. Open VS 2010 and create a new web project.
2. Ones the web project was created, the first thing we need to do is ADD a new item from installed templates      in vs 2010. Here the new item is ADO.Net Entity Model with an extension *.edmx.
     In my project EFDB  is my file name. ones it was added EFDBEENTITY is the class name to create
     an object, to work with it.
3. while adding a new item it will ask for a connection, provide appropriate things and add the tables you need.
4. Now build the project ones.
5. Design the GUI for adding deleting updating, editing and selecting, now i didnt had a chance to show the
    screen shots for u. Download the project from the provided link.
6. After designing the GUI we had 5 buttons and 7 text boxes and a gridview to show the details.
7. CODING

Bulk Insert Into SQL Table using BulkCopy from DataTable

In the previous post ADD, Delete and Update in datatable was posted. Now continuing the previous post for Bulk Insertion from DataTable you can write the following code in a button click to insert into SQL datatable as shown below.

This is a method as shown

private void Bulkinsert()
    {
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[
           "MyConnectionString"].ConnectionString))
        {
            cn.Open();
            using (SqlBulkCopy copy = new SqlBulkCopy(cn))
            {
                copy.ColumnMappings.Add(0,0);
                copy.ColumnMappings.Add(1, 1);
                copy.ColumnMappings.Add(2, 2);
                copy.DestinationTableName = "TableName";
                copy.WriteToServer(dt);
            }
        }
    }

Insert, Update and Detele in DataTable using C#.Net

Inserting, Deleting and Updating in DataTable is very important in the real senario, with out interation to database every time.
So first i am  creating a datatable in the pageload and this datatable is binded to gridview.
Before that declare static datatable globally as shown below

static DataTable dt;

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dt = new DataTable();
            dt.Columns.Add("S.NO");
            dt.Columns.Add("Name");
            dt.Columns.Add("Sal");           
            for (int i = 0; i < 10; i++)
            {
                dr = dt.NewRow();
                dr["S.No"] = (1 + i);
                dr["Name"] = "Sample " + (i+1);
                dr["Sal"] = "27000";
                dt.Rows.Add(dr);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

Friday, May 25, 2012

Interaction between two user controls in asp net c#.Net


  Create 2 tables in the DB 1. companies and  2. products

  Create 2 user controls
a)      one with dropdownlist name as UCDDL.ascx
b)      two with gridview name as UCGV.ascx
2.      In  ucddl.ascx drag and drop dropdownlist name it as ddlcountries
3.      Bind the companies table to dropdownlist as shown in example (page load event)
4.      Create an event globally as below
public event EventHandler CountryName;