ADO.NET

ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples

Examples

adplus-dvertising
Simple Data Binding in Windows Forms
Previous Home Next

Create a New Project in VS .NET

  1. Create a new project in VS .NET by choosing File, New, and then choosing the Project option.
  2. When the New Project dialog box appears, choose Visual C# Projects and Windows Applications. Name this project "simple data binding." This creates a default form for you to start from.

Add the Data Connection, Data Adapter, and DataSet

From the Data tab of the Toolbox, drag a OledbDataAdapter object into your form

  1. This will automatically invoke the Data Adapter Configuration Wizard. Both the data connection and the data adapter can be fully configured here.
    • a. The wizard creates with the Choose Your Data Connection dialog box. If already exist a connection defined in your project, it will be placed in the dialog box. Otherwise, choose to create a new connection and specify the appropriate connection information

    • b. we will then have to decide to supply SQL statements, build a new stored procedure, or give the name of an existing stored procedure for the data access. In our example we will use the Use SQL Statements option.

    • c. we will be presented with a Generate the SQL Statements dialog box where we will simply type in a valid SQL statement, or you can use the Query Builder option to formulate the SQL query. such as

      SELECT * FROM emp
      

    • d. The wizard will show we the tasks that it has done and indicate whether the SqlDataAdapter has been configured successfully.

  2. After the SqlDataAdapter and DataConnection objects have been configured and added to the form, you must generate a DataSet and then add an instance of this DataSet to the form. We will be binding our TextBox properties to the columns in the DataSet.
    • a. Simply right-click on the oledbDataAdapter that is on your form and choose the Generate Dataset menu option

    Generate a new dataset for the form

    • a. Now,we choose to create a new DataSet using the default name that it provides (DataSet1). Make sure you have checked the emp table and checked the box for it to be added to the designer.

    • b. When the process finishes, a DataSet instance named DataSet11 will be on the form and a dataset schema will be in the Solutions Explorer (named DataSet1.xsd).

    Create Text Boxes, Labels, and Buttons

    • Textbox: With a name of EmpID and text is blank.
    • Textbox: With a name of EmpName and text is blank.
    • Button: With a name of ok and text of OK

Bind the Text Boxes to the DataSet

  1. From the Forms Designer, select the EmpID text box and go to its property
  2. Expand the (DataBindings) node in the properties list and its text property.
  3. Within the text property, expand DataSet1 and Emp nodes and select the empid column from the list
  4. Now, from the Forms Designer, select the EmpName text box and press F4.
  5. Expand the (DataBindings) node in the properties list and its text property.
  6. Within the text property, expand the DataSet1 and Emp nodes and select the empname column from the list.

Now we are ready to complete the application by adding the code to fill the DataSet.Just double-click on the Ok button to create a method for the Click event.

private void button1_Click(object sender, EventArgs e)
{
oleDbDataAdapter1.SelectCommand.Parameters
["param1"].Value =EmpID.Text.ToString();
empDataSet1.Clear();
oleDbDataAdapter1.Fill(empDataSet1);
}
}

The Forms application, showing simple binding.

Previous Home Next