Results 1 to 16 of 16

Thread: [RESOLVED] Updated database, now need to re-read?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Location
    Ireland
    Posts
    85

    Resolved [RESOLVED] Updated database, now need to re-read?

    I've updated my database...

    And all the information goes in fine, and an Auto-Number is allocated.

    Do I need to re-read the database to find & display this auto-number?

    Where do I begin?

  2. #2
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Updated database, now need to re-read?

    show us your current code pls

    If you find my reply helpful , then rate it

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Updated database, now need to re-read?

    You can query the database (on the same open connection for the Select @@Identity)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Location
    Ireland
    Posts
    85

    Re: Updated database, now need to re-read?

    Quote Originally Posted by gautamshaw View Post
    show us your current code pls
    OK Code:
    1. Private Sub myAddNew(ByVal CusRef As Integer)
    2.         Dim ConnectionString As String
    3.         Dim SQLString As String
    4.         Dim glassOrPlastic As String = String.Empty
    5.         Dim scratchCoating As String = String.Empty
    6.         Dim uvFilter As String = String.Empty
    7.         Dim whichButtonDialogResult As DialogResult
    8.         Dim dbCommand As System.Data.OleDb.OleDbCommand
    9.         Dim Connection As System.Data.OleDb.OleDbConnection
    10.  
    11.         If GlassRadioButton.Checked = True Then
    12.             glassOrPlastic = "Glass"
    13.         ElseIf PlasticRadioButton.Checked = True Then
    14.             glassOrPlastic = "Plastic"
    15.         End If
    16.  
    17.         If CheckBox1.Checked = True Then
    18.             scratchCoating = "Yes"
    19.         Else
    20.             scratchCoating = "No"
    21.         End If
    22.  
    23.         If CheckBox2.Checked = True Then
    24.             uvFilter = "Yes"
    25.         Else
    26.             uvFilter = "No"
    27.         End If
    28.  
    29.         ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data "
    30.         ConnectionString += "Source=" & "Opticians.accdb "
    31.         Connection = New System.Data.OleDb.OleDbConnection(ConnectionString)
    32.         SQLString = "INSERT INTO SpecSalesTable (CustomerID, EyeTestID, FrameID, DateOfSale, GlassOrPlastic, ScratchCoating, UVFilter, TotalCost, DepositPaid) "
    33.         SQLString += "Values ('" & CustomerIDTextBox.Text & "','" & EyeTestIDTextBox.Text & "','" & StockTextBox.Text & "',#" & Date.Today & "#,'" & glassOrPlastic & "','" & scratchCoating & "','" & uvFilter & "','" & TextBox1.Text & "','" & TextBox2.Text & "')"
    34.         whichButtonDialogResult = MessageBox.Show("Are You Sure You Want To Place This Order?", "Add Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    35.         If whichButtonDialogResult = DialogResult.Yes Then
    36.             Connection.Open()
    37.             If CBool(ConnectionState.Open) Then
    38.                 dbCommand = New System.Data.OleDb.OleDbCommand(SQLString, Connection)
    39.                 Try
    40.                     dbCommand.ExecuteNonQuery()
    41.                     MessageBox.Show("Order placed, SpecSalesTable Updated! ")
    42.                 Catch ex As Exception
    43.                     MessageBox.Show(" Error placing order... ")    'IF ERROR DISPLAYS MESSAGE
    44.                 End Try
    45.  
    46.             End If
    47.  
    48.         End If
    49.  
    50.         Connection.Close()
    51.     End Sub


    Gary, where does the Select Identity go?

    I suppose I'd use Select Max ID? (to find the most recent)

  5. #5

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Updated database, now need to re-read?

    after this line:

    dbCommand.ExecuteNonQuery()

    add

    vb.net Code:
    1. SQLString = "Select @@IDentity"
    2. cmd.Text = SQLString
    3. Dim pkInserted as Integer = cmd.ExecuteScaler()
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Location
    Ireland
    Posts
    85

    Re: Updated database, now need to re-read?

    What do I declare "cmd" as?

  9. #9
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Updated database, now need to re-read?

    use your dbCommand
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Location
    Ireland
    Posts
    85

    Re: Updated database, now need to re-read?

    Error 1 'Text' is not a member of 'System.Data.OleDb.OleDbCommand'.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Location
    Ireland
    Posts
    85

    Re: Updated database, now need to re-read?

    Do I need to change something there?

    cmd.Text = SQLString

    Error 1 'Text' is not a member of 'System.Data.OleDb.OleDbCommand'.

  12. #12
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Updated database, now need to re-read?

    I did not enter this in the ide just typed here.

    Look at the commad objects properties one of them will be the text you want to run.


    .CommandType = CommandType.Text
    .CommandText = SQLString
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Location
    Ireland
    Posts
    85

    Re: Updated database, now need to re-read?

    Very sorry, completely lost on how to remedy it? Don't really understand it

  14. #14
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Updated database, now need to re-read?

    vb.net Code:
    1. Try
    2.      dbCommand.ExecuteNonQuery()
    3.      MessageBox.Show("Order placed, SpecSalesTable Updated! ")
    4.      SQLString = "SELECT @@IDENTITY"
    5.      dbCommand.CommandType =  CommandType.Text
    6.      dbCommand.CommandText = SQLString
    7.      Dim PKLastInserted As Integer = dbCommand.ExecuteScaler()
    8.      MessageBox.Show("Last inserted Identity is: " & PKLastInserted.ToString())
    9. Catch ex As Exception
    10.      MessageBox.Show(" Error placing order... ")    'IF ERROR DISPLAYS MESSAGE
    11. End Try
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Location
    Ireland
    Posts
    85

    Re: Updated database, now need to re-read?

    Works perfect, just incase anyone else is checking here,

    ExecuteScaler needs to be ExecuteScalar

    Thanks gary, once again

  16. #16
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [RESOLVED] Updated database, now need to re-read?

    Comments of what is going on in the code
    vb.net Code:
    1. Try
    2.     dbCommand.ExecuteNonQuery()
    3.     MessageBox.Show("Order placed, SpecSalesTable Updated! ")
    4.     'Reset the SQL Command I am going to run here
    5.     SQLString = "SELECT @@IDENTITY"
    6.     'The Select @@IDENTITY will return the last inserted identity value on the current command/connection object so we need to run it using the current command
    7.     'Ensure that the command object (dbCommand) command type is set to text
    8.     dbCommand.CommandType = CommandType.Text
    9.     'Assign the new SQLString value to the command objects text (what will be run)
    10.     dbCommand.CommandText = SQLString
    11.     'Declare a var as type Integer and set its value to the return of ExecuteScaler()
    12.     Dim PKLastInserted As Integer = dbCommand.ExecuteScalar()
    13.     MessageBox.Show("Last inserted Identity is: " & PKLastInserted.ToString())
    14. Catch ex As Exception
    15.     MessageBox.Show(" Error placing order... ")    'IF ERROR DISPLAYS MESSAGE
    16. End Try
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Tags for this Thread

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