Select from table Syntax with multiple paramaters - vb.net

I need help with getting my syntax correct on the myDataAdapter.SelectCommand.CommandText line. I cannot seem to get the date to work as my second parameter, before it would show if a person was in a seat of my theatre, now I made more dates for shows so I need to check the seat and the date now and I cannot seem to get the date check to work:
Private Sub lblSeat1_MouseEnter(sender As Object, e As EventArgs) Handles lblSeat1.MouseEnter
'NEED HELP ON THIS LINE BELOW
myDataAdapter.SelectCommand.CommandText = ("select * from seating where seat_no = " & seatNumber(0) And "select * from seating where perf_date = " & lstPerfDates.SelectedIndex)
myDataSet.Clear()
myDataAdapter.Fill(myDataSet)
If myDataSet.Tables(0).Rows.Count = 0 Then
lblSeat1.BackColor = Color.Green
ToolTipSeats.SetToolTip(lblSeat1, "Available")
ElseIf myDataSet.Tables(0).Rows.Count = 1 Then
lblSeat1.BackColor = Color.Red
ToolTipSeats.SetToolTip(lblSeat1, myDataSet.Tables(0).Rows(0)("patron"))
End If
End Sub

Changed to use parameters, and missing .item before ("patron")
Private Sub lblSeat1_MouseEnter(sender As Object, e As EventArgs) Handles lblSeat1.MouseEnter
'NEED HELP ON THIS LINE BELOW
myDataAdapter.SelectCommand.CommandText = ("select * from seating where seat_no = #seatNumber and perf_date #perfDate")
myDataAdapter.SelectCommand.CommandType = CommandType.Text
myDataAdapter.SelectCommand.Parameters.AddWithValue("#seatNumber", seatNumber(0))
myDataAdapter.SelectCommand.Parameters.AddWithValue("#perfDate", lstPerfDates.SelectedValue)
myDataSet.Clear()
myDataAdapter.Fill(myDataSet)
If myDataSet.Tables(0).Rows.Count = 0 Then
lblSeat1.BackColor = Color.Green
ToolTipSeats.SetToolTip(lblSeat1, "Available")
ElseIf myDataSet.Tables(0).Rows.Count = 1 Then
lblSeat1.BackColor = Color.Red
ToolTipSeats.SetToolTip(lblSeat1, myDataSet.Tables(0).Rows(0).item("patron"))
End If
End Sub

Related

vb.net threading sub slower than call sub

I know this Question was already posted, but it was never really answered, or I did not understand the answer :)
My Problem is that when I call this Sub within the Thread, it takes about 4 sec to execute the code, but if I make a simple Call Sub (Like commented in Code) it takes about 450 ms.
I have a simple program with 2 Subs:
To report Progress on Main Form
To do some "For" Looping
Imports System.Data.SqlClient
Public Class Form1
Public Delegate Sub ProzentDelegate(ByVal Prozent As Double)
Dim G_I_Temp As Integer = 0
Dim G_S_Prüfziffer As String
Dim G_S_Präfix As String
Dim G_I_Zähler As Integer
Dim G_I_Stellen As Integer
Dim G_D_Step As Integer = 1
Dim G_I_Position As Integer
Dim DT_G_Prüftabelle As DataTable
Dim DR_G_Prüftabelle As DataRow
Dim thisLock As New Object
Dim conn As String = "Data Source=SR-SQLWVS;Initial Catalog=Barcode;Integrated Security=True"
Dim sourceconn As New SqlConnection(conn)
Dim adap As SqlDataAdapter
Dim cmd As SqlCommand = New SqlCommand("SELECT TOP(10) * FROM dbo.Prüf_Tabelle", sourceconn)
Dim Thrd_preview As Threading.Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
sourceconn.Open()
cmd.Connection = sourceconn
End Sub
Public Sub ReportProgress(ByVal Prozent As Double)
ProgressBar1.Value = Prozent
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RB_Thread.Checked Then
If Thrd_preview IsNot Nothing Then Thrd_preview = Nothing
Thrd_preview = New Threading.Thread(AddressOf DoSomeWork)
Thrd_preview.IsBackground = True
Thrd_preview.Start()
Else
DoSomeWork()
End If
End Sub
Private Sub DoSomeWork()
Dim ds As New DataSet
DT_G_Prüftabelle = ds.Tables.Add("Prüf_Tabelle")
Dim L_I_Counter, L_I_Counter2 As Integer
Dim L_I_Ende As Integer
Dim L_I_Step As Integer
Dim L_I_Nutzen As Integer = 1
L_I_Step = 1
L_I_Ende = Val(TB_Auftrag_neu_Anzahl.Text)
G_I_Stellen = 8
G_S_Prüfziffer = "1"
G_S_Präfix = "00"
If TB_Info_Increment.Text = "" Then TB_Info_Increment.Text = 1
adap = New SqlDataAdapter("SELECT TOP(10) * FROM dbo.Prüf_Tabelle", sourceconn)
adap.FillSchema(DT_G_Prüftabelle, SchemaType.Mapped)
DT_G_Prüftabelle.Rows.Clear()
Dim pre As Date = Now
For L_I_Counter = 1 To (L_I_Ende * L_I_Step) Step L_I_Step
For L_I_Counter2 = 1 To L_I_Nutzen
DR_G_Prüftabelle = DT_G_Prüftabelle.NewRow
DR_G_Prüftabelle.Item("Auftrag_Lfd_nr") = TB_KeyLot.Text
DR_G_Prüftabelle.Item("Position") = G_I_Position
G_I_Position += G_D_Step
DR_G_Prüftabelle.Item("Barcode_soll") = F_Berechnung((Trim(Str(Val(TB_Auftrag_neu_Von.Text) + L_I_Counter - 1))).PadLeft(G_I_Stellen, "0"))
DT_G_Prüftabelle.Rows.Add(DR_G_Prüftabelle)
If ProgressBar1.InvokeRequired Then
ProgressBar1.BeginInvoke(New ProzentDelegate(AddressOf ReportProgress), (L_I_Counter * 100) / (L_I_Ende * L_I_Step))
Else
ReportProgress((L_I_Counter * 100) / (L_I_Ende * L_I_Step))
End If
Next
Next
MsgBox(Now.Subtract(pre).TotalMilliseconds.ToString & " ms.")
End Sub
Function F_Berechnung(ByVal L_Nummer As String) As String
Dim L_B_Gerade As Boolean = False
Dim L_I_Prüfziffer As Integer
Dim L_I_Stellen As Integer
Me.G_I_Temp = 0
Select Case G_S_Prüfziffer
Case "0"
F_Berechnung = L_Nummer
Case "1"
F_Berechnung = G_S_Präfix & Trim(L_Nummer)
Case "2"
F_Berechnung = G_S_Präfix & Trim(L_Nummer)
Case "3"
F_Berechnung = L_Nummer
'
Case "5" '-----Mod 10 gewichtung 31...-----
L_Nummer = G_S_Präfix & Trim(L_Nummer)
L_I_Stellen = Len(L_Nummer)
For Me.G_I_Zähler = L_I_Stellen To 1 Step -1
Me.G_I_Temp += Val(Mid(L_Nummer, Me.G_I_Zähler, 1)) * IIf(L_B_Gerade, 1, 3)
L_B_Gerade = Not L_B_Gerade
Next
L_I_Prüfziffer = IIf(10 - Me.G_I_Temp Mod 10 = 10, 0, 10 - Me.G_I_Temp Mod 10)
F_Berechnung = Trim(L_Nummer) & Trim(Str(L_I_Prüfziffer))
'
Case "6" '-----Mod 10 gewichtung 13...-----
L_Nummer = G_S_Präfix & Trim(L_Nummer)
L_I_Stellen = Len(L_Nummer)
For Me.G_I_Zähler = L_I_Stellen To 1 Step -1
Me.G_I_Temp += Val(Mid(L_Nummer, Me.G_I_Zähler, 1)) * IIf(L_B_Gerade, 3, 1)
L_B_Gerade = Not L_B_Gerade
Next
L_I_Prüfziffer = IIf(10 - Me.G_I_Temp Mod 10 = 10, 0, 10 - Me.G_I_Temp Mod 10)
F_Berechnung = Trim(L_Nummer) & Trim(Str(L_I_Prüfziffer))
'
Case Else
F_Berechnung = L_Nummer
End Select
End Function
I have tried to take out Progress Reporting but the Result is the same.
I hope you have some suggestions or solutions for me. I would be very thankful.
Give these changes a try
Change this
If ProgressBar1.InvokeRequired Then
ProgressBar1.Invoke(New ProzentDelegate(AddressOf ReportProgress), Progr)
Else
ReportProgress(Progr)
End If
to
ReportProgress(Progr)
And change method ReportProgress to
Public Sub ReportProgress(ByVal Prozent As Double)
Me.BeginInvoke(Sub()
ProgressBar1.Value = Prozent
End Sub)
End Sub
See if that makes a difference. The check for InvokeRequired is not needed, the answer is always yes.
Just wanted to report that I found a solution.
The main problem was this line of code in:
DR_G_Prüftabelle.Item("Auftrag_Lfd_nr") = TB_KeyLot.Text
So if I start this Sub from another Thread rather than from a Main Thread, for every single loop it has to go to main thread and to take Paramater from TextBox and to go back in Thread to Loop further.
I took that out, declared a variable at the begining and assigned this TextBox value to it and it works like charm.
Also I took a suggestion from dbasnett to Invoke Reporting every time without If-Else and also the suggestion from Steven Doggart to Report every 100 Loops.
You guys helped a lot. Thanks for that!!

Index is outside the bounds of the array VB 2015

I'm getting this error message, and it points to this line
Index was outside the bounds of the array
bookinfo(i).Author = parts(1)
Could someone help me understand what I'm doing wrong?
Thank you!
Private Sub JamesProject1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
Dim parts() As String
lines = IO.File.ReadAllLines("LibraryDatabase.txt")
ReDim Preserve bookinfo(lines.Count)
If Not OpenFileDialog1.ShowDialog() = DialogResult.OK Then
MessageBox.Show("You must choose a file")
Me.Close()
End If
lines = IO.File.ReadAllLines(OpenFileDialog1.FileName)
For i = 0 To lines.Count - 1
parts = lines(i).Split(","c)
bookinfo(i).Title = parts(0)
bookinfo(i).Author = parts(1)
bookinfo(i).ISBN = parts(2)
bookinfo(i).YearPublished = parts(3)
Next
Dim query = From book In bookinfo
Order By book.Title, book.Author, book.ISBN, book.YearPublished
Select book.Title, book.Author, book.ISBN, book.YearPublished
DGVBookInfo.DataSource = query.ToList
DGVBookInfo.Columns("Title").HeaderText = "Title"
DGVBookInfo.Columns("Author").HeaderText = "Author"
DGVBookInfo.Columns("ISBN").HeaderText = "ISBN"
DGVBookInfo.Columns("Year Published").HeaderText = "Year Published"
DGVBookInfo.AutoResizeColumns()
DGVBookInfo.RowHeadersVisible = False
End Sub

Operator '+' is not defined for type 'Decimal' and type 'DBNull'

im working to show the data in my datagrid,and show the total of items brought in textbox2,but this error "Operator '+' is not defined for type 'Decimal' and type 'DBNull'." what is the problem? please help me i need to finish this so badly. :(
Private Sub dailySales_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.Connect()
DataGridView1.DataSource = dtb
dtb.Columns.Add("Transaction Code")
dtb.Columns.Add("Transaction Date ")
dtb.Columns.Add("Item Code")
dtb.Columns.Add("Item")
dtb.Columns.Add("Description")
dtb.Columns.Add("Quantity")
dtb.Columns.Add("Price")
dtb.Columns.Add("Total")
display()
con.Close()
total()
TextBox1.Text = DataGridView1.RowCount()
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)
Dim i As Integer
For i = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Columns.Item(i).SortMode = DataGridViewColumnSortMode.Programmatic
Next
End Sub
Sub total()
Dim sum As Decimal = 0
For x = 0 To DataGridView1.Rows.Count - 1
sum =sum + DataGridView1.Rows(x).Cells(7).Value
Next
TextBox2.Text = sum
End Sub
Sub display()
con.Connect()
Label6.Text = Date.Now.ToString("dd MMMM yyyy")
Dim da = Label6.Text
Dim sc = " where lower(tns_date) like lower('%" & da & "%') "
Dim sql = "select * from tns " & sc & " order by tns_code desc"
odr = con.Retrieve(sql)
dtb.Clear()
While (odr.Read)
Dim ic = odr.GetOracleValue(0)
Dim itn = odr.GetOracleValue(1)
Dim de = odr.GetOracleValue(2)
Dim ca = odr.GetOracleValue(3)
Dim pr = odr.GetOracleValue(4)
Dim st = odr.GetOracleValue(5)
Dim sst = odr.GetOracleValue(6)
dtb.Rows.Add(ic, itn, de, ca, pr, st, sst)
End While
con.Close()
End Sub
Test like this.
Sub total()
Dim sum As Decimal = 0
For x = 0 To DataGridView1.Rows.Count - 1
If Not isDBNull(DataGridView1.Rows(x).Cells(7).Value) then sum =sum + DataGridView1.Rows(x).Cells(7).Value
Next
TextBox2.Text = sum
End Sub

Issue with combobox autocomplete in vb.net

I have a combobox populated by a datatable, the code searches for a text string located at any position of the field while the user is writing, so far no problem.
So the problem is: When I write the third character the combobox autocompletes with the first result, and there is no way to type anything else.
I have tried already using all AutocompleteMode & AutocompleteSourse properties settings and combinations.
That’s why I’m asking for help.
The code is below:
Private Sub ComboListadoRemitente_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboListadoRemitente.KeyUp
Dim strText As String
strText = ComboListadoRemitente.Text
If Len(strText) > 2 Then
ComboListadoRemitente.DataSource = dtListado.Select("listado LIKE '%" & strText & "%'")
ComboListadoRemitente.DroppedDown = True
Cursor.Current = Cursors.Default
End If
End Sub
Thanks
Finally I got something that works well, it is not the final version, surely can be further improved, here is the code:
Public Sub ComboListadoRemitente_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboListadoRemitente.KeyUp
Dim strText As String
strText = ComboListadoRemitente.Text
If ComboListadoRemitente.Text = "" Then
ComboListadoRemitente.DataSource = Me.dtListado
ComboListadoRemitente.ValueMember = "Id"
ComboListadoRemitente.DisplayMember = "listado"
ComboListadoRemitente.SelectedIndex = -1
ComboListadoRemitente.DroppedDown = False
End If
If Len(strText) > 2 Then
ComboListadoRemitente.DataSource = dtListado.Select("listado LIKE '%" & strText & "%'")
ComboListadoRemitente.ValueMember = "Id"
ComboListadoRemitente.DisplayMember = "listado"
If ComboListadoRemitente.Items.Count <> 0 Then
ComboListadoRemitente.DroppedDown = True
ComboListadoRemitente.SelectedIndex = -1
ComboListadoRemitente.Text = ""
ComboListadoRemitente.SelectedText = strText
strText = ""
Cursor.Current = Cursors.Default
Else
ComboListadoRemitente.DataSource = Me.dtListado
ComboListadoRemitente.ValueMember = "Id"
ComboListadoRemitente.DisplayMember = "listado"
ComboListadoRemitente.SelectedIndex = -1
ComboListadoRemitente.Text = ""
ComboListadoRemitente.SelectedText = strText
strText = ""
ComboListadoRemitente.DroppedDown = False
End If
End If
End Sub

Highlight a row in Datagridview based on textbox value

Good Evening.
I have a program in VB.Net that refreshes datagridview after you edit some data my problem here is im populating over 1000 records.
Let say Im editing row 999 and after i click update the data will refresh causing the datagridview to return at the top (The blue highlighter)
My goal here is how can I maintain it to its current position after update?
My solution here is highlight the data where Textbox1 = value
Is this possible like this?
'SAMPLE CODE
Datagridview1.Column(0).value.BlueHighLighter = Textbox1.text
Pls see my code on how i refresh DGV
Dim con11 As MySqlConnection = New MySqlConnection("server=192.168.2.87;userid=root;password=admin1950;database=inventory")
Dim sql1 As MySqlCommand = New MySqlCommand("select PONo,ItemCode,Description,QtyPack,PackUoM,QtyStan,StanUoM,UnitPrice,Total,Remarks,ExpiryDate from Receiving where RINo = '" & Add_Receiving_Items.TextBox1.Text & "';", con1)
Dim ds1 As DataSet = New DataSet
Dim adapter1 As MySqlDataAdapter = New MySqlDataAdapter
con1.Open()
adapter1.SelectCommand = sql1
adapter1.Fill(ds1, "MyTable")
Add_Receiving_Items.DataGridView1.DataSource = ds1.Tables(0)
con1.close()
With Add_Receiving_Items.DataGridView1()
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "PO No"
.Columns(1).HeaderCell.Value = "Item Code"
.Columns(2).HeaderCell.Value = "Description"
.Columns(3).HeaderCell.Value = "Quantity/Pack"
.Columns(4).HeaderCell.Value = "Packaging UoM"
.Columns(5).HeaderCell.Value = "Quantity/Pc"
.Columns(6).HeaderCell.Value = "Standard UoM"
.Columns(7).HeaderCell.Value = "Unit Price"
.Columns(8).HeaderCell.Value = "Total"
.Columns(9).HeaderCell.Value = "Remarks"
.Columns(10).HeaderCell.Value = "Expiry Date"
End With
Add_Receiving_Items.DataGridView1.Columns.Item(0).Width = 80
Add_Receiving_Items.DataGridView1.Columns.Item(1).Width = 80
Add_Receiving_Items.DataGridView1.Columns.Item(2).Width = 120
Add_Receiving_Items.DataGridView1.Columns.Item(3).Width = 86
Add_Receiving_Items.DataGridView1.Columns.Item(4).Width = 68
Add_Receiving_Items.DataGridView1.Columns.Item(5).Width = 75
Add_Receiving_Items.DataGridView1.Columns.Item(6).Width = 68
Add_Receiving_Items.DataGridView1.Columns.Item(7).Width = 70
Add_Receiving_Items.DataGridView1.Columns.Item(8).Width = 80
Add_Receiving_Items.DataGridView1.Columns.Item(9).Width = 105
Add_Receiving_Items.DataGridView1.Columns.Item(10).Width = 63
Add_Receiving_Items.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
Add_Receiving_Items.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
With Add_Receiving_Items.DataGridView1
.RowsDefaultCellStyle.BackColor = Color.WhiteSmoke
.AlternatingRowsDefaultCellStyle.BackColor = Color.Lavender
End With
TYSM for future help
Use DataGridView.CurrentRow property. But please be aware that CurrentRow is ReadOnly, you must use CurrentCell
Before refreshing your data store Dim oldIndex = DataGridView.CurrentRow.Index and after the refresh set DataGridView.CurrentCell = DataGridView.Rows(oldIndex).Cells(0)
EDIT:
How to test the Code
Create a form with a Button1 and a DataGridView1 with two columns and paste the following code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 5
DataGridView1.Rows.Add("foo" & i, "bar" & i)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim currentIndex = DataGridView1.CurrentRow.Index
currentIndex += 1
If currentIndex >= DataGridView1.Rows.Count Then currentIndex = 0
DataGridView1.CurrentCell = DataGridView1.Rows(currentIndex).Cells(0)
End Sub
End Class
EDIT:
Dim oldIndex = DataGridView.CurrentRow.Index
'Put your code here on how you refresh your data
DataGridView.CurrentCell = DataGridView.Rows(oldIndex).Cells(0)