Results 1 to 5 of 5

Thread: [RESOLVED] ObjectDataSource SQL Timestamp Code-Behind

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Resolved [RESOLVED] ObjectDataSource SQL Timestamp Code-Behind

    I have a web form with a GridView control on it. It has the DataKeyNames property set to "Contact_ID, TStamp". The way the form works is that the form show ALL Contacts whether they are added to the Contact List or not. Then, the user can click a checkbox to have the Contact added to or remove from the Contact List. When the user clicks on Submit, I enumerate through the GridView's rows and check to see if a change was made to the row and if so, I process the change. This could trigger an Insert, Update or Delete to the SQL database.

    Inserts are working great. The problem comes when I need to get the TStamp value in the code-behind out of the DataKeys and into the UpdateParameter or DeleteParameter.

    ObjectDataSource1.UpdateParameters("TStamp").DefaultValue = GridView1.DataKeys(gvRow.RowIndex)("TStamp") gives me an invalid conversion from byte[] to string. The DefaultValue of the UpdateParameter only takes a string value.

    How do I hand it a byte[]?

    Thanks in advance!!
    My.Settings.Signature = String.Empty

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: [RESOLVED] ObjectDataSource SQL Timestamp Code-Behind

    I knew if I kept beating on this, I would find an answer. Here is what I did:

    Didn't Work:
    Code:
        Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
            Dim gvRow As GridViewRow
            Dim dkRow As DataKeys
    
            For Each gvRow In gvwContacts.Rows
                dkRow = gvwContacts.DataKeys(gvRow.RowIndex)
    
                ' The item has been removed
                With ObjectDataSource1
                    .DeleteParameters("ContactListMembership_ID").DefaultValue = dkRow("ContactListMembership_ID")
                    .DeleteParameters("TStamp").DefaultValue = dkRow("TStamp")
    
                    .Delete()
                End With
            Next
    
        End Sub
    Worked:
    Code:
        Private dkRow As DataKey
    
        Protected Sub ObjectDataSource1_Deleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles ObjectDataSource1.Deleting
            e.InputParameters.Item("ContactListMembership_ID") = dkRow("ContactListMembership_ID")
            e.InputParameters.Item("TStamp") = dkRow("TStamp")
        End Sub
    
    
        Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
            ' Do whatever must be done to save the data, close this form and reload the calling form
            Dim gvRow As GridViewRow
    
            For Each gvRow In gvwContacts.Rows
                dkRow = gvwContacts.DataKeys(gvRow.RowIndex)
    
                With ObjectDataSource1
                    .Delete()
                End With
            Next
    
        End Sub

    I shortened the code just to show the point. So, what have I learned from this??? Well, you cannot pass a byte array to an ObjectDataSource.InsertParameter, ObjectDataSource.UpdateParameter or ObjectDataSource.DeleteParameter. What you need to do is make a form-level variable to hold the value and call the ObjectDataSource.Delete() method and then use e.InputParameters to assign the value.

    I must say... I think this is silly. Why can I assign it in one way, but not another? I spent over 3 hours playing around with this.
    My.Settings.Signature = String.Empty

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] ObjectDataSource SQL Timestamp Code-Behind

    Hey,

    To me though, your first approach was slightly flawed though. Why were you trying to set the DefaultValue of the parameter? This is not what you were actually doing.

    Gary

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: [RESOLVED] ObjectDataSource SQL Timestamp Code-Behind

    I know, but there isn't any other way that I can set a value from the Click event.

    I have an Insert that I do from the Click event and I set the DefaultValue properties from there. If you have a better way of doing such a thing, please let me know.
    My.Settings.Signature = String.Empty

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] ObjectDataSource SQL Timestamp Code-Behind

    Hey,

    You would either need to "tie" the parameter value of the ObjectDataSource to a control on your page, or do something like what you are doing, i.e. setting the parameter value at the time you are doing something, i.e. Updating, Selecting, Deleting.

    The DefaultValue property should only be used as documented here:

    http://msdn.microsoft.com/en-us/libr...aultvalue.aspx

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width