I am getting an error at this line:
CheckedListBox3.Items.Remove(CheckedListBox3.CheckedItems(0))
I don't understand why because it does read the first two but not the third... the index of my checkedlistbox1, 2 and 3 is 3 so that makes it (0), (1), and (2) in each while...
Please help me out and tell me where i am wrong because i don't understand how it's possible that he can find the index of the first and second listbox but can't find the index of the third listbox
This is my code:
cmd = New SqlCommand("SELECT serienaam, ouderdom, aantal FROM klantserie Where klantnummer = '" & klantnummer & "' ", con)
Dim ttr As SqlDataReader = cmd.ExecuteReader()
While ttr.Read = True
CheckedListBox1.Items.Add(ttr.Item("serienaam")) "serienaam" = a-map or c-map
CheckedListBox2.Items.Add(ttr.Item("ouderdom")) "ouderdom" = week1 - week16
CheckedListBox3.Items.Add(ttr.Item("aantal")) "aantal" = 1 - 10
End While
added the code that adds the items to the checkedlistbox
Private Sub Button79_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button79.Click
While CheckedListBox1.CheckedItems.Count - 1 >= 0 ''i have changed this line a few times don't really know if it matters to the outcome of the error''
Dim serienaam
Dim oud
Dim aantal As Integer
Dim bedrag As Decimal
Dim totaal As Decimal
Try
serienaam = CheckedListBox1.CheckedItems(0)
oud = CheckedListBox2.CheckedItems(0)
aantal = CheckedListBox3.CheckedItems(0)
Catch ex As Exception
End Try
cmd = New SqlCommand("SELECT " & oud & " FROM series Where naamserie = '" & serienaam & "' ", con)
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read = True Then
Dim week = sdr.Item(oud)
bedrag = week * aantal
totaal = Label69.Text
totaal = totaal - bedrag
sdr.Close()
Label69.Text = totaal
End If
CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems(0))
CheckedListBox2.Items.Remove(CheckedListBox2.CheckedItems(0))
Try
CheckedListBox3.Items.Remove(CheckedListBox3.CheckedItems(0)) ''<<<----error
this try doesn't do anything actually except for the fact that im not getting any errors anymore
Catch ex As Exception
If ex IsNot Nothing Then
Dim s = CheckedListBox3.Items.Count
CheckedListBox3.SelectedItem = s
CheckedListBox3.Items.Remove(s)
End If
End Try
End While
End Sub
this is the code for the check
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
Dim index As Integer = e.Index
CheckedListBox2.SetItemChecked(index, e.NewValue)
CheckedListBox3.SetItemChecked(index, e.NewValue)
End Sub
my solution since it didn't work.
Private Sub Button76_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button76.Click
Dim serienaam
Dim oud
Dim aantal As Integer
Dim bedrag As Decimal
Dim totaal As Decimal
Dim t As String
serienaam = ComboBox6.SelectedItem
oud = ComboBox7.SelectedItem
aantal = TextBox45.Text
cmd = New SqlCommand("SELECT " & oud & " FROM series Where naamserie = '" & serienaam & "' ", con)
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read = True Then
Dim week = sdr.Item(oud)
bedrag = week
totaal = Label69.Text
totaal = totaal + bedrag * aantal
sdr.Close()
Label69.Text = totaal
t = " '" & serienaam & "' '" & oud & "' '" & aantal & "' "
CheckedListBox1.Items.Add(t)
End If
End Sub
Private Sub Button79_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button79.Click
While CheckedListBox1.CheckedItems.Count <> 0
Dim klantnr As String
Dim serienaam As String
Dim oud As String
Dim list As String
Dim aantal As Integer
Dim bedrag As Decimal
Dim totaal As Decimal
klantnr = TextBox32.Text
list = CheckedListBox1.CheckedItems(0)
Dim strlist = list.Split("'")
For count = 0 To strlist.Length - 1
serienaam = strlist(1)
oud = strlist(3)
aantal = strlist(5)
Next
cmd = New SqlCommand("SELECT " & oud & " FROM series Where naamserie = '" & serienaam & "' ", con)
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If sdr.Read = True Then
Dim week = sdr.Item(oud)
bedrag = week * aantal
totaal = Label69.Text
totaal = totaal - bedrag
sdr.Close()
Label69.Text = totaal
End If
cmd = New SqlCommand("DELETE FROM klantserie Where klantnummer = '" & klantnr & "' And serienaam = '" & serienaam & "' And ouderdom = '" & oud & "' And aantal = '" & aantal & "' ", con)
If con.State = ConnectionState.Closed Then con.Open()
cmd.ExecuteNonQuery()
ShowData()
CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems(0))
End While
End Sub
Not sure if you've fixed this but I tried to reproduce the problem and failed. Here's what I used:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CheckedListBox1.Items.Clear()
CheckedListBox1.Items.Add("List1Item1")
CheckedListBox1.Items.Add("List1Item2")
CheckedListBox1.Items.Add("List1Item3")
CheckedListBox2.Items.Clear()
CheckedListBox2.Items.Add("List2Item1")
CheckedListBox2.Items.Add("List2Item2")
CheckedListBox2.Items.Add("List2Item3")
CheckedListBox3.Items.Clear()
CheckedListBox3.Items.Add("List3Item1")
CheckedListBox3.Items.Add("List3Item2")
CheckedListBox3.Items.Add("List3Item3")
End Sub
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
Dim index As Integer = e.Index
CheckedListBox2.SetItemChecked(index, e.NewValue)
CheckedListBox3.SetItemChecked(index, e.NewValue)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
While CheckedListBox1.CheckedItems.Count <> 0
Dim serienaam As String
Dim oud As String
Dim aantal As String
serienaam = CheckedListBox1.CheckedItems(0)
oud = CheckedListBox2.CheckedItems(0)
aantal = CheckedListBox3.CheckedItems(0)
CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems(0))
CheckedListBox2.Items.Remove(CheckedListBox2.CheckedItems(0))
CheckedListBox3.Items.Remove(CheckedListBox3.CheckedItems(0))
End While
End Sub
This works just fine. I can add items to the boxes using Button 1 and then check anything in the first box (which checks the corresponding items in the other boxes based on index - couldn't be bothered to wire up lists 2 and 3 to do the same) and then I can delete the checked items with no errors using Button2. The only change to yours is obviously I'm using mocked data, not SQL queries.
Related
i have tried
Private Sub MOSTRAR_Click(sender As Object, e As EventArgs) Handles MOSTRARButton.Click
BARCODEReportViewer.ReportSource = Application.StartupPath + "\BARCODE\BarcodeReport.rpt"
Dim box As Integer = TextBox2.Text
Dim index As Integer = 0
Dim stringFormula As String
stringFormula = "{PRODUCT.PRO_UNIT_BARCODE} = '" & TextBox1.Text.ToString() & "'"
stringFormula &= "Or {PRODUCT.PRO_BARCODE_PACK} = '" & TextBox1.Text.ToString() & "'"
While index <= box
BARCODEReportViewer.SelectionFormula = stringFormula
index += 1
End While
BARCODEReportViewer.Refresh()
BARCODEReportViewer.RefreshReport()
End Sub
Hello I have this problem in came upon when trying to do simple manual search in vb.net. It says System.IndexOutOfRangeException: 'Cannot find column 4.'
could anyone explain what this means i am quite new to coding and dont quite get what i could to do fix this. In the attached database i have only one table called customer with 4 columns custid, custfname, custlname and dob.
This is the code and the error occurs in the btnNext and Navigaterecords. The database was made in sqlite
Imports System.Data.SQLite
Public Class Form1
Dim inc As Integer
Dim MaxRows As Integer
Dim ConnectionString As String = "Data Source=dbRoomBookings.db"
Dim ds As New DataSet
Dim dt As New DataTable
Dim mSQL As String = "SELECT * FROM Customer"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim con As New SQLiteConnection(ConnectionString)
Dim cmd As New SQLiteCommand(mSQL, con)
con.Open()
Dim da As New SQLiteDataAdapter(cmd)
da.Fill(ds, "customer")
dt = ds.Tables(0)
MaxRows = ds.Tables("customer").Rows.Count
con.Close()
Dim msSQL As String = "SELECT * FROM customer;"
dgvSearchResults.DataSource = display(msSQL, "customer")
End Sub
Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If inc <> MaxRows - 1 Then
inc = inc + 1
txtCusId.Text = ds.Tables("customer").Rows(inc).Item(0)
txtFname.Text = ds.Tables("customer").Rows(inc).Item(1)
txtLname.Text = ds.Tables("customer").Rows(inc).Item(2)
cboDay.Text = ds.Tables("customer").Rows(inc).Item(3)
cboMonth.Text = ds.Tables("customer").Rows(inc).Item(4)
cboYear.Text = ds.Tables("Customer").Rows(inc).Item(5)
Else
MsgBox("no more rows")
End If
End Sub
Sub navigaterecords()
txtCusId.Text = ds.Tables("customer").Rows(inc).Item(0)
txtFname.Text = ds.Tables("customer").Rows(inc).Item(1)
txtLname.Text = ds.Tables("customer").Rows(inc).Item(2)
cboDay.Text = ds.Tables("customer").Rows(inc).Item(3)
cboMonth.Text = ds.Tables("customer").Rows(inc).Item(4)
cboYear.Text = ds.Tables("customer").Rows(inc).Item(5)
txtDbo.Text = ds.Tables("customer").Rows(inc).Item(3) & "/" & ds.Tables("customer").Rows(inc).Item(4) & "/" & ds.Tables("customer").Rows(inc).Item(5)
End Sub
Private Sub BtnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
If inc > 0 Then
inc = inc - 1
navigaterecords()
Else
MsgBox("no more rows")
End If
End Sub
Private Sub BtnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
If inc <> 0 Then
inc = 0
navigaterecords()
End If
End Sub
Private Sub BtnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
If inc <> MaxRows - 1 Then
inc = MaxRows - 1
navigaterecords()
Else
End If
End Sub
Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim sSQL As String
Dim newds As New DataSet
Dim newdt As New DataTable
If txtSearchFname.Text <> "" Then
sSQL = "SELECT * FROM customer WHERE custfname LIKE'" & txtSearchFname.Text & "%'"
Dim con As New SQLiteConnection(ConnectionString)
Dim cmd As New SQLiteCommand(sSQL, con)
con.Open()
Dim da As New SQLiteDataAdapter(cmd)
da.Fill(newds, "customer")
newdt = newds.Tables(0)
dgvSearchResults.DataSource = newdt
con.Close()
ElseIf txtSearchId.Text <> "" Then
sSQL = "SELECT * FROM customer WHERE custid ='" & txtSearchId.Text & "'"
Dim con As New SQLiteConnection(ConnectionString)
Dim cmd As New SQLiteCommand(sSQL, con)
con.Open()
Dim da As New SQLiteDataAdapter(cmd)
da.Fill(newds, "customer")
newdt = newds.Tables(0)
dgvSearchResults.DataSource = newdt
con.Close()
End If
End Sub
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Dim con2 As New SQLiteConnection
Dim da2 As New SQLiteDataAdapter
Dim dsql, qsql As String
Dim ds2 As New DataSet
Dim dt2 As DataTable
con2 = New SQLiteConnection(ConnectionString)
dsql = "DELETE FROM customer WHERE custid = " & txtSearchId.Text & ""
qsql = "SELECT * FROM customer"
Dim cmd As New SQLiteCommand(qsql, con2)
con2.Open()
da2.DeleteCommand = con2.CreateCommand
da2.DeleteCommand.CommandText = dsql
da2.DeleteCommand.ExecuteNonQuery()
MsgBox("Row(s) Deleted !! ")
Dim da3 As New SQLiteDataAdapter(cmd)
da3.Fill(ds2, "customer")
dt2 = ds2.Tables(0)
dgvSearchResults.DataSource = dt2
con2.Close()
End Sub
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim con3 As New SQLiteConnection
Dim da3 As New SQLiteDataAdapter
con3 = New SQLiteConnection(ConnectionString)
Dim usql As String = "UPDATE Customer SET custfname = '" & txtFname.Text & "'" & "WHERE custid =" & CInt(txtCusId.Text) & ""
con3.Open()
da3.UpdateCommand = con3.CreateCommand
da3.UpdateCommand.CommandText = usql
da3.UpdateCommand.ExecuteNonQuery()
MsgBox("Row Updated")
End Sub
Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim isql As String = "INSERT INTO customer(custfname, custlname, dob) VALUES('" _
& txtFname.Text & "','" & txtLname.Text & "','" & cboYear.Text _
& "-" & cboMonth.Text & "-" & cboDay.Text & "');"
Dim msql As String = "SELECT * FROM customer"
add(isql)
'refresh the Data Grid
dgvSearchResults.DataSource = display(msql, "customer")
End Sub
End Class
Here is the specific documentation on IndexOutOfRangeException: https://learn.microsoft.com/en-us/dotnet/api/system.indexoutofrangeexception
For visibility, the definition is:
The exception that is thrown when an attempt is made to access an
element of an array or collection with an index that is outside its
bounds.
In your case, you are trying to get a value by passing the index of a column but the index you're passing is greater than the total number of columns.
Assuming that your table definition has the following columns: custfname, custlname, and dob, the following represents when the Exception would occur:
Get column 0, returns the value of custfname
Get column 1, returns the value of custlname
Get column 2, returns the value of dob
Get column 3, throws an IndexOutOfRangeException
The same thing would also happen if you tried to get a value at a negative index too.
You have a 4-column table which will have indexes from 0 to 3 only:
custid is column 0, custfname is column 1, custfname is column 2, and dob is column 3.
So any attempt to access ds.Tables("customer").Rows(inc).Item(4) (and (5), etc) will yield an Index out of range exception.
But you seem to know that the table only has 4 columns; why are you then attempting to access columns that you know don't exist? I'm a bit confused by that. If you're trying to split DOB into day/month/year, you'll have to manipulate the dob value from the DOB composite column, Item(3).
my problem is when I add account the account successfully added, but the row content is not showing in my datagridview.
this is the code for my add button in addaccount form. "frmaddaccount is the name of the form and button1 is the button when i add.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim un = TextBox1.Text
Dim pw = TextBox2.Text
Dim ac = lbluser.Text
Dim fn = TextBox4.Text
Dim ln = TextBox5.Text
Dim age = TextBox6.Text
Dim val = "accountId_seq.nextval,'" & un & "','" & pw & "','" & ac & "','" & fn & "','" & ln & "','" & age & "'"
Dim sql = "insert into d_account values(" & val & ")"
con.Connect()
con.Manipulate(sql)
accountLog()
MessageBox.Show("Account successfulyy added!")
display()
con.Close()
clear()
Catch ex As Exception
MessageBox.Show("ERROR:" & vbCrLf & " " & vbCrLf & " 1. Complete the required information " & vbCrLf & " 2. Make sure that you input correct datatypes " & vbCrLf & " 3. Username already exist")
End Try
End Sub
Private Sub frmaddAccount_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.Connect()
datagridview1.DataSource = dtb
dtb.Columns.Add("Username")
dtb.Columns.Add("Password")
dtb.Columns.Add("Access")
dtb.Columns.Add("First Name")
dtb.Columns.Add("Last Name")
dtb.Columns.Add("Age")
display()
con.Close()
End Sub
Sub display()
con.Connect()
Dim sql = "select * from d_account order by d_un asc"
odr = con.Retrieve(sql)
dtb.Clear()
While (odr.Read)
Dim un = odr.GetOracleValue(1)
Dim pw = odr.GetOracleValue(2)
Dim ac = odr.GetOracleValue(3)
Dim fn = odr.GetOracleValue(4)
Dim ln = odr.GetOracleValue(5)
Dim age = odr.GetOracleValue(6)
dtb.Rows.Add(un, pw, ac, fn, ln, age)
End While
con.Close()
End Sub
and this is the code in manageaccount form where my datagridview place.. frmmanageaccount is the name of the form.
Private Sub frmmanageAccount_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.Connect()
DataGridView1.DataSource = dtb
dtb.Columns.Add("Account ID")
dtb.Columns.Add("Username")
dtb.Columns.Add("Password")
dtb.Columns.Add("Access")
dtb.Columns.Add("First Name")
dtb.Columns.Add("Last Name")
dtb.Columns.Add("Age")
display()
con.Close()
Label14.Text = DataGridView1.RowCount()
Dim i As Integer
For i = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic
Next
Me.DataGridView1.DefaultCellStyle.Font = New Font("CAmbria", 10)
Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font = New Font("CAmbria", 12)
Me.DataGridView1.DefaultCellStyle.SelectionBackColor = Color.FromArgb(129, 207, 224)
End Sub
Sub display()
con.Connect()
Dim sql = "select * from d_account where d_ac = 'user' order by d_un asc"
odr = con.Retrieve(sql)
dtb.Clear()
While (odr.Read)
Dim id = odr.GetOracleValue(0)
Dim un = odr.GetOracleValue(1)
Dim pw = odr.GetOracleValue(2)
Dim ac = odr.GetOracleValue(3)
Dim fn = odr.GetOracleValue(4)
Dim ln = odr.GetOracleValue(5)
Dim age = odr.GetOracleValue(6)
dtb.Rows.Add(id, un, pw, ac, fn, ln, age)
Label14.Text = DataGridView1.RowCount()
End While
con.Close()
End Sub
I have created the following stored procedure in SQL Server:
CREATE Procedure CalcTotal
#InvoiceID Varchar(4)
As
Declare #Total int
Begin
SELECT
#Total = (TransportFee + EquipmentFee + LabourFee)
FROM
Invoice
WHERE
InvoiceID = #invoiceID
UPDATE Invoice
SET Total = #Total
WHERE InvoiceID = #invoiceID
END
My code for my form on Visual Basic are:
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Invoice
Dim sqlConn As New SqlConnection With {.ConnectionString = "Data Source=Hamsheed;Initial Catalog=assignment;Integrated Security=True"}
Dim sqlstr As String = "Select * From Invoice"
Dim MaxRows As Integer
Dim RowIndex As Integer = 0
Dim dt As New DataTable
Private Sub Invoice_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim DataAdapter As New SqlDataAdapter(sqlstr, sqlConn)
DataAdapter.Fill(dt)
DataAdapter.Dispose()
MaxRows = dt.Rows.Count
InvoiceDetails()
cmbSearch.DataSource = dt
cmbSearch.DisplayMember = "FirstName"
End Sub
Sub InvoiceDetails()
txtInvoiceID.Text = CStr(dt.Rows(RowIndex)("InvoiceID"))
txtClientID.Text = CStr(dt.Rows(RowIndex)("ClientID"))
txtEmployeeID.Text = CStr(dt.Rows(RowIndex)("EmployeeID"))
txtFillInDate.Text = CStr(dt.Rows(RowIndex)("FillInDate"))
txtTransportFee.Text = CStr(dt.Rows(RowIndex)("TransportFee"))
txtEquipmentFee.Text = CStr(dt.Rows(RowIndex)("EquipmentFee"))
txtLabourFee.Text = CStr(dt.Rows(RowIndex)("LabourFee"))
txtTotal.Text = CStr(dt.Rows(RowIndex)("Total"))
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim InvoiceID As String = txtInvoiceID.Text
Dim ClientID As String = txtClientID.Text
Dim EmployeeID As String = (txtEmployeeID.Text)
Dim FillInDate As Date = CDate(txtFillInDate.Text)
Dim TransportFee As Integer = CInt(txtTransportFee.Text)
Dim EquipmentFee As Integer = CInt(txtEquipmentFee.Text)
Dim LabourFee As Integer = CInt(txtLabourFee.Text)
Dim Total As Integer = CInt(txtTotal.Text)
If CStr(dt.Rows(RowIndex)("paymentmethod")) = "Card" Then
radCard.Select()
ElseIf CStr(dt.Rows(RowIndex)("paymentmethod")) = "Cash" Then
radCash.Select()
Else
radCredit.Select()
End If
Dim sqlQuery As String = ("Exec Insert_Invoice #InvoiceID = ' " & InvoiceID & " ', #ClientID = '" & ClientID & "',#EmployeeID ='" & EmployeeID & "', #FillInDate ='" & FillInDate & "',#TransportFee='" & TransportFee & "',#EquipmentFee = '" & EquipmentFee & "',#LabourFee= '" & LabourFee & "',#Total= '" & Total)
Dim sqlcmnd As New SqlCommand(sqlQuery, sqlConn)
Try
sqlConn.Open()
Dim changes As Integer = sqlcmnd.ExecuteNonQuery()
sqlConn.Close()
MessageBox.Show(changes & "Changes Made")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Save()
End Sub
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
dt.Rows(RowIndex)("InvoiceID") = CStr(txtInvoiceID.Text)
dt.Rows(RowIndex)("ClientID") = CStr(txtClientID.Text)
dt.Rows(RowIndex)("EmployeeID") = CStr(txtEmployeeID.Text)
dt.Rows(RowIndex)("FillInDate") = CStr(txtFillInDate.Text)
dt.Rows(RowIndex)("TransportFee") = CStr(txtTransportFee.Text)
dt.Rows(RowIndex)("EquipmentFee") = CStr(txtEquipmentFee.Text)
dt.Rows(RowIndex)("LabourFee") = CStr(txtLabourFee.Text)
dt.Rows(RowIndex)("Total") = CStr(txtTotal.Text)
Dim sqlquery As String = "exec edit_invoice '" & txtInvoiceID.Text & "', " & txtLabourFee.Text & ", " & txtTransportFee.Text & ", " & txtEquipmentFee.Text & ", '" & txtpaymentMethod.Text & "', '" & txtClientID.Text & "', '" & txtEmployeeID.Text & "', '" & txtFillInDate.Text & "', '" & txtTotal.Text & "'"
Dim sqlcmnd As New SqlCommand(sqlquery, sqlConn)
Try
sqlConn.Open()
Dim changes As Integer = sqlcmnd.ExecuteNonQuery()
sqlConn.Close()
MessageBox.Show(changes & "Changes Made")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Save()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtInvoiceID.Clear()
txtClientID.Clear()
txtEmployeeID.Clear()
txtFillInDate.Clear()
txtTransportFee.Clear()
txtEquipmentFee.Clear()
txtLabourFee.Clear()
txtTotal.Clear()
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If RowIndex <> MaxRows - 1 Then
RowIndex += 1
InvoiceDetails()
End If
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
RowIndex = MaxRows - 1
InvoiceDetails()
End Sub
Private Sub btnMainMenu_Click(sender As Object, e As EventArgs) Handles btnMainMenu.Click
SecretaryMainMenu.Show()
Me.Hide()
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Dim invoiceID As String = txtInvoiceID.Text
Dim SqlQuery As String = ("EXECUTE Delete_Invoice #InvoiceID = '" & InvoiceID & "'")
Dim cmd As New SqlCommand(SqlQuery, sqlConn)
Try
sqlConn.Open()
Dim changes As Integer = cmd.ExecuteNonQuery()
sqlConn.Close()
MessageBox.Show(changes & "Record Deleted")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Save()
dt.Rows(RowIndex).Delete()
End Sub
Sub Save()
Dim dataAdapter As New SqlDataAdapter(sqlstr, sqlConn)
Dim commandbuilder As New SqlCommandBuilder(dataAdapter)
dataAdapter.Update(dt)
dataAdapter.Dispose()
End Sub
Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
Dim searchitem As String = InputBox("Input Search invoice ID: ", "Search by ID")
Dim sresult As Boolean = False
searchitem = searchitem.Trim.ToUpper
For i As Integer = 0 To MaxRows - 1
If CStr(dt.Rows(i)("invoiceid")).ToUpper = searchitem Then
sresult = True
RowIndex = i
InvoiceDetails()
End If
Next
If sresult = False Then
MsgBox("No result found", MsgBoxStyle.OkOnly, "Information")
End If
End Sub
Private Sub txtinvoice_validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtInvoiceID.Validating
If Not txtInvoiceID.Text Like "I###" Then
e.Cancel = True
ErrorProvider1.SetError(txtInvoiceID, "Improper ID format")
End If
End Sub
Private Sub txtinvoice_validated(ByVal sender As Object, ByVal e As EventArgs) Handles txtInvoiceID.Validated
ErrorProvider1.SetError(txtInvoiceID, "")
End Sub
End Class
Now I want to call the stored procedure into the textbox Total, which will compute the 'transportFee, EquipmentFee and Labourfee' costs, and display the total into the textbox of txtTotal.Text
How do I write that piece of code?
use executescalar: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx
remember to set the commandtype property of the command to stored procedure
How to fill up the AutoCompleteCustomSource List with the query results using Data reader in vb.net ?
An example code needed.
Edit 1:
This is what I have tried and its not working
Private Sub cbEmployeeNo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbEmployeeNo.Click
cbEmployeeNo.AutoCompleteCustomSource.Clear()
Dim mySelectQuery As String = "SELECT * FROM EmployeeTable WHERE " & cbSearch.Text & " LIKE '" & cbEmployeeNo.Text & "' AND Status LIKE '" & cbStatus.Text & "'"
Dim myConnString As String = "Data Source=" & Application.StartupPath & "\Database\SimpleDB.db3"
Dim sqConnection As New SQLiteConnection(myConnString)
Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
sqConnection.Open()
Try
Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()
Dim m As Integer
Do While sqReader.Read
For m = 1 To sqReader.FieldCount() - 1
If (Not sqReader.IsDBNull(m)) Then
If cbSearch.Text = "EmployeeID" Then
cbEmployeeNo.AutoCompleteCustomSource.Add(sqReader.GetInt32(m))
Else
cbEmployeeNo.AutoCompleteCustomSource.Add(sqReader.GetString(m))
End If
End If
Next m
Loop
sqReader.Close()
Catch ex As Exception
MsgBox("Error: " & ex.ToString, vbExclamation)
Finally
sqConnection.Close()
End Try
End Sub
Edit 2: This is the second code I tried (following the link in DevExpress's answer). Its not working either
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sStringColl As New AutoCompleteStringCollection
Dim mySelectQuery As String = "SELECT * FROM EmployeeTable WHERE " & cbSearch.Text & " LIKE '" & cbEmployeeNo.Text & "' AND Status LIKE '" & cbStatus.Text & "'"
Dim myConnString As String = "Data Source=" & Application.StartupPath & "\Database\SimpleDB.db3"
Dim sqConnection As New SQLiteConnection(myConnString)
Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
sqConnection.Open()
Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()
While sqReader.Read()
sStringColl.AddRange(New String() {sqReader(0)})
End While
cbEmployeeNo.AutoCompleteCustomSource = sStringColl
End Sub
I have found the code you are looking for at:
AutoCompleteCustomSource