I have a problem with my program, I need to INCREMENT the value of a certain Label
Dim p1num As Integer = 0
M.Text = Format(Now, "MM")
Y.Text = Format(Now, "yy")
txtPNumber.Text = Y.Text.ToString & M.Text.ToString & p1num.ToString("D4")
I use this to generate the Patient ID, where in the M is for Month and the Y is for Year + the p1num to create a 4 digit number…. so the output will be like this:
(Let use the current date and year)
13020001
13020002
13020003
13020004
and so on….
After this, I will use these codes to insert it to my DB in SQL SERVER :
Dim SQLcon As New SqlConnection
Dim SQLdr As SqlDataReader
Try
SQLcon.ConnectionString = "Data Source=####;" & _
"Initial Catalog=####;Persist Security Info=True;User ID=####;Password="
Dim SQLcmd As New SqlCommand("INSERT INTO dbo.Patients" & _
"(pIDNo)" & _
"VALUES(@pIDNo)", SQLcon)
SQLcmd.Parameters.AddWithValue("@pIDNo", txtPNumber.Text)
MsgBox("Patient Added!", MsgBoxStyle.Information)
SQLdr = SQLcmd.ExecuteReader()
Catch ex As Exception
MessageBox.Show("Error Occured, Can't Add Patient!" & ex.Message)
Finally
SQLcon.Close()
End Try
Return ""
the problem now is that when I close the program the value that generates to my TextBox is start always in 13020001. I want to retain the value that recently added and then increment it to +1 so I plan to display the current ID that just added to my DB using these :
Dim querystring As String = "SELECT MAX(pIDNo) FROM dbo.Patients"
Using connection As New SqlConnection("Data Source=####;" & _
"Initial Catalog=####;Persist Security Info=True;User ID=####;Password=")
Dim command As New SqlCommand(querystring, connection)
connection.Open()
Dim reader As SqlDataReader = command.ExecuteReader
While reader.Read
txtCurrentID.Text = reader.GetString(0)
End While
reader.Close()
I manage to display the current ID but the problem now is I can’t increment it by 1.
Other problem : I try to copy the displayed value and just add it by one but another problem came up, the month and year doesn’t change. If I reached the month ‘MARCH’ the value that generates is still
1302 + pnum1.text(“D4”) that supposed to be 1303 + pnum1.text(“D4”)
1302 is for FEBRUARY
1303 is for MARCH
could anyone have a solution with my problem?
if you just want the date: then use
Date.TodayIntelliSense is your first book so must use it. 😉