error as index was outside the bounds of the array? - vb.net

I tried debugging the code...but couldnt rectify...Hope someone gives suggestions
index was outside the bounds of the array
Private Sub loaditems()
Try
sqL = "SELECT bills.productname,bills.qty,bills.unitprice,bills.totalamt FROM bills INNER JOIN transactionDetails ON bills.invoiceno = transactionDetails.invoiceID where bills.invoiceno ='" & Trim(lblInvoice.Text) & " '"
ConnDB()
cmd = New SqlCommand(sqL, conn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
dgw.Rows.Clear()
Do While dr.Read = True
dgw.Rows.Add(dr(1), dr(0), dr(2), dr(3))
lblbalance.Text = dr(4)
dgw.Height += 19
x += 19
Loop
MsgBox("data showing")
Y = x - 30
dgw.Height = dgw.Height - 20
change.Location = New Point(49, 280 + Y)
lblbalance.Location = New Point(249, 280 + Y)
lblLine.Location = New Point(52, 299 + Y)
lblOR.Location = New Point(86, 315 + Y)
lblThank.Location = New Point(106, 331 + Y)
btnexit.Location = New Point(198, 369 + Y)
btnprint.Location = New Point(104, 369 + Y)
Panel1.Height = Panel1.Height + Y
Me.Height = Me.Height + Y
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

The index starts counting at 0.
this means that:
column 1 has a index of 0
column 2 has a index of 1.
column 3 has a index of 2.
column 4 has a index of 3.
Column 5 has a index of 4. <- dr(4) references column 5.
column 6 has a index of 5
dr(4) references to the fifth column, which does not exist. it only has 4 columns.

Related

use select case condition with SQL query statement in vb.net

I try to use select case statement with SQL statement
I have 4 columns when the lbl1.text equal 1 then x = "column1" and put this value in the SQL statement
when I try this code
Dim x As String
Select Case lbl1.Text
Case 1
x = "column1"
Case 2
x = " column2"
Case 3
x = " column3"
Case 4
x = " column4"
DGV1.Columns.Clear()
Dim dt As New DataTable
dt.Clear()
DGV1.Visible = True
Dim comm2 As New SqlCommand
comm2.CommandText = Nothing
DGV1.Refresh()
comm2.CommandText = "select first_name,last_name,age from students where " + x + "=" + "yes" + ""
comm2.Connection = sqlconn
sqlconn.Open()
dr2 = comm2.ExecuteReader
dt.Load(dr2)
DGV1.AutoGenerateColumns = True
DGV1.DataSource = dt
DGV1.Refresh()
sqlconn.Close()
DGV1.Visible = True
comm2.Dispose()
End Select
and when lbl1.text = 1 or 2 or 3 it doesn't run the SQL statement but when the lbl1.text = 4 it's run SQL statement
can any one help me in this code
try this code, i just don't know if the break is the right syntax, but i guarantee you that this is the right code
` Dim x As String
Select Case lbl1.Text
Case 1
x = "column1"
Case 2
x = " column2"
Case 3
x = " column3"
Case 4
x = " column4"
End Select
DGV1.Columns.Clear()
Dim dt As New DataTable
dt.Clear()
DGV1.Visible = True
Dim comm2 As New SqlCommand
comm2.CommandText = Nothing
DGV1.Refresh()
comm2.CommandText = "select first_name,last_name,age from students where " + x + "=" + "yes" + ""
comm2.Connection = sqlconn
sqlconn.Open()
dr2 = comm2.ExecuteReader
dt.Load(dr2)
DGV1.AutoGenerateColumns = True
DGV1.DataSource = dt
DGV1.Refresh()
sqlconn.Close()
DGV1.Visible = True
comm2.Dispose()`

Add text to automatic labels and textboxes vb.net

The following code creates/formats labels and textboxes to a groupbox.
My problem is that i want to change the textbox text to 1 and 0 periodically ( like intercalated ), and i have no idea how to.
Private Sub ResizeData()
' Create as many textboxes as fit into window
grpData.Controls.Clear()
Dim x As Integer = 0
Dim y As Integer = 10
Dim z As Integer = 20
While y < grpData.Size.Width - 100
labData = New Label()
grpData.Controls.Add(labData)
labData.Size = New System.Drawing.Size(30, 20)
labData.Location = New System.Drawing.Point(y, z)
labData.Text = Convert.ToString(x + 1)
txtData = New TextBox()
grpData.Controls.Add(txtData)
txtData.Size = New System.Drawing.Size(50, 20)
txtData.Location = New System.Drawing.Point(y + 30, z)
txtData.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
txtData.Tag = x
x += 1
z = z + txtData.Size.Height + 5
If z > grpData.Size.Height - 40 Then
y = y + 100
z = 20
End If
End While
End Sub
i need something like this:
txtData1.text="1"
txtData2.text="0"
txtData3.text="1"
txtData4.text="0"
...and so on.
Thank you!
Private Sub ResizeData()
' Create as many textboxes as fit into window
grpData.Controls.Clear()
Dim a As Integer = 1
Dim x As Integer = 1
Dim y As Integer = 10
Dim z As Integer = 20
While y < grpData.Size.Width - 100
Dim labData As New Label()
grpData.Controls.Add(labData)
labData.Size = New System.Drawing.Size(30, 20)
labData.Location = New System.Drawing.Point(y, z)
labData.Text = Convert.ToString(a)
Dim txtData As New TextBox()
grpData.Controls.Add(txtData)
txtData.Size = New System.Drawing.Size(50, 20)
txtData.Location = New System.Drawing.Point(y + 30, z)
txtData.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
txtData.Tag = x
txtData.Text = x
a += 1
If x = 1 Then
x = 0
ElseIf x = 0 Then
x = 1
End If
z = z + txtData.Size.Height + 5
If z > grpData.Size.Height - 40 Then
y = y + 100
z = 20
End If
End While
End Sub
To give only the first 2 textboxes a value '1', use the same code above with these 3 lines modified:
Dim txtData As New TextBox() With {.Name = "txt" & a}
txtData.Text = 0
If txtData.Name = "txt1" Or txtData.Name = "txt2" Then txtData.Text = 1 'add this line just below the above one
Maybe using a boolean would work. Not the cleaner way but it should work
Where you dim everything:
Dim even as boolean
And after all the property changes of the txtData:
if even then
txtData.Text=1
else
txtData.Text=0
end if
even= not even

VB - comparing numbers in two labels

I'm doing a school project in Visual Basic (using visual studio 2015) and i'm kinda stuck.
My goal is to create a lottery, where player chooses 6 numbers from checkboxes, then he generates six random numbers (1 - 49) and finally, those two sets should be compared and needed result is the number of correctly guessed numbers.
I have both results (guessed numbers, generated numbers) saved in two different labels.
The checkboxes itself are genereted like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lev = 20
tt = 0
For j = 1 To 50
tt = tt + 1
n = n + 1
box(j) = New CheckBox
box(j).Name = "box(" & Str(j) & ")"
If n = 11 Then lev = lev + 110 : n = 1 : tt = 1
box(j).Left = lev
box(j).Parent = Me
box(j).Top = tt * 20
box(j).Tag = j
box(j).Text = j
box(j).Visible = True
Next
box(50).Enabled = False
End Sub
First label (guessed numbers) is filled this way (i'm not posting whole code)
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + " " + box(j).Text
Next
and the second one (generated numbers) like this:
Do
rn = rg.Next(1, 50)
If Not r.Contains(rn) Then
r.Add(rn)
End If
Loop Until r.Count = 6
Label1.Text = r(0).ToString + " " + r(1).ToString + " " + r(2).ToString + " " + r(3).ToString + " " + r(4).ToString + " " + r(5).ToString
any idea how to compare numbers stored in those labels and get the result (number of correctly guessed numbers).
thanks in advance
You can compare numbers in the labels by splitting the Text properties of the labels into arrays of strings and converting them to integer arrays. First though there is a tiny problem with your code that adds the guessed numbers to the label.
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + " " + box(j).Text
Next
The " " should be moved to the end of the line because at the moment, the label will always start with a space and that messes with the function below. So you should have -
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + box(j).Text + " "
Next
Ok. The function below splits the two text labels into their own array and loops through the guesses and checks if any number is contained in the generated numbers. It then returns the number of matches.
Private Function ComparePicks() As Integer
Dim numbersMatched As Integer
Dim picks(5) As Integer
Dim generatedNumbers(5) As Integer
For i As Integer = 0 To 5
picks(i) = CInt(Split(Label9.Text, " "c)(i))
Next
For i As Integer = 0 To 5
generatedNumbers(i) = CInt(Split(Label1.Text, " "c)(i))
Next
For i As Integer = 0 To 5
If generatedNumbers.Contains(picks(i)) Then
numbersMatched += 1
End If
Next
Return numbersMatched
End Function

vb.net Getting numbers from array horizontally and vertically?

I have this crazy array.
ReDim arrayDeCeldas(filas - 1, columnas - 1)
For i = 0 To filas - 1
For j = 0 To columnas - 1
arrayDeCeldas(i, j) = i & j
Debug.Write(arrayDeCeldas(i, j) & " ")
Next j
Debug.WriteLine("")
Next i
And I'm trying to link it to a number of conditions and is not well to do
I'm trying to try to get 6 index from array
The number can't repeat and it are pick horizontally or vertically randomly
0 1 2 3 4 5 6 7
10 11 12 13 14 15 16 17
20 21 22 23 24 25 26 27
30 31 32 33 34 35 36 37
40 41 42 43 44 45 46 47
This example about i'm try.
It can be seen, there are a total of 6 items with different sizes
Actually my code, it's getting huge
Private Sub setBarco()
Dim numeroRandom As New System.Random()
Dim indiceA, indiceB As Integer
For b = 1 To barcos
Randomize()
Dim value As Integer = CInt(Int((2 * Rnd() + 1)))
indiceA = numeroRandom.Next(0, filas)
indiceB = numeroRandom.Next(0, columnas)
Select Case b
Case 1
If arrayDeCeldas(indiceA, indiceB) = 0 Then
arrayDeCeldas(indiceA, indiceB) = b & 1
End If
Case 2
Select Case value
Case 1
For c = 0 To 1
If indiceB + c < columnas Then
If arrayDeCeldas(indiceA, indiceB + c) = 0 Then
arrayDeCeldas(indiceA, indiceB + c) = b & c
End If
Else
If arrayDeCeldas(indiceA, indiceB - c) = 0 Then
arrayDeCeldas(indiceA, indiceB - c) = b & c
End If
End If
Next
Case 2
For c = 0 To 1
If indiceA + c < filas Then
If arrayDeCeldas(indiceA + c, indiceB) = 0 Then
arrayDeCeldas(indiceA + c, indiceB) = b & c
Else
End If
Else
End If
Next
End Select
Case 3
Case 4
Case 5
Case 6
End Select
Next b
End Sub
Based on your comment-reply, I understand the program must:
Pick at random any six elements from a 2D-array, and each element or value must be selected only once.
That's fairly straightforward: here's a pseudo-code implementation:
Perform some initial computation:
Get the bounds of the 2D array
Seed a random number generator:
Create a HashSet instance
Get six pairs of coordinates:
Loop continuously until HashSet has 6 elements in it:
Generate a coordinate pair within the bounds of the array found in Step 1.
Get the value from the coordinate pair
Check to see if we've seen the value before (by looking at the HashSet). If we've not seen it before, then add it to the HashSet and continue, otherwise ignore it and try again.
Return the contents of the HashSet
In C# this would be:
Int32[,] numbers = new Int32[] { ... };
Int32 maxY = numbers.GetUpperBound(0); // y-axis in dimension 0
Int32 maxX = numbers.GetUpperBound(1); // x-axis in dimension 1
Random rng = new Random();
HashSet<Int32> values = new HashSet<Int32>();
while( values.Count < 6 ) {
Int32 x = rng.Next( maxX + 1 ); // Random.Next is upperbound exclusive, hence +1
Int32 y = rng.Next( maxY + 1 );
Int32 value = numbers[ y, x ];
if( !values.Contains( value ) ) values.Add( value );
}
return values.ToArray();
Note that by keeping track of observed values, instead of coordinates, we can simultaneously avoid duplicate values and duplicate coordinates: as duplicate coordinates will give you duplicate values anyway.

Datatable and Datagridview cell calculation

This my initial code,
Private Sub Create_vTable()
Dim i,j As Integer
Dim q, PV, YP, MW, id As Double
q = TextBox1.Text
MW = TextBox2.Text
PV = TextBox3.Text
YP = TextBox4.Text
vTable = New DataTable
vTable.Columns.Add("Name", GetType(String))
vTable.Columns.Add("Len", GetType(Double))
vTable.Columns.Add("ID", GetType(Double))
vTable.Columns.Add("OD", GetType(Double))
vTable.Columns.Add("Totlen", GetType(Double))
vTable.Columns.Add("Vel", GetType(Double))
id = DGV6.CurrentRow.Cells(2).Value
For i = 0 To DGV6.Rows.Count - 2
vRow = vTable.NewRow
vRow.Item(5) = (q / (2.448 * (id) ^ 2))
For j = 0 To DGV6.Columns.Count - 1
vRow.Item(j) = DGV.Rows(i).Cells(j).Value.ToString
Next
vTable.Rows.Add(vRow)
Next
Catch ex As Exception
End Try
End Sub
I have data in DGV6 and textbox, i like to do calculation on datatable item 5 each cell but after test, the result show only one calculated for entire columns not following each cell, any one can help, thanks in advance
You only get the id value once from one row in the DataGridView. You'll have to get it's value in each loop iteration:
For i = 0 To DGV6.Rows.Count - 2
vRow = vTable.NewRow
id = DGV.Rows(i).Cells(2).Value
vRow.Item(5) = (q / (2.448 * (id) ^ 2))
For j = 0 To DGV6.Columns.Count - 1
vRow.Item(j) = DGV.Rows(i).Cells(j).Value.ToString
Next
vTable.Rows.Add(vRow)
Next