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
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)
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!
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:
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!
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
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()