Question about using "New"

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Hi Everyone,

Just wondering if using "New" repeatedly will eventually use of memory or this is not a very efficient way to write code.

Here's an example I did with a command builder:

VB.NET:
            ' Add a new blank row into the Data Set if the user chose to Insert.
            '-------------------------------------------------------------------
            If booleanAddNew Then
                objDataSetParentDetails.Tables("Parent Details").Rows.Add(objDataRowParentDetails)
            End If

            ' Create an instance of the command builder.
            '-------------------------------------------
            objCommandBuilderParentDetails = New SqlCommandBuilder(objDataAdapterParentDetails)

            If booleanAddNew Then

                ' Have the command builder create an Insert SQL command.
                '-------------------------------------------------------
                objDataAdapterParentDetails.InsertCommand = objCommandBuilderParentDetails.GetInsertCommand
            Else

                ' Have the command builder create an update SQL command.
                '-------------------------------------------------------
                objDataAdapterParentDetails.UpdateCommand = objCommandBuilderParentDetails.GetUpdateCommand
            End If

            ' Perform the update SQL command; then close the connection.
            '-----------------------------------------------------------
            objDataAdapterParentDetails.Update(objDataSetParentDetails, "Parent Details")
            objDataSetParentDetails.Tables("Parent Details").AcceptChanges()

I'm just wondering if the command builder can be created once and then just tell it to get the update or insert command because this sub procedure get called a lot.

Thanks.

Truly,
Emad
 
The CommandBuilder is linked to the DataAdapter, so you should always create the CommandBuilder immediately after creating the DataAdapter. If you only create one DataAdapter then you only create one CommandBuilder.

Also, the highlighted lines are pointless:
VB.NET:
            ' Add a new blank row into the Data Set if the user chose to Insert.
            '-------------------------------------------------------------------
            If booleanAddNew Then
                objDataSetParentDetails.Tables("Parent Details").Rows.Add(objDataRowParentDetails)
            End If

            ' Create an instance of the command builder.
            '-------------------------------------------
            objCommandBuilderParentDetails = New SqlCommandBuilder(objDataAdapterParentDetails)

            [COLOR="red"]If booleanAddNew Then

                ' Have the command builder create an Insert SQL command.
                '-------------------------------------------------------
                objDataAdapterParentDetails.InsertCommand = objCommandBuilderParentDetails.GetInsertCommand
            Else

                ' Have the command builder create an update SQL command.
                '-------------------------------------------------------
                objDataAdapterParentDetails.UpdateCommand = objCommandBuilderParentDetails.GetUpdateCommand
            End If[/COLOR]

            ' Perform the update SQL command; then close the connection.
            '-----------------------------------------------------------
            objDataAdapterParentDetails.Update(objDataSetParentDetails, "Parent Details")
            [COLOR="red"]objDataSetParentDetails.Tables("Parent Details").AcceptChanges()[/COLOR]
You DO NOT need to call GetInsertCommand, etc, on the CommandBuilder. The whole point is that it generates and uses the commands automaticlly. The ONLY reason that you would need to call those methods is if you need to change the Command somehow. The most common reason would be to enlist them in a transaction, so you need access to the Command object to set the Transaction property.
 
Back
Top