Results 1 to 7 of 7

Thread: saving Datagrid edits to the Database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Location
    N.Ireland
    Posts
    71

    saving Datagrid edits to the Database

    Hi - I'm creating an admin application that will allow specific users to manage users login credentials for an application. On a form I have a datagrid that is populated by selecting * from Operator table on the DB. I want the admin users to be able to add, edit and delete records from the datagrid and i have enabled these properties on the grid but any changes need to be confirmed by clicking a 'Saves Changes' button. Here's the code behind this button:
    vb Code:
    1. Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    2.         Const cSUBNAME As String = "btnSave_Click"
    3.  
    4.         Try
    5.  
    6.             'Open the DataBase connection which will be used to execute the SP
    7.             If DatabaseConnectionOpen() = False Then
    8.                 Call WriteError(cMODULENAME, cSUBNAME, Err.Number, Err.Description)
    9.                 GoTo ExitNow
    10.             End If
    11.  
    12.  
    13.             Dim cb As New SqlCommandBuilder(da)
    14.  
    15.             cb.GetUpdateCommand()
    16.             da.Update(dt)
    17.  
    18.             DatabaseConnectionClose()
    19.  
    20.             FillOperatorGrid()
    21. ExitNow:
    22.         Catch ex As Exception
    23.             Call WriteError(cMODULENAME, cSUBNAME, Err.Number, Err.Description)
    24.         End Try
    25.  
    26.     End Sub
    I have added a new record but when I save these changes I am getting this error: "02/12/2011 11:43:56,frmOperator,btnSave_Click,5,Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information."

    Can anyone enlighten me as to what is wrong please?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: saving Datagrid edits to the Database

    there's no key column information because you're using:

    vb Code:
    1. SELECT *

    you need to use:

    vb Code:
    1. SELECT field1, field2, field3 FROM tableName

    then you should be able to generate an updateCommand

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Location
    N.Ireland
    Posts
    71

    Re: saving Datagrid edits to the Database

    I have changed my Select and am still getting the same error, here's my revised code to fill the datagrid
    vb Code:
    1. Public Function FillOperatorGrid()
    2.         Const cSUBNAME As String = "FillOperatorGrid"
    3.  
    4.         Dim lstrSQL As String
    5.  
    6.         Try
    7.             If gDebugging Then Debugging("Start")
    8.  
    9.             'Open the DataBase connection which will be used to execute the SP
    10.             If DatabaseConnectionOpen() = False Then
    11.                 Call WriteError(cMODULENAME, cSUBNAME, Err.Number, Err.Description)
    12.                 GoTo ExitNow
    13.             End If
    14.  
    15.             lstrSQL = "SELECT Operator, Description, Password, Protean_ID, Site, Area, Flag_Schedule, Flag_Declaration, Flag_Movement, Flag_PickShip FROM dbo.mp_SUFrance_Operator"
    16.  
    17.             'Create New Sql Command to execute Insert statement
    18.             Dim cmd As New SqlCommand(lstrSQL, gSQLcn)
    19.             Dim value As Integer = 600
    20.             cmd.CommandTimeout = value
    21.             cmd.ExecuteNonQuery()
    22.  
    23.             da = New SqlDataAdapter(cmd)
    24.             da.Fill(dt)
    25.  
    26.             dgOperator.DataSource = dt
    27.  
    28.             cmd.Dispose()
    29.  
    30.             DatabaseConnectionClose()
    31.  
    32.             dgOperator.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    33.             If gDebugging Then Debugging("Start")
    34. ExitNow:
    35.             Exit Function
    36.         Catch ex As Exception
    37.             Call WriteError(cMODULENAME, cSUBNAME, Err.Number, Err.Description)
    38.         End Try
    39.     End Function

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: saving Datagrid edits to the Database

    How to update a database from a DataSet object by using Visual Basic .NET:

    http://support.microsoft.com/kb/301248

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Location
    N.Ireland
    Posts
    71

    Re: saving Datagrid edits to the Database

    it turns out that the Database table to be updated needs to have a primary key set for dataset edits and deletes to update the DB table. Thanks to your link above my code for retrieving the data and doing the updates now looks like this:
    vb Code:
    1. Private Sub frmAppParameters_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.         Const cSUBNAME As String = "frmAppParameters_Load"
    3.  
    4.         Try
    5.  
    6.             If DatabaseConnectionOpen() = False Then
    7.                 Call WriteError(cMODULENAME, cSUBNAME, Err.Number, Err.Description)
    8.                 GoTo ExitNow
    9.             End If
    10.  
    11.             lstrSQL = "SELECT Printer AS 'Id imprimante'," & _
    12.                   " IPAddress AS 'Adresse IP'," & _
    13.                   " Description," & _
    14.                   " Comment AS 'Commentaires' " & _
    15.                   " FROM dbo.mp_SUFrance_Printer"
    16.  
    17.             Dim cmd As New SqlCommand(lstrSQL, gSQLcn)
    18.             Dim value As Integer = 600
    19.             cmd.CommandTimeout = value
    20.             cmd.ExecuteNonQuery()
    21.  
    22.             adapter = New SqlDataAdapter(cmd)
    23.             adapter.Fill(ds)
    24.             DatabaseConnectionClose()
    25.             dg.DataSource = ds.Tables(0)
    26.  
    27. ExitNow:
    28.             Exit Sub
    29.         Catch ex As Exception
    30.             Call WriteError(cMODULENAME, cSUBNAME, Err.Number, Err.Description)
    31.         End Try
    32.     End Sub
    33.  
    34.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    35.  
    36.         Try
    37.             cmdBuilder = New SqlCommandBuilder(adapter)
    38.             changes = ds.GetChanges()
    39.             If changes IsNot Nothing Then
    40.                 adapter.Update(changes)
    41.             End If
    42.             MsgBox("These changes have been updated in the Database")
    43.             dg.RefreshEdit()
    44.         Catch ex As Exception
    45.             MsgBox(ex.ToString)
    46.         End Try
    47.  
    48.     End Sub
    49.  
    50.     Private Sub btnReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReturn.Click
    51.  
    52.         dg.EndEdit()
    53.         Me.Close()
    54.         frmMain.Show()
    55.  
    56.     End Sub

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: saving Datagrid edits to the Database

    Why are you executing your query twice? Why are you executing your query with a call to ExecuteNonQuery?

    Is it really reasonable to wait 10 minutes for your query to execute? How much data are you retrieving?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Location
    N.Ireland
    Posts
    71

    Re: saving Datagrid edits to the Database

    at present the data being returned is not a lot but over time the table size will increase but reducing the timeout value is something I will consider.

    As for executing the query twice, I had used the ExecuteNonQuery in another application and had copied it across without actually realising that I was doing this twice, thanks for highlighting that.

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