开发者

asp.net parameter update reports 1 row updated but no data in table added

开发者 https://www.devze.com 2023-04-13 07:16 出处:网络
I am getting an incorrect repsonse after an update to a table. In my RowUpdating event I have the following code which builds an Update Parameter to call an SP to update a table. I must be doing somet

I am getting an incorrect repsonse after an update to a table. In my RowUpdating event I have the following code which builds an Update Parameter to call an SP to update a table. I must be doing something wrong but everything runs fine. My rowsAffected variable reports 1 after the update, but the table never gets the data.

Here is the relevant code behind in RowUpdating event. The values are correctly populated in the parameters as well.

itemDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("myConnString").ToString()

itemDataSource.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure
itemDataSource.UpdateCommand = "usp_updatePTT"                 ' Call stored procedure

    'Establishes value of Primary key and String sent to SP 
    itemDataSource.UpdateParameters.Add("ID", updateKey)
    itemDataSource.UpdateParameters.Add("lang_String", updateString)

    Dim rowsAffected As Integer = 0
    rowsAffected = itemDataSource.Update()

    GridView4.DataBind()

and here is the SP....

@ID int,
@lang_String nvarchar(1000)

AS
    /* SET NOCOUNT ON */

BEGIN
   UPDATE       tblLangROOTptt
  开发者_JS百科 SET                lang_String = @lang_String
   WHERE        (ID = @ID)
END

RETURN

Any suggestions would be appreciated... thanks,

UPDATE: I found a solution that is working. On the original DataSource for the update command I needed to add the following....

UpdateCommand="EXEC usp_updatePTTrootString @ID=@original_ID, @lang_String=@lang_string" >

This in essence allows the original data source to call the SP. Note: that the command type needs to stay as TEXT not Stored Procedure as one might think.


It seems that you are redatabinding the table with the preupdated table values. You might have to recall the select statement that gets the values of the data sent to the datagrid.


Is it possible you are updating the table with a value that it already has?


I think there are a couple of problems all of them related to doing too much in the row_updated method.

If you are in row_Updated, that means that your SQL data source is already in the process of being updated, so then you are calling a separate update before the previous set of updates completes (you're only in the row and I'm assuming there are multiple rows).

Then, you are binding the gridview that is tied to the datasource before you re-update the DataSource.

I would suggest using a separate DataSource to do your secondary update. For example:

Sub MyGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("myConnString").ToString())
    Dim cmd As New SqlCommand("usp_updatePPT")
    cmd.Connection = conn
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.AddWithValue("@ID", updateKey)
    cmd.Parameters.AddWithValue("@lang_string", updateString)
    Try
        conn.Open()
        Dim rdr As SqlDataReader = cmd.ExecuteReader()
        Dim dt As New DataTable()
        dt.Load(rdr, LoadOption.Upsert)
        conn.Close()
    Catch ex As Exception
        If conn.State > 0 Then
            conn.Close()
        End If
        'TODO: Your error handling logic here
    End Try
End Sub

As for updating the GridView again, just do a page refresh (unless you want to significantly alter the flow of your code-behind).


UPDATE: I found a solution that is working. On the original DataSource for the update command I needed to add the following....

UpdateCommand="EXEC usp_updatePTTrootString @ID=@original_ID, @lang_String=@lang_string" >

This in essence allows the original data source to call the SP. Note: that the command type needs to stay as TEXT not Stored Procedure as one might think.

0

精彩评论

暂无评论...
验证码 换一张
取 消