This is for a helpdesk ticketing program I’m creating:
When the user clicks to start a ticket I want to be able to display an autonumber from mysql that corresponds with the current ticket being filled out, so that the user can track their ticket later on. I have everything set up but I’m not too sure how I can display the Ticket ID in a textbox.
Right now I have it so that the query is being ran to get the next Ticket Id number when the form loads, but I’m not sure how to display it as text in the textbox.
'QUERY FOR NEW ID TICKET NUMBER
Private Sub Suggestions_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim idQuery As String = "SELECT MAX(itID)+1 AS Expr1 FROM(TableName)"
With cmd
.Connection = conn
.CommandText = idQuery
.CommandType = CommandType.Text
End With
Try
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
Me.Close()
Catch ex As Exception
MsgBox(ex.ToString)
conn.Close()
End Try
Now I managed to change it to this but I’m still getting hung up cause I’m not sur eif it’s correct:
Dim conn As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand()
Dim Reader As MySqlDataReader
Dim submitteddate As String
'QUERY FOR NEW ID TICKET NUMBER
Private Sub Suggestions_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Try
Dim idQ As String = "INSERT INTO tblit (itID, ersBugIssue, ersSubject, ersBugCategory, ersBugDate, ersBugSubmittedby, ersBugOrHelp )" & _
"VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL);"
Dim cmd As new SQLCommand()
With cmd
.Connection = conn
.CommandText = idQ
End With
conn.Open()
cmd.ExecuteNonQuery()
cmd.CommandText = "SELECT LAST_INSERT_ID() as NewTicketID"
Dim Reader As SqlDataReader = cmd.ExecuteReader()
Reader.Read()
MsgBox(Reader("NewTicketID"))
Reader.Close()
conn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
"SELECT MAX(itID)+1 AS Expr1 FROM(TableName)"Is a bad idea, you will run into concurrency issues very quickly.
You should run the following SQL statements instead.
If you do it like that you will never run into issues with duplicate or wrong
TicketIDbeing issued.If you do a
select, you cannot say it’s a nonQuery.Use the following code