Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / May 2005

Tip: Looking for answers? Try searching our database.

UpdateCommand not returning correct value to DataSet

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
joanneshovelton@yahoo.co.uk - 16 May 2005 16:54 GMT
Hi,

I am having trouble with my UpdateCommand stored procedure. It updates
the row in the Data Source OK but keeps returning the original value of
"Cost" to the DataSet rather than the newly updated value. Any values
changed in the stored procedure before the Update statement seem to be
returned correctly. I set parameters such as @Cost to InputOutput
because sometimes they need to be changed in the stored procedure e.g.
when @Cost=-1.

Here is a simplified version of stored procedure and C# calling code
follows:-

*** BEGIN **************

ALTER PROCEDURE spUpdateRowInSubscription
@UserID int,
@Cost money OUTPUT,
@ID int
AS

--Cost values of -1, set them to null
if @Cost=-1
begin
    -- set it to null
    select @Cost=null
end

UPDATE subscription
SET
UserID = @UserID,
Cost = @Cost
WHERE ID=@ID

select @UserID UserID,
@Cost Cost

RETURN

*** END ****************

And here is the calling code in c#:-

*** BEGIN ****************
public void UpdateSub(DataSet dsSubs)
        {
            //Connection string
            string strConnection = "Provider=MSDataShape; Data
Provider=SQLOLEDB; server=SERVER; uid=myID; pwd=myPwd; database=sctc;
Connect Timeout=90";

            //Connection object
            OleDbConnection conn = new OleDbConnection(strConnection);

            //create command objects using stored procedures etc.
            //UpdateRowSource property of command objects defaults to 'Both'

            //Select
            OleDbCommand selectCommand = new OleDbCommand(cstrTableName,conn);
            selectCommand.CommandType = CommandType.TableDirect;

            //Update
            OleDbCommand updateCommand = new
OleDbCommand("spUpdateRowInSubscription",conn);
            updateCommand.CommandType = CommandType.StoredProcedure;

            // ...

            //Set up the parameters
            OleDbParameterCollection cparams;

            //Update command parameters
            //-------------------------
            cparams=updateCommand.Parameters;

            //input/output params
            cparams.Add("@UserID",System.Data.OleDb.OleDbType.Integer,4,"UserID");
            cparams["@UserID"].Direction = System.Data.ParameterDirection.Input;
            cparams["@UserID"].SourceVersion = DataRowVersion.Current;

            cparams.Add("@Cost",System.Data.OleDb.OleDbType.Currency,8,"Cost");
            cparams["@Cost"].Direction =
System.Data.ParameterDirection.InputOutput;
            cparams["@Cost"].SourceVersion = DataRowVersion.Current;

            //primary key
            AddParameterToCollection(cparams,"@ID",System.Data.OleDb.OleDbType.Integer,4,"ID",ParameterDirection.Input,DataRowVersion.Original);

            //Create the DataAdapter
            //----------------------
            OleDbDataAdapter da = new
OleDbDataAdapter(selectCommand.CommandText,conn);
            // ...
            da.UpdateCommand = updateCommand;

            //Update the datasource using the custom update logic
            da.Update(dsSubs,"subscription");
        }
*** END ********************************

Can anyone tell me how to return the current value of "Cost" to the
DataSet?

Thanks in advance for your help,
Jo

P.S. I did try putting a SELECT statement at the end of the sproc but
it didn't make any difference.
Rogas69 - 17 May 2005 11:36 GMT
Well, actually you are returning the same Cost value unless you pass -1 to
the stored proc and then you return -1. On the other hand, typical update
command does not return any result - it calls Command.ExecuteNonQuery
method. If you want to get control what is returned from this procedure, you
have to call stored procedure on your own.
If you want to return the value as scalar there has to be only one value as
ExecuteScalar takes first column of first row - in this case @UserID. If you
executed ExecuteRow method you could get both values but still - you are
returning only values you passed as parameters.
What is the point of returning user id you pass to the procedure anyway? If
you do not change the @Cost in procedure - which actually you do not
(except -1 case) - you know the value of @Cost even before the procedure is
called. And in -1 case you get NULL as result anyway.
And you do not have to make @Cost parameter OUTPUT to change locally its
value in sp anyway.

Peter

> Hi,
>
[quoted text clipped - 104 lines]
> P.S. I did try putting a SELECT statement at the end of the sproc but
> it didn't make any difference.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.