Doing Objects in Visual Basic 2005 Project Question
I am hoping that someone from the forum is familiar with the Doing Objects in Visual Basic 2005 book so that this question makes a little more sense. I have completed the sample project and understand most of what I have done. There are some holes in my knowledge but I am sure I will pick up those pieces as I become more familiar with object orientated programming.
The sample project is an almost complete application where you view and edit Objects that are stored in a database. The author leaves the remaining features needed up to the reader, which is great because I have been able to put new knowledge into use. However I am having trouble saving a new object back to the database.
I think this is the proper way to add a new object to the binding source
VB Code:
Public Function ProcessNew() As Boolean Implements IMDIChild.ProcessNew
Dim myGoal As Goal = TryCast(GoalBindingSource.AddNew, Goal)
myGoal.Save()
End Function
And I think this block of code from the Class has everything it needs to save back to the database
VB Code:
Public Function Save() As Boolean
Dim dt As DataTable = DAC.ExecuteDataTable(My.Resources.sp_Update, _
DAC.Parameter(CN_GoalID, GoalID), _
DAC.Parameter(CN_Goal, Goal), _
DAC.Parameter(CN_RowState, EntityState.ToString))
If EntityState = EntityStateEnum.Added Then
GoalID = CType(dt.Rows(0).Item(CN_GoalID), Integer)
End If
DataStateChanged(EntityStateEnum.Unchanged)
Return True
End Function
By looking at this code it appears that I can save the data that is in the bindingsource by changing the EntityState to the Enum added. However I am not sure where to set it. I am pretty confident that I have the rest of this correct because when I try to do the save I get an error that the stored procedure is expecting the GoalID.
Re: Doing Objects in Visual Basic 2005 Project Question
The error I am getting is 'UPD_Goal' expects parameter '@Goal', which was not supplied, which is why I think the status of my EntityState IF THEN is the key to correcting this. If it helps here is the stored procedure.
My stored procedure (UPD_Goal) looks like this
vb Code:
IF @RowState = 'Added'
BEGIN
SET NOCOUNT ON;
INSERT INTO Goals (Goal)
VALUES (@Goal)
SELECT ID = @@Identity
END
Re: Doing Objects in Visual Basic 2005 Project Question
I'm going to guess there's more to the sproc than just that... can you post the whole thing? The bit I'm most interested in is the parameters declaration of the sproc.
-tg
Re: Doing Objects in Visual Basic 2005 Project Question
Please Delete Post. I accidently posted to the wrong thread.
Re: Doing Objects in Visual Basic 2005 Project Question
Here it is:
Code:
ALTER PROCEDURE [dbo].[UPD_Goal]
( @GoalID int
, @Goal varchar(max)
, @RowState VarChar(50)
)
AS
IF @RowState = 'Added'
BEGIN
SET NOCOUNT ON;
INSERT INTO Goals (Goal)
VALUES (@Goal)
SELECT ID = @@Identity
END
IF @RowState = 'Modified'
BEGIN
UPDATE Goals
SET Goal = @Goal
WHERE GoalID = @GoalID
END
IF @RowState = 'Deleted'
BEGIN
Update Goals Set IsDeleted = 1
WHERE GoalID = @GoalID
END
RETURN
Re: Doing Objects in Visual Basic 2005 Project Question