Observable Collections
page 8 of 9
by frans eilering
Feedback
Average Rating: 
Views (Total / Last 10 Days): 40706/ 48

Update changed information from datagrid.

You may click in a cell in the datagrid and change its contents. But when you refresh your page all changes will be ignored. So, after changing the info you should store it in the database.

First, insert the code below into your Contact.vb Class

Update code in contact class

Public Sub UpdateContact()
      Dim arParms() As SqlParameter = New SqlParameter(3) {}
      arParms(0) = New SqlParameter("@IDContact", Data.SqlDbType.VarChar, 38)
      arParms(0).Value = _IDContact
      arParms(1) = New SqlParameter("@City", Data.SqlDbType.VarChar, 100)
      arParms(1).Value = demo.Functions.NullSafeString(_City, 
DBNull.Value.ToString)
      arParms(2) = New SqlParameter("@Address", Data.SqlDbType.VarChar, 100)
      arParms(2).Value = demo.Functions.NullSafeString(_Address, 
DBNull.Value.ToString)
      arParms(3) = New SqlParameter("@Name", Data.SqlDbType.VarChar, 100)
      arParms(3).Value = demo.Functions.NullSafeString(_Name, 
DBNull.Value.ToString)
      SqlHelper.ExecuteNonQuery(demo.Config.Connectionstring, 
Data.CommandType.StoredProcedure, "O_Upd_Contact", arParms)
End Sub

This will call stored procedure O_Upd_Contact.

So, implement this stored procedure.

Stored procedure for O_Upd_Contact

USE [yentel]
GO
/****** Object:  StoredProcedure [eilering].[O_Upd_Contact]    Script Date: 
08/17/2011 13:10:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [eilering].[O_Upd_Contact]
      (
      
            @IDContact varchar (38),
            @Name varchar (100),
            @Address varchar (100),
            @City varchar (100)
      )
      AS
      SET NOCOUNT ON
 
Update Contact
      SET           
 
            Name = @Name,
            Address = @Address,
            City = @City
      WHERE IDContact  = @IDContact
Then you should modify the servicereference

Modify IService.vb

Code for IService.vb for updating

Imports System.ServiceModel
 
' NOTE: You can use the "Rename" command on the context menu to change the 
' interface name "IService1" in both code and config file together.
<ServiceContract()>
Public Interface IService1
 
    <OperationContract()>
    Sub DoWork()
 
    <OperationContract()> _
    Function FindContactList() As List(Of demo.Contact)
 
    <OperationContract()> _
    Function UpdateContactList(ByVal ModifiedRow As demo.Contact)
 
End Interface

Code for Service1.svc.vb for updating

Imports System.Data.SqlClient
 
' NOTE: You can use the "Rename" command on the context menu to change the class 
' name "Service1" in code, svc and config file together.
Public Class Service1
    Implements IService1
 
    Public Sub DoWork() Implements IService1.DoWork
    End Sub
 
    Public Function FindContactList() As List(Of demo.Contact) Implements 
IService1.FindContactList
        Return New ContactManager().FindContactList()
    End Function
 
    Public Function UpdateContactList(ByVal ModifiedRow As demo.Contact) 
Implements IService1.UpdateContactList
        ModifiedRow.UpdateContact()
    End Function
 
 
    Public Class ContactManager
        Public Function FindContactList() As List(Of demo.Contact)
            Dim OContact As demo.Contact
            OContact = New demo.Contact
 
            Dim Bdr As System.Data.SqlClient.SqlDataReader  ' IS CLOSED BELOW F
            Bdr = OContact.GetsContactAll
 
            Dim ContactList = New List(Of demo.Contact)()
            If Bdr IsNot Nothing Then
                Do While Bdr.Read()
                    Dim NewContact = CreateNewContact(Bdr)
                    ContactList.Add(NewContact)
                Loop
            End If
            Bdr = Nothing
 
            Return ContactList
        End Function
 
        Private Function CreateNewContact(ByVal rdr As SqlDataReader) As 
demo.Contact
            Dim NewContact = New demo.Contact With {
                .IDContact = rdr("IDContact").ToString(),
                .Name = rdr("Name").ToString(),
                .Address = rdr("Address").ToString(),
                .City = rdr("City").ToString()
            }
            Return NewContact
        End Function
    End Class
End Class

Here a new function UpdateContactList is inserted for updating. It expects a row from the datagrid after modifying it. Before going on, build your solution.

Then you should update your servicereference by right clicking your ServisReference1 and selecting Update Service reference.

This MUST be done before you can use the update function in your MainPage.aspx.vb code.

If you insert the update code in your MainPage.aspx.vb code before updating your reference, you get an error.

Then you should include the code which handles the modification in your mainPage.aspx.vb

Code for handling updates from datagrid.

  Private Sub DataGrid1_RowEditEnded(ByVal sender As ObjectByVal e As 
System.Windows.Controls.DataGridRowEditEndedEventArgs) Handles 
DataGrid1.RowEditEnded
<span lang=NL>        </span>'TAKE THE MODIFIED ROW
        Dim ModifiedRow As ServiceReference1.Contact = New 
ServiceReference1.Contact
        ModifiedRow = CType(sender, DataGrid).SelectedItem
 
        'UPDATE THE DATABASE
        Dim proxy As ServiceReference1.Service1Client = New 
ServiceReference1.Service1Client
        AddHandler proxy.UpdateContactListCompleted, AddressOf 
GetUpdateContactcompleted
        proxy.UpdateContactListAsync(ModifiedRow)
    End Sub
 
    Sub GetUpdateContactcompleted(ByVal sender As ObjectByVal e As 
ServiceReference1.UpdateContactListCompletedEventArgs)
        Dim proxy As ServiceReference1.Service1Client = New 
ServiceReference1.Service1Client
        AddHandler proxy.FindContactListCompleted, AddressOf GetContactcompleted
        proxy.FindContactListAsync()
    End Sub
 

When you have finished editing, DataGrid1_RowEditEnded is called. As seen before, first the proxy is created and a handler is added to the servicereference1 UpdateContactListCompleted routine. If this routine is finished GetUpdateContactcompleted is called.

 

If you have changed the information in the database you have to refresh the data in your datagrid because there may be others who have modified other records. So after updating call FindContactListAsync again.


View Entire Article

User Comments

Title: C# Code example   
Name: eilering
Date: 2011-09-16 3:50:25 AM
Comment:
The VB code is very basic. If you copy the code and use a convertor like
http://converter.telerik.com/
then you can paste it and you get the C# code.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 12:05:35 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search