Datagridview - prevent duplicates in comboboxcell - vb.net

I have a ComboboxCell in first cell of Datagridview. This combobox has Datasource from my DB. When I select Item from It, all other cells in a row gets populated by a record from my DB. Now I want to prevent duplicate row entries, based on Comboboxcell selected item. After that I want to clear that Comboboxcell, and keep code running. I managed to do almost everything, but problem is that after msgbox is displayed, code stops working - so when I select ComboboxCell Item again, nothing happens, even in a new row. Here is my whole code:
Private Sub My_DGV_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles My_DGV.CellValueChanged
If My_DGV.Columns(e.ColumnIndex).Name = "Column1" Then
'Prevent duplicates
For i As Integer = 0 To My_DGV.RowCount - 2
For j As Integer = i + 1 To My_DGV.RowCount - 2
If My_DGV.Rows(i).Cells(0).Value = My_DGV.Rows(j).Cells(0).Value Then
MsgBox("You allready selected this item. Duplicates are not allowed.", MsgBoxStyle.Information, "Warning")
My_DGV.Rows(j).Cells(0).Value = " "
Dim cbx As ComboBox = DGV_APO.EditingControl
cbx.SelectedIndex = -1
Exit Sub
End If
Next
Next
OracleconnOpen()
Using cmd As New OracleCommand()
Dim SQL As String = "Select NAMES,SURNAMES,STATE FROM My_Table"
Dim Concat_SQL As String = " Where "
SQL = String.Concat(SQL, Concat_SQL, " ID_NUMBER = :id")
cmd.Parameters.Add(New OracleParameter("id", My_DGV.CurrentRow.Cells(0).Value))
cmd.Connection = OracleconnOpen()
cmd.CommandText = SQL
cmd.CommandType = CommandType.Text
Dim dr As OracleDataReader = cmd.ExecuteReader()
Dim dt As New DataTable
dt.Load(dr)
My_DGV.CurrentRow.Cells(1).Value = dt.Rows(0)("NAMES").ToString()
My_DGV.CurrentRow.Cells(2).Value = dt.Rows(0)("SURNAMES").ToString()
My_DGV.CurrentRow.Cells(3).Value = dt.Rows(0)("STATE").ToString()
End Using
OracleconnClose()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'SELECT from my DB
Dim SQL As String = "SELECT ID_NUMBER from My_Table"
Dim dtb As New DataTable()
Try
OracleconnOpen() 'Open my connection
Using dad As New OracleDataAdapter(SQL, OracleconnOpen)
dad.Fill(dtb)
End Using
Column1.DisplayMember = "ID_NUMBER"
Column1.DataSource = dtb
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
OracleconnClose() 'Close my connection
End Try
End Sub
Private Sub My_DGV_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles My_DGV.CurrentCellDirtyStateChanged
If My_DGV.IsCurrentCellDirty Then My_DGV.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub
Private Sub My_DGV_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DGV_APO.DataError
e.ThrowException = False
End Sub
So, what am I doing wrong ? Any help appreciated.

You have an Exit Sub call within your loop that will always execute so nothing after the exit sub will ever be run when you change this value. If you want to exit the loop and continue running the code after a duplicate is found, that should be done within the If statement. This code should stop working after the first row is compared even if it is not a match.

Related

VB.net/MS Access Monthly Donations System Assistance

I'm doing a project for my Database Management subject. I cannot figure out how to add an amount to a previously added amount. For now, I'm only able to update the amount. Here's the code. I'm sorry if I cannot explain it well.
I have 2 forms. My first form allows me to enter a last name and retrieve the data to my list view.
My second form lets me retrieve the data I entered in my first form and it will show up on a separate list view with a "Last Name | Amount" tab.
I have two textboxes. One for last name set to readonly to disable editing, and another for the amount I want to enter.
After entering an amount, let's say 20, it will update on the listview and my database as 20.
The problem is that when I enter a new amount for the same last name, let's say 30, the 30 will replace the 20 but it should be 50 because 20+30 = 50.
I understand the logic and I have tried adding another textbox for addition but I simply do not know the codes for it.
Imports System.Data.OleDb
Public Class Form2
Dim conString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Israel De Leon\Documents\testing.accdb;"
Dim con As OleDbConnection = New OleDbConnection(conString) 'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database2.accdb
Dim cmd As OleDbCommand
Dim adapter As OleDbDataAdapter
Dim dt As DataTable = New DataTable()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'SET LISTVIEW PROPERTIES
ListView1.View = View.Details
ListView1.FullRowSelect = True
'Construct Columns
ListView1.Columns.Add("Last Name", 100)
ListView1.Columns.Add("Amount", 100)
End Sub
Private Sub UpdateLV(lname As String)
'Updates last name and amount entered into the database
Dim sql As String = "UPDATE Table1 SET LastName='" + TextBox1.Text + "',Amount='" + TextBox2.Text + "' WHERE LastName='" + lname + "'"
cmd = New OleDbCommand(sql, con)
'OPEN CON, EXECUTE, UPDATE, CLOSE
Try
con.Open()
adapter = New OleDbDataAdapter(cmd)
adapter.UpdateCommand = con.CreateCommand()
adapter.UpdateCommand.CommandText = sql
If (adapter.UpdateCommand.ExecuteNonQuery() > 0) Then
MsgBox("Successfully Updated")
End If
con.Close()
Retrieve()
ClearBox()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
End Sub
Private Sub Retrieve()
ListView1.Items.Clear()
'SQL STM
Dim sql As String = "SELECT * FROM Table1 "
cmd = New OleDbCommand(sql, con)
'OPEN CON, RETRIEVE, FILL LISTVIEW
Try
con.Open()
adapter = New OleDbDataAdapter(cmd)
adapter.Fill(dt)
'LOOP THROUGH DT
For Each row In dt.Rows
Populate(row(0), row(1)) 'Index of database row
Next
'CLEAR DATATABLE
dt.Rows.Clear()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
End Sub
Private Sub Populate(lname As String, aamount As String)
'ROW ARRAY
Dim row As String() = New String() {lname, aamount}
Dim item As ListViewItem = New ListViewItem(row)
'ADD TO ROWS COLLECTION
ListView1.Items.Add(item)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Retrieve()
End Sub
Private Sub ListView1_MouseClick(sender As Object, e As MouseEventArgs) Handles ListView1.MouseClick
Dim llname As String = ListView1.SelectedItems(0).SubItems(0).Text
Dim amounts As String = ListView1.SelectedItems(0).SubItems(1).Text
TextBox1.Text = llname
TextBox2.Text = amounts
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim amounts As String = ListView1.SelectedItems(0).SubItems(0).Text
UpdateLV(amounts)
End Sub
Private Sub ClearBox()
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
End Class
Mathematical operation should not be done using strings. This is a real basic principle that many VB.NET programmers don't think enough thanks to the forgiveness allowed by Option Strict Off in the VB.NET project settings.
If you are just starting a new project in VB.NET don't use this setting but switch it ASAP to On. This will give you an halt when you try to use strings as they were numbers and force you to do the appropriate conversion and checking on the values provided.
So your code that updates the amount rewritten
Private Sub UpdateLV(lname As String)
' Get the amount as a number (decimal for currency is the best)
Dim addAmt As Decimal
if Not decimal.TryParse(textbox2.Text, addAmt) Then
MessageBox.Show("Insert a valid amount please")
return
End If
' Sanity check
if addAmt <= 0 Then
MessageBox.Show("Amount should be > 0")
return
End If
'Updates last name and amount entered into the database
Dim sql As String = "UPDATE Table1 SET LastName=#name
,Amount=Amount+#amt
WHERE LastName=#oldname"
cmd = New OleDbCommand(sql, con)
Try
con.Open()
' Using an adapter here is wrong. You use directly the command
cmd.Parameters.Add("#name", OleDbType.VarWChar).Value = textBox1.Text
cmd.Parameters.Add("#amt", OleDbType.Decimal).Value = addAmt
cmd.Parameters.Add("#oldname", OleDbType.VarWChar).Value = lName
If (cmd.ExecuteNonQuery() > 0) Then
MsgBox("Successfully Updated")
End If
con.Close()
Retrieve()
ClearBox()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
End Sub
Something else is not clear in your code. What is the purpose of changing also the LastName here? Finally do not keep a global connection object. Instead create it when you need it and destroy it afterward with Using statement. It will be better for your memory footprint and for your database

Check if the row checked in DataGridview exist in database

I have a DatagridView with a Checkboxcolumn My Goal here is to check if the Checked Row is already exist in the Database but my code does not give me the output I want. Whats wrong?
Here it is
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As MySqlConnection = New MySqlConnection("datasource=localhost;database=operations;userid=root;password=admin1950;Convert Zero Datetime=True")
conn.Open()
Dim comm As MySqlCommand = New MySqlCommand()
comm.Connection = conn
Dim name As String
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1 Step 1
If Me.DataGridView1.Rows(i).Cells(0).Value = True Then
name = Me.DataGridView1.Rows(i).Cells(1).Value
comm.CommandText = "select ElecAssigned,ScheduleDate from assignments where ElecAssigned = '" & name & "' and ScheduleDate = #ScheduleDate"
comm.Parameters.AddWithValue("#ScheduleDate", DateTimePicker1.Value)
comm.ExecuteNonQuery()
Dim reader As MySqlDataReader
reader = comm.ExecuteReader
If reader.HasRows Then
MsgBox("The persons that you Selected is also Scheduled Today.")
End If
End If
Next
End Sub
Here is the Scenario.
I save a Data in the Table assignments and it looks like this.
and here is the Scenario in the Program
I checked a row in the Datagridview and select a Date in DateTimePicker which is the same in the my table there must be Message that will show saying The selected Person is also Scheduled Today
here is the code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connection As New MySqlConnection("datasource=localhost;database=operations;userid=root;password=admin1950;Convert Zero Datetime=True")
Dim command As New MySqlCommand("SELECT COUNT(*) FROM Assignments WHERE ElecAssigned = #ElecAssigned AND ScheduleDate = #ScheduleDate", connection)
Dim parameter = command.Parameters.Add("#ElecAssigned", MySqlDbType.VarChar, 50)
command.Parameters.AddWithValue("#ScheduleDate", DateTimePicker1.Value)
connection.Open()
For Each row As DataGridViewRow In Me.DataGridView1.Rows
If CBool(row.Cells(0).Value) Then
parameter.Value = CStr(row.Cells(1).Value)
If CInt(command.ExecuteScalar()) > 0 Then
'Match found.
Else
MsgBox("The Personnel(s) you Selected is also Scheduled Today")
'No match found.
'Save It
End If
End If
Next
End Sub
Regardless of how many I select the program will check if the persons selected is also checked.
E.g.
Dim connection As New MySqlConnection("connection string here")
Dim command As New MySqlCommand("SELECT COUNT(*) FROM Assignments WHERE ElecAssigned = #ElecAssigned AND ScheduledDate = #ScheduledDate", connection)
Dim parameter = command.Parameters.Add("#ElecAssigned", MySqlDbType.VarChar, 50)
command.Parameters.AddWithValue("#ScheduledDate", DateTimePicker1.Value.Date) 'Add a word Date to check only the Date
connection.Open()
For Each row As DataGridViewRow In Me.DataGridView1.Rows
If CBool(row.Cell(0).Value) Then
parameter.Value = CStr(row.Cells(1).Value)
If CInt(command.ExecuteScalar()) > 0 Then
'Match found.
Else
'No match found.
End If
End If
Next
You might require a few small adjustments but that's basically the way to go about it if you want to know individually for each checked row.

Removing a row from a datatable

I have a program that asks the user a series of questions that are collected from a database and stored in a datatable.
I have a system that chooses two random numbers, one to determine which question and one to determine what order the answers are displayed. I want questions to remain in the datatable until the user gets it correct, and afterwards that question cannot come up again.
My datatable is called DT, and there's a line of code:
DT.Rows.RemoveAt(QNumber)
Which sounds like it should remove the row selected. However I have a question regarding this.
If for example QNumber was 2, and so row 2 was deleted. Would this then move everything from row 3 in to row 2, and then everything from row 4 to row 3 and so on, or would this just make row 2 blank, and so break my code?
Since you didn't post any context are some test snippets:
Button1_Click loads a table
Button2_Click "processes" each row and optionally deletes the row.
In your case "process" would mean: display the question and loop until you get an answer.
In your case Button2 code would determine which record to load, probably using .Select() on the datatable that returns the "question" to ask, and eventually delete.
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Dim con As New System.Data.SqlClient.SqlConnection()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Using con As New System.Data.SqlClient.SqlConnection()
con.ConnectionString = "Data Source=APCD03;Initial Catalog=OIStest;Integrated Security=True"
Dim rdr As Data.SqlClient.SqlDataReader
con.Open()
Dim cmd As New SqlCommand("SELECT [DBLinked] as PK ,[TEBackupDate] FROM [OISTest].[dbo].[_DBLink]", con)
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dt = New DataTable
dt.Load(rdr)
rdr.Close()
End Using
Catch ex As Exception
MsgBox(ex, ex.Message)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each row As DataRow In dt.Rows
If Not row.RowState = DataRowState.Deleted Then
If MsgBox("Delete this row, PK: " & row("PK"), vbYesNo) = MsgBoxResult.Yes Then
row.Delete()
End If
End If
Next
End Sub
Button3 demos selecting a specific row to process and delete:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim rows() As DataRow = dt.Select("PK='test row'")
If rows.Length = 0 Then
MsgBox("row was deleted already")
Else
MsgBox(rows(0)(0)) ' display PK
If MsgBox("Delete " & rows(0)(0), vbYesNo) = MsgBoxResult.Yes Then
rows(0).Delete()
End If
End If
End Sub
Note that rows(0) is the first record of the result from the select - not the record index in the overall table.

Runtime datatable update error

I have a datagridview with a checkbox column. When I click(check/Uncheck) on checkbox field as random, two other fields in corrensponding row should be added OR removed in a datatable (declared runtime).
So that I can do some procedures with data in the datatable.
For that I have declared a datatable as global.
Now the problem is, each time when I click on a checkbox, a simple mouse scrolling is required to update datatable, OR a click needed in the new datagridview which is showing values in the datatable.
My code given below,
global declaration: Public PaymentTable As DataTable
Private Sub ShowOrdersFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataBind()
Me.DGVOrders.RowsDefaultCellStyle.BackColor = Color.GhostWhite
Me.DGVOrders.AlternatingRowsDefaultCellStyle.BackColor = Color.PaleGoldenrod
PaymentTable = New DataTable()
PaymentTable.Columns.Add("RowId", GetType(Integer))
PaymentTable.Columns.Add("Amount", GetType(Decimal))
End Sub
Private Sub DataBind()
DGVOrders.DataSource = Nothing
DGVOrders.Columns.Clear()
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim dt As New DataTable
con.Open()
With cmd
.Connection = con
.CommandText = "select * from VIEW_PAYMENTS_DUES_BYORDER where CustCode='" & CustCode & "' order by OrderNo DESC"
End With
da.SelectCommand = cmd
da.Fill(dt)
BindingSource1.DataSource = dt
DGVOrders.DataSource = BindingSource1
DGVOrders.ClearSelection()
con.Close()
DGVOrders.Columns(0).Visible = False
DGVOrders.Columns(1).HeaderText = "Order No"
DGVOrders.Columns(2).HeaderText = "Cust Code"
DGVOrders.Columns(3).HeaderText = "Name"
DGVOrders.Columns(4).HeaderText = "Order Date"
DGVOrders.Columns(5).HeaderText = "Order Price"
DGVOrders.Columns(6).HeaderText = "Total Payment"
DGVOrders.Columns(7).HeaderText = "Dues"
For i = 0 To DGVOrders.RowCount - 1
If DGVOrders.Rows(i).Cells(7).Value > 0 Then
DGVOrders.Rows(i).Cells(7).Style.ForeColor = System.Drawing.Color.Red
Else
DGVOrders.Rows(i).Cells(7).Style.ForeColor = System.Drawing.Color.Green
End If
Next
' CHECK BOX
Dim colmnchk As New DataGridViewCheckBoxColumn
colmnchk.DataPropertyName = "chkSelect"
colmnchk.HeaderText = "SELECT"
colmnchk.Name = "chkSelect"
DGVOrders.Columns.Add(colmnchk)
For i = 0 To DGVOrders.RowCount - 1
Next
'CHECK BOX END
End Sub
Private Sub DGVOrders_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVOrders.CellValueChanged
If DGVOrders.Columns(e.ColumnIndex).Name = "chkSelect" Then
Dim checkCell As DataGridViewCheckBoxCell = _
CType(DGVOrders.Rows(e.RowIndex).Cells("chkSelect"), _
DataGridViewCheckBoxCell)
If checkCell.Value = True Then
PaymentTable.Rows.Add(DGVOrders.Rows(e.RowIndex).Cells(0).Value, DGVOrders.Rows(e.RowIndex).Cells(7).Value)
Else
Dim toRemoveID As Integer = DGVOrders.Rows(e.RowIndex).Cells(0).Value
For i = 0 To PaymentTable.Rows.Count - 1
If PaymentTable.Rows(i).Item(0) = toRemoveID Then
PaymentTable.Rows(i).Delete()
Exit Sub
End If
Next
End If
DataGridView1.DataSource = PaymentTable
End If
End Sub
Can sombody to solve the issue, or is there any other good method if my code is wrong ?
Try using a different event like CellContentClick and testing for the ColumnIndex. The drawback with this is the event will fire whenever you click on any cell.
Private Sub DGVOrders_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGVOrders.CellContentClick
'Only interested in the CheckBox column
If e.ColumnIndex = colmnchk.Index Then
Debug.WriteLine("In DGVOrders_CellContentClick for the checkbox")
End If
End Sub

How do I read contents of a DataTable and assign them to other controls?

Good morning,
I've been up all night trying to figure this out on my own without bugging anybody else, but I can't.
I've been successful in querying my MySQL database and gotten a set of records into a DataTable (dbTable). During debugging, I can see its contents so I know the data is there. Initially, the DataTable is used to populate a ListView control I have on my form.
When I select a record, I want the contents of the DataTable (or the query I just ran) to be assigned to some TextBox controls. I can't seem to figure out how to do this. Any help would be greatly appreciated.
UPDATE TO ADD IMAGES:
I'm hoping these screenshots will give an idea of what I'm looking to do. The first image shows what happens after an account number has been entered. The second box shows a Groupbox expanded to reveal the form fields after a record has been selected in the ListView.
The control names are: TextBoxCustomer, TextBoxLastName, TextBoxFirstName, ComboBoxSalutation, ComboBoxCardType, TextBoxCard.Text, TextBoxExpireMonth, TextBoxExpireYear, TextBoxCVV2.
The field names in the DataTable (dbTable) are: nameCOMPANY, nameLAST, nameFIRST, nameSALUTATION, ccType, ccNumber, ccExpireMonth, ccExpireYear, ccCode.
IMAGE 1:
IMAGE 2:
Have you tried this?
TextBox1.Text = dbTable.Rows(0)("ColumnName").ToString()
TextBox2.Text = dbTable.Rows(1)("OtherColumnName").ToString()
You can also do this:
Dim row as DataRow = dbTable.Rows(0)
TextBox1.Text = row("ColumnName").ToString()
row = dbTable.Rows(1)
TextBox2.Text = row("OtherColumnName").ToString()
You could also DataBind to a DataGrid (or similar control) using dbTable as the DataSource and then set the DataGrid.EditMode to True. This would create the textbox controls for you.
UPDATE:
Try something like this to bind your textboxes to the selected values of your ListView:
Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Dim item As ListViewItem = Nothing
Dim tb As TextBox = Nothing
Dim i As Integer = 0
For Each item In ListView1.SelectedItems
tb = Me.Controls.Find("TextBox" & i.ToString, True)(0)
If tb IsNot Nothing Then
tb.Text = item.Text
End If
i += 1
Next
End Sub
UPDATE:
This is a little more error-proof, but this routine will only work if your textboxes are named TextBox1, TextBox2, TextBox3, etc.:
Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Dim item As ListViewItem = Nothing
Dim found() As Control = Nothing
Dim tb As TextBox = Nothing
Dim i As Integer = 0
For Each item In ListView1.SelectedItems
found = Me.Controls.Find("TextBox" & i.ToString, True)
If found.Length > 0 Then
tb = TryCast(found(0), TextBox)
Else
tb = Nothing
End If
If tb IsNot Nothing Then
tb.Text = item.Text
End If
i += 1
Next
End Sub
UPDATE:
Okay, thanks to the screenshots, I am assuming that your ListView.MultiSelect = False, so only one item can be selected at a time. Given that, the following should work as long as the textboxes and ListView columns are named correctly:
Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Dim item As ListViewItem = Nothing
If ListView1.SelectedItems.Count = 1 Then
item = ListView1.SelectedItems(0)
txtCardNumber.Text = item.SubItems("CARD NUMBER")
txtCardExpirationMonth.Text = item.SubItems("EXP MO")
txtCardExpirationYear.Text = item.SubItems("EXP YEAR")
End If
End Sub
Hello guys/gals,
With tremendous assistance from Pete, I was able to modify the suggested answer and I achieved the desired solution. To prevent the horizontal scrollbars from displaying, I didn't add columns to the Listview (from the Designer). Instead, I added the fields to the Listview programatically - this way they were available for selection.
The main trouble I ran into was figuring out the Index numbers of the fields. I had to debug several times to figure out what the numbers were - so if anybody knows of a better way please do share.
Here're the two codes I used (thanks Pete):
Private Sub loadCard()
Try
'FOR MySQL DATABASE USE
Dim dbQuery As String = ""
Dim dbCmd As New MySqlCommand
Dim dbAdapter As New MySqlDataAdapter
Dim dbTable As New DataTable
Dim i As Integer
If dbConn.State = ConnectionState.Closed Then
dbConn.ConnectionString = String.Format("Server={0};Port={1};Uid={2};Password={3};Database=accounting", FormLogin.ComboBoxServerIP.SelectedItem, My.Settings.DB_Port, My.Settings.DB_UserID, My.Settings.DB_Password)
dbConn.Open()
End If
dbQuery = "SELECT *" & _
"FROM cc_master INNER JOIN customer ON customer.accountNumber = cc_master.customer_accountNumber " & _
"WHERE customer.accountNumber = '" & TextBoxAccount.Text & "'"
With dbCmd
.CommandText = dbQuery
.Connection = dbConn
End With
With dbAdapter
.SelectCommand = dbCmd
.Fill(dbTable)
End With
ListViewCard.Items.Clear()
For i = 0 To dbTable.Rows.Count - 1
With ListViewCard
.Items.Add(dbTable.Rows(i)("ccID"))
With .Items(.Items.Count - 1).SubItems
.Add(dbTable.Rows(i)("ccNumber"))
.Add(dbTable.Rows(i)("ccExpireMonth"))
.Add(dbTable.Rows(i)("ccExpireYear"))
.Add(dbTable.Rows(i)("ccCode"))
.Add(dbTable.Rows(i)("ccType"))
.Add(dbTable.Rows(i)("ccAuthorizedUseStart"))
.Add(dbTable.Rows(i)("ccAuthorizedUseEnd"))
.Add(dbTable.Rows(i)("nameCOMPANY"))
.Add(dbTable.Rows(i)("nameSALUTATION"))
.Add(dbTable.Rows(i)("nameLAST"))
.Add(dbTable.Rows(i)("nameFIRST"))
End With
End With
Next
If dbTable.Rows.Count = 0 Then
LabelNoCard.Visible = True
LabelNoCard.Focus()
TextBoxAccount.Focus()
Me.Refresh()
Else
If dbTable.Rows.Count > 1 Then
LabelNoCard.Visible = False
LabelMultipleCards.Visible = True
ListViewCard.Visible = True
Me.Refresh()
End If
End If
Catch ex As MySqlException
MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
End Try
dbConn.Close()
End Sub
Here's the second one:
Private Sub ListViewCard_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListViewCard.SelectedIndexChanged
GroupBox2.Visible = True
Dim item As ListViewItem = Nothing
If ListViewCard.SelectedItems.Count = 1 Then
item = ListViewCard.SelectedItems(0)
TextBoxCustomer.Text = item.SubItems(8).Text
TextBoxLastName.Text = item.SubItems(10).Text
TextBoxFirstName.Text = item.SubItems(11).Text
ComboBoxSalutation.Text = item.SubItems(9).Text
ComboBoxCardType.Text = item.SubItems(5).Text
TextBoxCard.Text = item.SubItems(1).Text
TextBoxExpireMonth.Text = item.SubItems(2).Text
TextBoxExpireYear.Text = item.SubItems(3).Text
TextBoxCVV2.Text = item.SubItems(4).Text
DateTimePickerStartDate.Text = item.SubItems(6).Text
DateTimePickerEndDate.Text = item.SubItems(7).Text
End If
End Sub