|
-
Jul 31st, 2003, 09:50 AM
#1
Thread Starter
Addicted Member
Determine IDnumber in dataset column
Is there a way to determine the last value in a column before inserting? Here's my situation. I want to insert data into my database but before do so, I want to determine the ID number and increment that number then do the insert. The problem is, how do I determine what that number is in the column. I have dataset1 and dataadapter1 in the form. But I'm not sure how I would go about using the dataset to determine the ID number in the column IDnumb.
Any suggestion will be greatly welcome!
ljCharlie
-
Jul 31st, 2003, 12:25 PM
#2
Sleep mode
If MS Access is your database you're using , then let it handle this for you . No code needed , just set datatype as autonumber .
-
Jul 31st, 2003, 12:33 PM
#3
Thread Starter
Addicted Member
Many thanks for the help. I have thought of using the autonumber in Access but the problem is, if I ever need to delete a row in the table, the autonumber is not reset back. For example, I have three rows:
IDnubmer 1, 2, 3 but if I delete row three or row IDnumber 3 and then I insert another row into the table, the IDnumber will be 4 instead of 3. And then if I delete row IDnumber 4 and again, insert another row into the table, the IDnumber is 5 instead of 3 or 4.
Do you see my problem? If I can configure this out to work properly then I guess using this autonumber is the easist method.
ljCharlie
-
Jul 31st, 2003, 01:12 PM
#4
Frenzied Member
In cases like that I would just use a guid. Just generate a new one for inserts and when you delete there wont be a problem. I never use auto number for any column. I like to keep things simple.
-
Jul 31st, 2003, 01:25 PM
#5
Thread Starter
Addicted Member
What do you mean by using a guide? The IDnumber is a primary key and it can not be duplicate so how do I determine that the next IDnumber I create will not be a duplicate?
ljCharlie
-
Jul 31st, 2003, 01:29 PM
#6
Addicted Member
I don't know if it's of any help, but in those case, i do a
Code:
sSQL = "SELECT MAX(MyID) FROM MyTable;"
...
try
objdr.read
iMaxId = objDR(0) + 1
catch
iMaxId=0
End try
...
sSQL = "INSERT INTO MyTable (MyID, Field1, ...) VALUES (" & iMaxID & ", ..., ...);"
...
-
Jul 31st, 2003, 01:35 PM
#7
Thread Starter
Addicted Member
I believed that's exactly what I'm trying to do. But because I use a dataset that I created during design time, I need to get the max number from the IDnumber column. But the problem I have is I couldn't figure out how to get that number from the column IDnumber so I can increment that number for the insert statement.
ljCharlie
-
Jul 31st, 2003, 06:38 PM
#8
Frenzied Member
A GUID is a globally unique identifier that is 128 bits long. The format is usually like this 166769E1-88E8-11cf-A6BB-0080C7B2D682. You could have have a string column as the primary key. Now each time you do any insert, you just generate a new guide and place in that column.
-
Aug 4th, 2003, 08:28 AM
#9
Thread Starter
Addicted Member
Thanks for the help. How would I generate the guide for new insert?
ljCharlie
-
Aug 4th, 2003, 06:54 PM
#10
Frenzied Member
Guid.NewGuid().ToString()
-
Aug 5th, 2003, 07:39 AM
#11
Thread Starter
Addicted Member
Thanks! And so everytime when I generate a new guide by using the code you gave me it will be unique, correct?
ljCharlie
-
Aug 5th, 2003, 07:33 PM
#12
Frenzied Member
Originally posted by Chong
Thanks! And so everytime when I generate a new guide by using the code you gave me it will be unique, correct?
ljCharlie
Yeah
-
Sep 18th, 2003, 03:42 PM
#13
Lively Member
eL_NiNo
In the code you posted can you expalin what objdr is?
sSQL = "SELECT MAX(MyID) FROM MyTable;"
...
try
objdr.read
iMaxId = objDR(0) + 1
catch
iMaxId=0
End try
...
sSQL = "INSERT INTO MyTable (MyID, Field1, ...) VALUES (" & iMaxID & ", ..., ...);"
...
Thanks,
Corinne
-
Sep 18th, 2003, 05:00 PM
#14
Addicted Member
It's a DataReader.
Code:
Dim objDR as OleDBDataReader
or
Dim objDR as ODBCDataReader
or
...
-
Sep 19th, 2003, 10:34 AM
#15
Lively Member
I have a save routine that is working just fine up to this point. I need the application to autosave an ID field that is specified as an int and identity increment is 1. Everything saves fine. My problem comes in when I also need an eight character serial number to be autogenerated ie: LC000001, for the row that was just saved. This field is specified in the SQL table as char. What would be the best way to grab the last serial number in the table and increment it by 1? Can this be done since it is a char field? I'm very confused on where to go to accomplish this.
Thanks,
Corinne
-
Sep 25th, 2003, 11:03 AM
#16
Lively Member
-
Oct 19th, 2005, 03:52 PM
#17
Member
Re: Determine IDnumber in dataset column
eL_Nino I tried to use your code to auto increment my Ticket Number field for an error reporting program I wrote and it doesn't seem to want to auto increment. This is the last error I have to fix before I can finalize my program to version 1.0. I know I probably have to initialize the connection first, however I am unsure of how to correctly do so. It seems to always go to the catch because it keeps trying to insert a 0.
Here is my code:
VB Code:
' This sub is used to add a error report to the database
Dim cmSQL As SqlCommand
Dim strSQL As String
Dim ssql As String
Dim sqldr As SqlDataReader
Dim iTickNum As Integer
Dim SQL_CONNECTION_STRING_TEST As String = "Server=myserver; DataBase=TESTPAERS; uid=****; pwd=****"
ssql = "SELECT MAX([Ticket Number]) FROM Errors;"
Try
sqldr.Read()
iTickNum = sqldr(0) + 1
Catch
iTickNum = 0
End Try
Try
'Create the SQL Query and insert a row
strSQL = "INSERT INTO Errors ([Ticket Number], Name, " & _
"Program, Problem, Department, " & _
"Frequency, People, Description, Date, Time) " & _
"VALUES('" & iTickNum & "' , " & _
"'" & cboName.Text & "' , " & _
"'" & cboProg.Text & "' , " & _
"'" & cboProb.Text & "' , " & _
"'" & cboDept.Text & "' , " & _
"'" & cboFreq.Text & "' , " & _
"'" & radYes.Checked & "' , " & _
"'" & txtDesc.Text & "' , " & _
"'" & txtDate.Text & "' , " & _
"'" & txtTime.Text & "')"
cnSQL = New SqlConnection(SQL_CONNECTION_STRING_TEST)
cnSQL.Open()
cmSQL = New SqlCommand(strSQL, cnSQL)
cmSQL.ExecuteNonQuery()
' Close and Clean up objects
cnSQL.Close()
cmSQL.Dispose()
cnSQL.Dispose()
Catch Exp As SqlException
MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
End Try
'Show success messagebox, clear form and reset focus.
MsgBox("You have successfully submitted your " & _
"error report.", MsgBoxStyle.Information, Me.Text)
Last edited by pOfa; Oct 19th, 2005 at 05:45 PM.
Reason: More information
Chris Griffin: When I stick this army guy with the sharp bayonette up my nose, it tickles my brain. Hah hah hah ... ow. Oh, now I don't know math.
-
Oct 20th, 2005, 09:44 AM
#18
Addicted Member
Re: Determine IDnumber in dataset column
pOfa :
In your try statement, you're not handling the NULL value neither if it can read; I guess your table is empty, thus always return 0 in the Catch.
VB Code:
Try
sqldr.Read()
iTickNum = sqldr(0) + 1 ' what if it is null ? Use IsDbNull()... and assign next logical value, in this case 1
Catch
iTickNum = 0
End Try
Edited: Oh and you should use a transaction if available for all these operations....
Good luck!
-
Oct 20th, 2005, 10:19 AM
#19
Member
Re: Determine IDnumber in dataset column
 Originally Posted by eL_NiNo
pOfa :
In your try statement, you're not handling the NULL value neither if it can read; I guess your table is empty, thus always return 0 in the Catch.
VB Code:
Try
sqldr.Read()
iTickNum = sqldr(0) + 1 ' what if it is null ? Use IsDbNull()... and assign next logical value, in this case 1
Catch
iTickNum = 0
End Try
Edited: Oh and you should use a transaction if available for all these operations....
Good luck!
Thank you eL_NiNo for your response but I don't quite understand what you mean as this is new territory for me could you explain more possibly give an example? Thank you so much!
Chris Griffin: When I stick this army guy with the sharp bayonette up my nose, it tickles my brain. Hah hah hah ... ow. Oh, now I don't know math.
-
Oct 20th, 2005, 10:57 AM
#20
Addicted Member
Re: Determine IDnumber in dataset column
Ok, try this :
VB Code:
Try
sqldr.Read()
If not isdbnull( sqldr(0) ) Then
' A value exists, so increase by 1
iTickNum = sqldr(0) + 1
Else
' No value found in DB, so create one at 1
iTickNum = 1
End if
Catch
iTickNum = 0
End Try
-
Oct 20th, 2005, 12:04 PM
#21
Member
Re: Determine IDnumber in dataset column
Thank you so much! That did the trick. just for those of you who need the help also here is my code:
VB Code:
Dim cnsql As New SqlConnection(ConnectionString)
Dim cmSQLReadCom As New SqlCommand("SELECT MAX([Ticket Number]) FROM Errors", cnSQL)
cnSQL.Open()
Dim sqldr As SqlDataReader = cmSQLReadCom.ExecuteReader
Try
sqldr.Read()
If Not IsDBNull(sqldr(0)) Then
' A value exists, so increase by 1
iTickNum = sqldr(0) + 1
Else
' No value found in DB, so create one at 1
iTickNum = 1
End If
Catch
iTickNum = 0
End Try
sqldr.Close()
cnSQL.Close()
Chris Griffin: When I stick this army guy with the sharp bayonette up my nose, it tickles my brain. Hah hah hah ... ow. Oh, now I don't know math.
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
|