Your Ads Here

Friday, 23 March 2012

How to connect Microsoft SQL Server Compact or .sdf format database in C#.NET


It shows how to connect Microsoft SQL Server Compact or .sdf format database file into our project using C#.NET
Keywords: database, sql, C#.net, connection.

Steps to add .sdf file database into our project.

Step 1: download Microsoft SQL Server Compact setup from here

Step 2: After installation, you can see two setup file (SSCERuntime_x64-ENU and SSCERuntime_x86-ENU), install SSCERuntime_x86-ENU if you are using WINXP.

Step 3: After installation, open your project and create a new database connection, through selecting “Connect to Database


Step 4: Then change data source into “Microsoft SQL Server Compact 3.5” and press OK



Step 5: Now click browse in connection properties to choose the .sdf file

Step 6: Then test your connection through clicking ‘Test Connection’ and click OK to exit


 
Step 7: Imp: you have to add a reference to your project named: “System.Data.SqlServerCe.dll”(located in “C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Desktop”)


 
Step 8: To check this add a datagridview into your project and type the below code into your project.

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

namespace testc
{
    public partial class Form1 : Form
    {
        SqlCeConnection sqlceCon = new SqlCeConnection();
        SqlCeCommand sqlCeCom = new SqlCeCommand();
        DataTable dt = new DataTable();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            sqlceCon = new SqlCeConnection("Data Source=C:\\Program Files\\Microsoft SQL Server Compact Edition\\v3.5\\Samples\\Northwind.sdf");
            sqlceCon.Open();

            SqlCeDataAdapter adptrOdbc = new SqlCeDataAdapter("select * from customers", sqlceCon);
            adptrOdbc.Fill(dt);

            dataGridView1.DataSource = dt;
        }      
    }
}

Note: you can create new database at Connection adding time, through selecting Create option in Connection Properties


Thursday, 22 March 2012

How to create MDI Parent form in C#.NET


It shows how to create MDI parent and child form in C#.NET
Keywords: C#.NET, C Sharp.NET, MDI, ismdicontainer.

The foundation of a Multiple-Document Interface (MDI) application is the MDI parent form. This is the form that contains the MDI child windows, which are the sub-windows wherein the user interacts with the MDI application. Creating an MDI parent form is easy, both in the Windows Forms Designer and programmatically.

Steps to create MDI Parent.

Step 1: Click on the main form and takes its isMdiContainer propert in Property box, default value is false, change to true that will set your selected form to mdi parent. (your form background become grey color)



Steps to create MDI child.

For this add a button to your form and write below code into the button click event to open the child form.

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 testc
{
    public partial class Form1 : Form
    {
        Form2 frm2 = new Form2();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frm2.MdiParent = this;
            frm2.Show();
        }
    }
}


Here your new form will open inside the main form.


Wednesday, 21 March 2012

Anchor and dock controls or property in C#.NET


It shows how to dock and anchor controls or property in C#.NET
Keywords: C#.NET, C SHARP.NET, align, anchor, docking.

Anchoring and Docking

The Anchor and Dock properties of a form are two separate properties. Anchor refers to the position a control has relative to the edges of the form. A button, for example, that is anchored to the left edge of a form will stay in the same position as the form is resized. Docking refers to how much space you want the control to take up on the form. If you dock a control to the left of the form, it will stretch itself to the height of the form, but its width will stay the same.

Anchoring

Step 1: Add two buttons to the form, click on the button and take its anchor property in property box, The default is to anchor the control to the Top, Left edge of the form.



Step 2: The button in the middle represents your control. The big white areas are rather confusing - they don't actually do anything! To change the property, you click the smaller grey or white rectangles between the big white rectangle. Click again to deselect it. In the image below, the property has been changed so that the button is anchored to the Top, Left and Right sides of the form:



Step 3: Thus you can change your controls position as you like.

Docking

Docking is similar to Anchoring, but this time the control fills a certain area of the form. To see how it works, click on one of your button and locate the Dock property. 



This time, all the rectangles are like buttons. You can only dock to one side at a time, and the default is None. Click a button to see what it does to your button. Click the middle one, and the button will Fill the whole form.



Docking is quite useful when used with the splitter control and panels, allowing you to create a Windows-style interface.