Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to add new, edit, update, and delete record in Datag
Message
De
16/04/2003 12:41:53
 
 
À
16/04/2003 04:04:00
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00778221
Message ID:
00778430
Vues:
16
This message has been marked as the solution to the initial question of the thread.
I'll give you very simple example, but the idea behing it is the same for more complex scenarios:
1. Start new project and from Server Explorer navigate to some SQL Server or MSDE instance. Expand sample Northwind database, which comes with SQL Server (I think also with MSDE). Drag some table (for example, Customers) on your form. This will create SqlConnection1 and SqlDataAdapter1.

2. Right-click SqlDataAdapter1 and select "Generate Dataset..." option. Change dataset name for example to dstCustomers. When you click OK, you will see also one instance, DstCustomers1, ready to use.

3. Put DataGrid on the form and bind it to the DstCustomers1 (from Properties window, set DataSource property of the DataGrid to the DstCustomers1.Customers)

4. You need to put only one row into Form load event:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SqlDataAdapter1.Fill(DstCustomers1.Customers)
    End Sub
5. This will fill your grid and allows you to edit existing records, add new records and delete existing records (to delete, click header of the row and press DEL key).

6. To save your changes to the database, add button to the form and put also one row in its Click event:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        SqlDataAdapter1.Update(DstCustomers1.Customers)
    End Sub
Lets see how this works:
1. Select SqlDataAdapter1 and in Properties window expand SelectCommand, InsertCommand, UpdateCommand and DeleteCommand properties. For each of them, see CommandText property (the "@" parameters in Insert, Update and Delete statements are in the Parameters collection of each command - see it also in Properties window).

2. Everything will become clear to you, if you know that Customers DataTable in the DstCustomers dataset maintains rowstate for every row you change (add, edit or delete). Update method, which I use to update the database, calls appropriate command for every changed row and pass necessary parameters from the fields on the updated row.

All I make through wizards you can (and some of the tasks - should be) make also through code.

Plamen Ivanov
MCSD .NET Early Achiever and MCAD .NET Charter Member (VB .NET/SQL Server 2000)
MCSD (VB 6.0/SQL Server 2000)

VB (.NET) - what other language do you need in the whole Universe?...

Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform