Question : Code retriving duplicated numbers.

I need to retrieve a an lngvisitid, lngvoucher and get the max strvoucher.  Per the code below.  

It works just find unless two people click the button at the same time then they both get the same strvoucher number.  It also takes a long time for the code to process.  Can some one please tell me how I can speed it up and also make is so the users do not get the same strvoucher number.  strvoucher is not a auto increment.

Thank you
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
Private Sub assignnumber()

        If Label22.Text = "" Then

            newrecord()
            assignvonum()
        Else
            ' MsgBox("You have already assigned a number!")
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        assignbutton = True
        assignnumber()
    End Sub
    Private Sub assignvonum()
        If visitid = "" Then
            newvisit = True
            newrecord()
        End If
        Connection1.Close()
        Connection1.Open()
        Dim currentdate As Date
        currentdate = Now
        Dim cmdupdatevovisit As New SqlClient.SqlCommand("insert into tblPAVoucher  (lngvisitid,  strcreated, dtmcreated ) values ('" & visitid & "','" & PAAss & "','" & currentdate & "') ", Connection1)
        Try
            cmdupdatevovisit.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Dim cmdvoucherid As New Data.SqlClient.SqlCommand("Select max(lngvoucher) from tblPAVoucher where lngvisitid = '" & visitid & "'", Connection1)
        Try
            voucherid = cmdvoucherid.ExecuteScalar.ToString
            '      trans1.Commit()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


        'Get Next Voucher Number from tblPAVoucher using auto numbering
         Dim cmd2 As New Data.SqlClient.SqlCommand("Select max(strVoucher) from tblPAVoucher", Connection1)

        Try
            vonumber = cmd2.ExecuteScalar.ToString

        Catch ex As Exception
            MsgBox(ex.Message)


        End Try



        vonumber = "0" & vonumber + 1
        Label22.Text = vonumber

       

        Dim cmdupdatevonum As New SqlClient.SqlCommand("update tblPAVoucher  set strvoucher = '" & vonumber & "' where lngvoucher = '" & voucherid & "'", Connection1)
        Try
            cmdupdatevonum.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        updatevo()






    End Sub


   Private Sub newrecord()



        'If comboTest.Text = " " Then
        '  newrecord()
        'insert into newvisit

        '  If nextrec = False Then
        Connection1.Close()
        Connection1.Open()
        If newvisit = True Then
            'If comboTest.Text = " " Then
            Connection1.Close()
            Connection1.Open()
            If visitid = "" Then
                Dim trans As SqlTransaction = Connection1.BeginTransaction(IsolationLevel.Serializable)
                Dim cmdupdatevisit As New SqlClient.SqlCommand("insert into tblPAVisit ( lngPatientid,dtmcontact,bytvisit,memvisit,bytPartD) values ('" & lngpatientid & "',   '" & DateTimePicker1.Text & "',   '" & visittype & "',   '" & TextBox1.Text.Replace("'", "''") & "',   '" & partd & "')", Connection1, trans)

                Try
                    cmdupdatevisit.ExecuteNonQuery()
                Catch ex As Exception
                    'MsgBox(ex.Message)
                End Try

                'Get Next Voucher Number from tblPAVoucher
                Dim cmd2 As New Data.SqlClient.SqlCommand("Select max(lngvisitid) from tblPAVisit with (TABLOCK)", Connection1, trans)

                Try
                    visitid = cmd2.ExecuteScalar.ToString
                    SqlParametersDB.Name.visitid = visitid
                    trans.Commit()
                Catch ex As Exception
                    'MsgBox(ex.Message)
                    trans.Rollback()
                End Try

                visitidscroll = visitid
            End If

            newvisit = False
            refreshscroll()
        End If


    End Sub

Answer : Code retriving duplicated numbers.

You can get a free HTML to PDF tool at http://www.digitaljunkies.ca/dompdf/. All the information on installation and configuration are there as well. I've used it before without any problems.
Random Solutions  
 
programming4us programming4us