2 ADOAndASP

download 2 ADOAndASP

of 20

Transcript of 2 ADOAndASP

  • 8/14/2019 2 ADOAndASP

    1/20

    ADO.NET in ASP.NET As we have seen before, we can program using

    ADO.NET in two ways By explicitly writing code to create

    Connection, DataReader,DataAdpater etc. and then binding themwith controls traditional way

    By dragging and dropping controls and

    configuring them visually. DataBinding New feature in ASP 2.0. We will use the 2nd approach in this session.

  • 8/14/2019 2 ADOAndASP

    2/20

    DataBinding

    ASP.NET includes a rich and full-featured modelfor data binding .

    Data binding is a technique of binding asp.netweb controls to the Data Source controls so thatdata is automatically fetched and shown.

    Data Source controls are the controls that linksitself with data sources like SQLserver, Oracle,Access etc.

    Data binding is declarative, not programmatic.

  • 8/14/2019 2 ADOAndASP

    3/20

    Data Source Controls

    Most of standard web controls (like TextBox, Label, LinkButton

    etc., )support single-value data binding. Controls like ListBox, DropDownList support multi-value. As with windows form application, for web application also

    there is a GridView which is specifically data-bound control. But apart from GridView, the diagram shows other controls

    also which are available for the web form.

    DataSource controls

    Special data controls

  • 8/14/2019 2 ADOAndASP

    4/20

    Creating SQLServer Databasetables

    Let us create two tables in a SQLServer database.

    Enter some sample data.

  • 8/14/2019 2 ADOAndASP

    5/20

    Creating DataSource control Create a new asp.net web project. Drag and drop SQLDataSource Control in

    Default.aspx . Dragging a dropping the control leads to entry of a new

    line in the source

  • 8/14/2019 2 ADOAndASP

    6/20

    Configuring the DataSource1. Right click and click on Configure Data Source.2. Select the database that you configured.

    Ensure that this isticked. This makes anentry in web.config file .

  • 8/14/2019 2 ADOAndASP

    7/20

    1. Select table and the columns.2. Click on Advanced and tick this

    1. Test Query and click on Finish

  • 8/14/2019 2 ADOAndASP

    8/20

  • 8/14/2019 2 ADOAndASP

    9/20

    Viewing the properties of DataSource control

    Similarly look at the InsertQuery and Update Querytoo. This happened because of step 5

  • 8/14/2019 2 ADOAndASP

    10/20

    Viewing data source code in aspx

  • 8/14/2019 2 ADOAndASP

    11/20

    SelectCommand="SELECT [EID], [Name] FROM [Employee]"UpdateCommand="UPDATE [Employee] SET [Name] = @Name

    WHERE [EID] = @EID">

    Data types enforcedfor the parameters

  • 8/14/2019 2 ADOAndASP

    12/20

  • 8/14/2019 2 ADOAndASP

    13/20

    Build and Execute

    Edit and Upadate

    After delete

    Note that the GridView was not designedto insert new rows.

  • 8/14/2019 2 ADOAndASP

    14/20

    Specifying form parameters in theselect query

    Drag and drop a text box and a button on to the form. Select property of the SQLDataSource and go to select

    query.

    Click on Query builder.

  • 8/14/2019 2 ADOAndASP

    15/20

    Building Query Enter the where clause and

    click ok. Click on refresh

    parameters. Select Control for

    Parameter Source andselect the name of textboxcontrol you dropped for controlID.

    Click OK

  • 8/14/2019 2 ADOAndASP

    16/20

    Execute

  • 8/14/2019 2 ADOAndASP

    17/20

    Types of parameters

    Apart from Control, parameter for the query could beHTML Form parameters, Query String, Session data,Cookie or a key from user profile.

    Code that just got added

  • 8/14/2019 2 ADOAndASP

    18/20

    Form Control Create another Der form. Drag and drop the SQLDataSource and configure in the

    same way.

    Drag and drop FormView and link it to the data source. Apply template , enable paging. Test

    Try inserting,updating anddeleting

  • 8/14/2019 2 ADOAndASP

    19/20

  • 8/14/2019 2 ADOAndASP

    20/20

    protected void UpdatEEMP(object sender,

    SqlDataSourceCommandEventArgs e){e.Command.Parameters["@Name"].Value =e.Command.Parameters["@FirstName"].Value;

    e.Command.Parameters["@Last"].Value =e.Command.Parameters["@LastName"].Value;e.Command.Parameters.Remove(e.Command.Parame

    ters["@FirstName"]);

    e.Command.Parameters.Remove(e.Command.Parameters["@LastName"]);}