|
-
Mar 24th, 2010, 11:48 AM
#1
Thread Starter
Lively Member
[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?
-
Mar 24th, 2010, 11:52 AM
#2
Frenzied Member
Re: Updated database, now need to re-read?
show us your current code pls
If you find my reply helpful , then rate it
-
Mar 24th, 2010, 11:53 AM
#3
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
-
Mar 24th, 2010, 11:57 AM
#4
Thread Starter
Lively Member
Re: Updated database, now need to re-read?
 Originally Posted by gautamshaw
show us your current code pls
OK Code:
Private Sub myAddNew(ByVal CusRef As Integer)
Dim ConnectionString As String
Dim SQLString As String
Dim glassOrPlastic As String = String.Empty
Dim scratchCoating As String = String.Empty
Dim uvFilter As String = String.Empty
Dim whichButtonDialogResult As DialogResult
Dim dbCommand As System.Data.OleDb.OleDbCommand
Dim Connection As System.Data.OleDb.OleDbConnection
If GlassRadioButton.Checked = True Then
glassOrPlastic = "Glass"
ElseIf PlasticRadioButton.Checked = True Then
glassOrPlastic = "Plastic"
End If
If CheckBox1.Checked = True Then
scratchCoating = "Yes"
Else
scratchCoating = "No"
End If
If CheckBox2.Checked = True Then
uvFilter = "Yes"
Else
uvFilter = "No"
End If
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data "
ConnectionString += "Source=" & "Opticians.accdb "
Connection = New System.Data.OleDb.OleDbConnection(ConnectionString)
SQLString = "INSERT INTO SpecSalesTable (CustomerID, EyeTestID, FrameID, DateOfSale, GlassOrPlastic, ScratchCoating, UVFilter, TotalCost, DepositPaid) "
SQLString += "Values ('" & CustomerIDTextBox.Text & "','" & EyeTestIDTextBox.Text & "','" & StockTextBox.Text & "',#" & Date.Today & "#,'" & glassOrPlastic & "','" & scratchCoating & "','" & uvFilter & "','" & TextBox1.Text & "','" & TextBox2.Text & "')"
whichButtonDialogResult = MessageBox.Show("Are You Sure You Want To Place This Order?", "Add Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If whichButtonDialogResult = DialogResult.Yes Then
Connection.Open()
If CBool(ConnectionState.Open) Then
dbCommand = New System.Data.OleDb.OleDbCommand(SQLString, Connection)
Try
dbCommand.ExecuteNonQuery()
MessageBox.Show("Order placed, SpecSalesTable Updated! ")
Catch ex As Exception
MessageBox.Show(" Error placing order... ") 'IF ERROR DISPLAYS MESSAGE
End Try
End If
End If
Connection.Close()
End Sub
Gary, where does the Select Identity go?
I suppose I'd use Select Max ID? (to find the most recent)
-
Mar 24th, 2010, 11:59 AM
#5
Re: Updated database, now need to re-read?
No, just use the sql query as 'ExecuteScalar' and it should return the last autonumber ID inserted.
-
Mar 24th, 2010, 12:01 PM
#6
Re: Updated database, now need to re-read?
after this line:
dbCommand.ExecuteNonQuery()
add
vb.net Code:
SQLString = "Select @@IDentity"
cmd.Text = SQLString
Dim pkInserted as Integer = cmd.ExecuteScaler()
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Mar 24th, 2010, 12:06 PM
#7
Re: Updated database, now need to re-read?
Pretty sure it's ExecuteScalar Gary 
I see Scaler in many places, but it's definitely scalar, as in 'one value'.
-
Mar 24th, 2010, 12:08 PM
#8
Thread Starter
Lively Member
Re: Updated database, now need to re-read?
What do I declare "cmd" as?
-
Mar 24th, 2010, 12:09 PM
#9
Re: Updated database, now need to re-read?
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Mar 24th, 2010, 12:11 PM
#10
Thread Starter
Lively Member
Re: Updated database, now need to re-read?
Error 1 'Text' is not a member of 'System.Data.OleDb.OleDbCommand'.
-
Mar 24th, 2010, 12:26 PM
#11
Thread Starter
Lively Member
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'.
-
Mar 24th, 2010, 12:27 PM
#12
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
-
Mar 24th, 2010, 12:30 PM
#13
Thread Starter
Lively Member
Re: Updated database, now need to re-read?
Very sorry, completely lost on how to remedy it? Don't really understand it
-
Mar 24th, 2010, 12:34 PM
#14
Re: Updated database, now need to re-read?
vb.net Code:
Try
dbCommand.ExecuteNonQuery()
MessageBox.Show("Order placed, SpecSalesTable Updated! ")
SQLString = "SELECT @@IDENTITY"
dbCommand.CommandType = CommandType.Text
dbCommand.CommandText = SQLString
Dim PKLastInserted As Integer = dbCommand.ExecuteScaler()
MessageBox.Show("Last inserted Identity is: " & PKLastInserted.ToString())
Catch ex As Exception
MessageBox.Show(" Error placing order... ") 'IF ERROR DISPLAYS MESSAGE
End Try
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Mar 24th, 2010, 12:43 PM
#15
Thread Starter
Lively Member
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
-
Mar 24th, 2010, 12:52 PM
#16
Re: [RESOLVED] Updated database, now need to re-read?
Comments of what is going on in the code
vb.net Code:
Try
dbCommand.ExecuteNonQuery()
MessageBox.Show("Order placed, SpecSalesTable Updated! ")
'Reset the SQL Command I am going to run here
SQLString = "SELECT @@IDENTITY"
'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
'Ensure that the command object (dbCommand) command type is set to text
dbCommand.CommandType = CommandType.Text
'Assign the new SQLString value to the command objects text (what will be run)
dbCommand.CommandText = SQLString
'Declare a var as type Integer and set its value to the return of ExecuteScaler()
Dim PKLastInserted As Integer = dbCommand.ExecuteScalar()
MessageBox.Show("Last inserted Identity is: " & PKLastInserted.ToString())
Catch ex As Exception
MessageBox.Show(" Error placing order... ") 'IF ERROR DISPLAYS MESSAGE
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|