Search code examples
c#mysqlasp.netn-tier-architecture

How to use SQL Update in N-Tier Data Application


I get some information and idea in this tutorial (click here) to create my first N-Tier data application with asp.net, But the problem is when I tried to use update query in my DataAccessTier I get 1 error in DataService.

Error:

Error   1   Cannot implicitly convert type 'int' to 'DataEntityTier.GW_UTADataSet.ActiveDirectory2DataTable'    C:\Users\BA-OJT\documents\visual studio 2010\Projects\GoActiveDirectory\DataService\Service1.cs 59  20  DataService

How can I fix this?

My Source-code:

Project: DataAccessTier Query: UpdateBySAN

UPDATE [dbo].[ActiveDirectory2] 
SET [SamAccountName] = @SamAccountName, [Surname] = @Surname, [GivenName] = @GivenName, [EmailAddress] = @EmailAddress, [Enabled] = @Enabled, [Guid] = @Guid, [DateCreated] = @DateCreated, [DateModified] = @DateModified, [SID] = @SID, [EmployeeNumber] = @EmployeeNumber 
WHERE (SamAccountName = @SamAccountName);

SELECT ID, SamAccountName, Surname, GivenName, EmailAddress, Enabled, Guid, DateCreated, DateModified, SID, EmployeeNumber FROM ActiveDirectory2 
WHERE (SamAccountName = @SamAccountName)

Project: DataService Class: IService1.cs

[OperationContract]
DataEntityTier.GW_UTADataSet.ActiveDirectory2DataTable UpdateAccountBySAN(
        string SamAccountName, string Surname, string GivenName, string EmailAddress,
        bool Enabled, string Guid, string DateCreated, string DateModifide, string SID,
        string EmployeeNumber);

Project: DataService Class: Service1.cs

public DataEntityTier.GW_UTADataSet.ActiveDirectory2DataTable UpdateAccountBySAN(
        string SamAccountName, string Surname, string GivenName, string EmailAddress,
        bool Enabled, string Guid, string DateCreated, string DateModified, string SID,
        string EmployeeNumber)
    {
        DataAccessTier.GW_UTADataSetTableAdapters.ActiveDirectory2TableAdapter
            ActiveDirectory2TableAdapter1
                = new DataAccessTier.GW_UTADataSetTableAdapters.ActiveDirectory2TableAdapter();

        return ActiveDirectory2TableAdapter1.UpdateBySAN(
            SamAccountName, Surname, GivenName, EmailAddress, Enabled, Guid, DateCreated,
            DateModified, SID, EmployeeNumber); //ErrorRedLine@ ActiveDirectorr2TableAdapter1.UpdateBysan() Cannot implicitly convert type 'int' to 'DataEntityTier.GW_UTADataSet.ActiveDirectory2DataTable'
    }

Solution

  • After doing some experiments I found how to solve my own problem.

    For those who following this tutorial (click here) and want to use Update Query in DataAccessTier

    To use update you can use int rather than ActiveDirectory2DataTable, Because ActiveDirectory2DataTable is only use to receive the table from DataAccessTier and the update query will only return the count of updated rows.


    Project DataAccessTier Name: UpdateAccountBySAN(@parameterhere...)

    UPDATE [dbo].[ActiveDirectory2] SET [SamAccountName] = @SamAccountName, [Surname] = @Surname, [GivenName] = @GivenName, [EmailAddress] = @EmailAddress, [Enabled] = @Enabled, [Guid] = @Guid, [DateCreated] = @DateCreated, [DateModified] = @DateModified, [SID] = @SID, [EmployeeNumber] = @EmployeeNumber WHERE (SamAccountName = @SamAccountName);
    SELECT ID, SamAccountName, Surname, GivenName, EmailAddress, Enabled, Guid, DateCreated, DateModified, SID, EmployeeNumber FROM ActiveDirectory2 WHERE SamAccountName = @SamAccountName
    

    Project: DataService Class: IService1.cs

    [OperationContract]
        int UpdateAccountBySAN( string SamAccountName, string Surname, string GivenName,string EmailAddress,bool Enabled, string Guid, string DateCreated, string DateModified,string SID, string EmployeeNumber);
    

    Project: DataService Class: Service1.cs

    public int UpdateAccountBySAN(string SamAccountName, string Surname, string GivenName, string EmailAddress,bool Enabled, string Guid, string DateCreated, string DateModified, string SID,string EmployeeNumber)
        {
            DataAccessTier.GW_UTADataSetTableAdapters.ActiveDirectory2TableAdapter
                ActiveDirectory2TableAdapter1
                    = new DataAccessTier.GW_UTADataSetTableAdapters.ActiveDirectory2TableAdapter();
            return ActiveDirectory2TableAdapter1.UpdateBySAN(SamAccountName, Surname, GivenName,
            EmailAddress, Enabled, Guid, DateCreated, DateModified, SID, EmployeeNumber);
        }
    

    and to use this in PresentationTier.

    PresentationTier

    DataServiceReference.Service1Client newService = new DataServiceReference.Service1Client();
    
    newService.UpdateAccountBySAN(_SamAccountName,_Surename,_GivenName,_EmailAddress,_Enable,_Guid,_DateCreated,_DateModified,_SID,_EmployeeNumber);
    //To know how many rows updated use: var _UpdatedRows = newService.UpdateAccountBySAN(<!--some parameter here-->);
    

    This is my answer to my problem, I hope this give some to idea future proponents. thank you.