Results 1 to 5 of 5

Thread: [RESOLVED] ExecuteNonQuery keep overwrite values

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    9

    Resolved [RESOLVED] ExecuteNonQuery keep overwrite values

    Below are a part of my coding for my system. This works fine if I only insert/update/delete one row only. But if there are more than one rows in rowstate.Modified, the ExecuteNonQuery() will overwrite the previous value and just insert/update/delete the last row that the user input only. May I know what's wrong with my coding?? How can i prevent the ExecuteNonQuery() for overwrite the values?? Or is there any others way to let user insert/update/delete values at one time??

    Code:
     Dim rowChanged As Integer = dsEmp.Tables(0).GetChanges(DataRowState.Modified).Rows.Count
                            If rowChanged > 0 Then
                                Dim rows As DataRow
                                For Each rows In dsEmp.Tables(0).GetChanges(DataRowState.Modified).Rows
                                    da.UpdateCommand.ExecuteNonQuery()
                                    da.UpdateCommand.Parameters.Clear()
                                Next
                            End If

    I really need your help and thanks for your help as well...

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: ExecuteNonQuery keep overwrite values

    Post your SQL !!

    Are you changing your SQL statement with each row you visit or does it remain static ?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    9

    Re: ExecuteNonQuery keep overwrite values

    Hi, thanks for the reply first.
    I am using stored procedure for executeNonQuery...

    Code:
    Dim deleteCommand As SqlCommand = New SqlCommand()
                deleteCommand.Connection = New SqlConnection(strConn)
    
                deleteCommand.Connection.Open()
    
                deleteCommand.CommandType = CommandType.StoredProcedure
                deleteCommand.CommandText = "dbo.Search_And_Delete_Employee"
    
                Dim param As SqlParameter
    
                param = deleteCommand.Parameters.Add("@EmpNo", SqlDbType.Int)
                param.Direction = ParameterDirection.Input
                param.Value = Me.txtEmpNo.Text
                param.SourceVersion = DataRowVersion.Original

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: ExecuteNonQuery keep overwrite values

    Don't use ExecuteNonQuery. If you have a DataTable, use a DataAdapter. You presumably used a DataAdapter to Fill the DataTable in the first place. If you didn't, you should. You then use the same DataAdapter to Update the database with the changes. Otherwise you're just making things more difficult for no gain. Follow the CodeBank link in my signature and check out my Retrieving & Saving Data thread for some relevant ADO.NET code examples.

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    9

    Re: ExecuteNonQuery keep overwrite values

    ya, jmcilhinney you are correct and I solved it using DataAdapter also...

    thanks everyone...

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