Results 1 to 21 of 21

Thread: Determine IDnumber in dataset column

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question 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

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    If MS Access is your database you're using , then let it handle this for you . No code needed , just set datatype as autonumber .

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    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

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    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

  6. #6
    Addicted Member
    Join Date
    Oct 2000
    Location
    Québec, Canada
    Posts
    212
    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 & ", ..., ...);"
    ...
    Regards,

    El-Nino

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    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

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Thanks for the help. How would I generate the guide for new insert?

    ljCharlie

  10. #10
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Guid.NewGuid().ToString()

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Thanks! And so everytime when I generate a new guide by using the code you gave me it will be unique, correct?

    ljCharlie

  12. #12
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  13. #13
    Lively Member
    Join Date
    Jul 2003
    Posts
    93
    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

  14. #14
    Addicted Member
    Join Date
    Oct 2000
    Location
    Québec, Canada
    Posts
    212
    It's a DataReader.

    Code:
    Dim objDR as OleDBDataReader
    or
    Dim objDR as ODBCDataReader
    or
    ...
    Regards,

    El-Nino

  15. #15
    Lively Member
    Join Date
    Jul 2003
    Posts
    93
    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

  16. #16
    Lively Member
    Join Date
    Jul 2003
    Posts
    93
    Anyone?

  17. #17
    Member pOfa's Avatar
    Join Date
    Sep 2005
    Location
    Portland, Oregon U.S.A.
    Posts
    55

    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:
    1. ' This sub is used to add a error report to the database
    2.             Dim cmSQL As SqlCommand
    3.             Dim strSQL As String
    4.             Dim ssql As String
    5.             Dim sqldr As SqlDataReader
    6.             Dim iTickNum As Integer
    7. Dim SQL_CONNECTION_STRING_TEST As String = "Server=myserver; DataBase=TESTPAERS; uid=****; pwd=****"
    8.  
    9.  
    10.             ssql = "SELECT MAX([Ticket Number]) FROM Errors;"
    11.  
    12.             Try
    13.                 sqldr.Read()
    14.                 iTickNum = sqldr(0) + 1
    15.             Catch
    16.                 iTickNum = 0
    17.             End Try
    18.  
    19.             Try
    20.  
    21.                 'Create the SQL Query and insert a row
    22.                 strSQL = "INSERT INTO Errors ([Ticket Number], Name, " & _
    23.                 "Program, Problem, Department, " & _
    24.                 "Frequency, People, Description, Date, Time) " & _
    25.                 "VALUES('" & iTickNum & "' , " & _
    26.                 "'" & cboName.Text & "' , " & _
    27.                 "'" & cboProg.Text & "' , " & _
    28.                 "'" & cboProb.Text & "' , " & _
    29.                 "'" & cboDept.Text & "' , " & _
    30.                 "'" & cboFreq.Text & "' , " & _
    31.                 "'" & radYes.Checked & "' , " & _
    32.                 "'" & txtDesc.Text & "' , " & _
    33.                 "'" & txtDate.Text & "' , " & _
    34.                 "'" & txtTime.Text & "')"
    35.  
    36.                 cnSQL = New SqlConnection(SQL_CONNECTION_STRING_TEST)
    37.                 cnSQL.Open()
    38.  
    39.                 cmSQL = New SqlCommand(strSQL, cnSQL)
    40.                 cmSQL.ExecuteNonQuery()
    41.  
    42.                 ' Close and Clean up objects
    43.                 cnSQL.Close()
    44.                 cmSQL.Dispose()
    45.                 cnSQL.Dispose()
    46.  
    47.             Catch Exp As SqlException
    48.                 MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
    49.  
    50.             Catch Exp As Exception
    51.                 MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
    52.             End Try
    53.  
    54.             'Show success messagebox, clear form and reset focus.
    55.             MsgBox("You have successfully submitted your " & _
    56.             "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.

  18. #18
    Addicted Member
    Join Date
    Oct 2000
    Location
    Québec, Canada
    Posts
    212

    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:
    1. Try
    2.                 sqldr.Read()
    3.                 iTickNum = sqldr(0) + 1 ' what if it is null ? Use IsDbNull()... and assign next logical value, in this case 1
    4.             Catch
    5.                 iTickNum = 0
    6.             End Try

    Edited: Oh and you should use a transaction if available for all these operations....

    Good luck!
    Regards,

    El-Nino

  19. #19
    Member pOfa's Avatar
    Join Date
    Sep 2005
    Location
    Portland, Oregon U.S.A.
    Posts
    55

    Re: Determine IDnumber in dataset column

    Quote 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:
    1. Try
    2.                 sqldr.Read()
    3.                 iTickNum = sqldr(0) + 1 ' what if it is null ? Use IsDbNull()... and assign next logical value, in this case 1
    4.             Catch
    5.                 iTickNum = 0
    6.             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.

  20. #20
    Addicted Member
    Join Date
    Oct 2000
    Location
    Québec, Canada
    Posts
    212

    Re: Determine IDnumber in dataset column

    Ok, try this :

    VB Code:
    1. Try
    2.     sqldr.Read()
    3.     If not isdbnull( sqldr(0) ) Then
    4.         ' A value exists, so increase by 1
    5.         iTickNum = sqldr(0) + 1
    6.     Else
    7.         ' No value found in DB, so create one at 1
    8.        iTickNum = 1
    9.     End if
    10.  
    11. Catch
    12.     iTickNum = 0
    13. End Try
    Regards,

    El-Nino

  21. #21
    Member pOfa's Avatar
    Join Date
    Sep 2005
    Location
    Portland, Oregon U.S.A.
    Posts
    55

    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:
    1. Dim cnsql As New SqlConnection(ConnectionString)
    2.             Dim cmSQLReadCom As New SqlCommand("SELECT MAX([Ticket Number]) FROM Errors", cnSQL)
    3.             cnSQL.Open()
    4.  
    5.             Dim sqldr As SqlDataReader = cmSQLReadCom.ExecuteReader
    6.  
    7.             Try
    8.                 sqldr.Read()
    9.                 If Not IsDBNull(sqldr(0)) Then
    10.                     ' A value exists, so increase by 1
    11.                     iTickNum = sqldr(0) + 1
    12.                 Else
    13.                     ' No value found in DB, so create one at 1
    14.                     iTickNum = 1
    15.                 End If
    16.  
    17.             Catch
    18.                 iTickNum = 0
    19.             End Try
    20.  
    21.             sqldr.Close()
    22.             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
  •  



Click Here to Expand Forum to Full Width