Waqas Iqbal Waqas Iqbal - 7 months ago 16
SQL Question

Query returs same data everytime it runs

Background:

Through these codes, I add different appeal numbers to the database. Each appeal number has an assignment code assigned to it through these codes.

Problem:

The problem is when I add more than 2 appeals simultaneously, this code only detects the assignment code assigned to 1st appeal added, but does not detect the next added assignment number.

I tried adding Thread.sleep method, but the result is same.

kindly help me solving this problem.

Try
' Find out last assignment code
Dim fillCommand As String
Dim FillList As New OleDb.OleDbDataAdapter
Dim dt As New DataTable
Dim rowdata As Integer

Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ComplianceDB.accdb;Jet OLEDB:Database Password=xxxxxxx;" 'D:\Important Programing links\Programming Learning\Compliance Diary\ComlianceDiary\ComlianceDiary\database\ComplianceDB.accdb"
Dim Conn As New OleDb.OleDbConnection(ConnString)
Conn.Open()

If ListView1.Items.Count = 0 Then
MessageBox.Show("Please enter valid information to add appeal.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
ElseIf BenchComboBox.Text = "" Or NoticeLocationTextBox.Text = "" Or BenchMembersTextBox.Text = "" Then
MessageBox.Show("Please enter all the required information to add appeal.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)

Else

For i = 0 To ListView1.Items.Count - 1
'Add Appeals to ATIR list
Me.ATIRTableAdapter.Insert(ClientComboBox.Text, ListView1.Items(i).SubItems.Item(1).Text, ListView1.Items(i).Text, ListView1.Items(i).SubItems.Item(2).Text, BenchComboBox.Text, BenchMembersTextBox.Text, HearingDate.Value.Date, ListView1.Items(i).SubItems.Item(4).Text, mgrComboBox.Text, txtdescription.Text, ListView1.Items(i).SubItems.Item(3).Text, NoticeLocationTextBox.Text, ChangePassword.GUIDTextBox.Text, DateAndTime.Today)

Dim AssignmentCommand = New OleDbCommand("select AssignmentCode, AssignmentName from Assignment where AssignmentName = 'ATIR Appeal - " & ListView1.Items(i).Text & "' AND ClientName = '" & ClientComboBox.Text & "'", Conn) ' (select max(AssignmentCode) from Assignment)" 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15)
Dim Reader As OleDbDataReader = AssignmentCommand.ExecuteReader
'Check if the assignment name already exists, if yes, fetch old assignment code and add it to new compliance
If Reader.Read Then
Dim ATIRCodeCmd = New OleDbCommand("select ID from ATIR where ID = (select max(ID) from ATIR)", Conn) 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15)
Dim ReaderATIRCode As OleDbDataReader = ATIRCodeCmd.ExecuteReader
While ReaderATIRCode.Read()
Me.NoticeComplianceTableAdapter1.Insert(ClientComboBox.Text, Reader.Item(1).ToString, Today.Date, Today.Date, Reader.Item(1).ToString, NoticeLocationTextBox.Text, txtdescription.Text, Reader.Item(0).ToString, "No", "", "", "Pending", ListView1.Items(i).SubItems.Item(1).Text, "", "", "", "Fresh Notice", "", "", ChangePassword.GUIDTextBox.Text, DateAndTime.Today, mgrComboBox.Text, PartnerNameTextBox.Text, SrManagerNameTextBox.Text, "AppellateCompliance", HearingDate.Value.Date, "", "", "", "", "", ReaderATIRCode.Item(0).ToString, "ATIR")
End While
Else

'if assignment does not exists, then create a new one and give new assignment number to new compliance.
Me.AssignmentTableAdapter1.Insert(ListView1.Items(i).SubItems.Item(1).Text, ClientComboBox.Text, "ATIR Appeal - " & ListView1.Items(i).Text, "N/A", "Pending", "No", Today.Date, HearingDate.Value.Date, HearingDate.Value.Date, mgrComboBox.Text, SrManagerNameTextBox.Text, PartnerNameTextBox.Text, "Income Tax Ordinance, 2001", "Pending", "", "Pending", "", "Pending", "", Today.Date, "", Today.Date, "Pending", Today.Date, "")
Dim AssignmentCodeCmd = New OleDbCommand("select AssignmentCode from Assignment where AssignmentCode = (select max(AssignmentCode) from Assignment)", Conn) 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15)
Dim ReaderCode As OleDbDataReader = AssignmentCodeCmd.ExecuteReader
While ReaderCode.Read()
rowdata = ReaderCode.Item(0).ToString
ReaderCode.NextResult()
End While

Dim ATIRCodeCmd = New OleDbCommand("select ID from ATIR where ID = (select max(ID) from ATIR)", Conn) 'SELECT * FROM Assignment Where AssignmentName = '" & AssignmentNameTextBox.Text & "' AND ClientName = '" & ClientNameComboBox.Text & "'" ' WHERE DateofHearing BETWEEN " & Today.Date & " AND " & Today.Date.AddDays(15)
'Thread.Sleep(1000)
Dim ReaderATIRCode As OleDbDataReader = ATIRCodeCmd.ExecuteReader
While ReaderATIRCode.Read()
Me.NoticeComplianceTableAdapter1.Insert(ClientComboBox.Text, "ATIR Appeal - " & ListView1.Items(i).Text, Today.Date, Today.Date, "ATIR Appeal - " & ListView1.Items(i).Text, NoticeLocationTextBox.Text, txtdescription.Text, rowdata, "No", "", "", "Pending", ListView1.Items(i).SubItems.Item(1).Text, "", "", "", "Fresh Notice", "", "", ChangePassword.GUIDTextBox.Text, DateAndTime.Today, mgrComboBox.Text, PartnerNameTextBox.Text, SrManagerNameTextBox.Text, "AppellateCompliance", HearingDate.Value.Date, "", "", "", "", "", ReaderATIRCode.Item(0).ToString, "ATIR")
End While

End If
Reader.Close()
Next

MessageBox.Show("ATIR Appeal(s) have been noted.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
Conn.Close()

Me.Close()
End If

Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try

Answer

Never mind. I resolved my problem by using @@IDENTITY sql command. Thank you all for your contributions.

Comments