..


Sponsored Links

Controls for managing data

After seeing how they access data through ADO.NET now see how to access them in an even faster and easier. In fact, ASP.NET includes several classes that limit the complexity of managing connections and data transfer, ie the so-called DataSource controls.

These controls make it transparent to the programmer all the mechanism of connection and command generator, and all you need to do is determine the data source (data source), controls that focus on that data source and provide an appropriate query. Visual Studio provides a wizard that allows you to easily manage these steps. Once you create a DataSource control, you can connect to any component for the management of data.

As usual we see an example. We add to our project a new web form from the Toolbox and drag a SqlDataSource control on it. Click on the Configure Data Source in its context menu

It opens a window where you can select an existing database or create a new one using the New Connection button. In my case I select the database type test called mydb SQL Server CE application in my local computer but clearly you can select any database

We continue and specify one of the following steps to collect data from a table residing on the database (in my case the table Friends) and select the columns (in my case Id, Name and Age)

On the next screen you can also test the query by clicking on Test Query, and then click on Finish. Now let's set the property value DataSourceMode DataReader

Insert a ListBox in our form and check the Enable AutoPostBack

Then click on the Choose Data Source dialog box and select the related control that we created earlier by specifying which field to be displayed in the list and instead must indicate which field the corresponding value

When you start the application displays a list of related data in the table that we set as a data source. The connection between the listbox and the data source, which we did through the window just seen, we could do it by writing the following code in the form's Load






 protected void Page_Load (object sender, EventArgs e)







 {

 

  



 if (! this.IsPostBack)



  



 {



    



 this.ListBox1.DataSource = this.SqlDataSource1;

 

    



 this.ListBox1.DataTextField = "Name";

 

    



 this.ListBox1.DataValueField = "Id";

 

    



 this.ListBox1.DataBind ();



  



 }







 }



This simple example serves only to give an idea of ​​the potential of the tools provided by ASP.NET for data management. In this case we used a simple control such as listbox, but there are many more complicated controls for managing data, enabling you to view themselves in different ways. These controls include the GridView, FormView, DetailsView, DataList. Clearly aim of this guide is not delve into the details of these controls and, as always, I refer you to the Microsoft official documentation to learn how they work.

Help with Visual Studio ASP.Net
E-Learning
ASP (Advanced) ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €.
ASP.NET (Course) ASP.NET (Course)
Full course for building Web applications from 49 €.
SQL and Database (Course) SQL and Database (Course)
Create and manage relational databases. From 39 €.
Sponsored Links