Index is outside the bounds of the array VB 2015 - vb.net

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

Related

Hangman vb.net only first letter showing

Basically I generate a random word for example
"Tree" and when I press the T button it changes the label into a T but then when I choose R it doesnt show, can someone else see what i've done wrong?
here is my code
Sub GuessLetter(ByVal LetterGuess As String)
Dim strGuessedSoFar As String = Lbltempword.Text
Dim LengthOfSecretWord As Integer
LengthOfSecretWord = secret.Length - 1
tempWord = ""
Dim letterPosition As Integer
For letterPosition = 0 To LengthOfSecretWord
If secret.Substring(letterPosition, 1) = LetterGuess Then
tempWord = tempWord & LetterGuess
Else
tempWord = tempWord & Lbltempword.Text.Substring(letterPosition, 1)
End If
Next
Lbltempword.Text = tempWord
If Lbltempword.Text = secret Then 'YOU WIN
DisableButtons()
BtnStart.Enabled = True
MsgBox("YOU WIN")
End If
If Lbltempword.Text = strGuessedSoFar Then
NumWrong = NumWrong + 1
End If
DisplayHangman(NumWrong)
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
randomword()
MsgBox(secret)
EnableButtons()
BtnStart.Enabled = False
'Load up the temp word label with dashes
Secret_Word = secret
LoadLabelDisplay()
NumWrong = 0
DisplayHangman(NumWrong)
End Sub
Sub LoadLabelDisplay()
Lbltempword.Text = ""
Dim LengthOfSecretWord As Integer
LengthOfSecretWord = secret.Length - 1
Dim LetterPosition As Integer
For LetterPosition = 0 To LengthOfSecretWord
Lbltempword.Text = Lbltempword.Text & "-"
Next
End Sub
I also generate the random words by doing this.
Sub randomword()
Dim RAND(16)
Dim rng As New System.Random()
For i = 0 To 16
RAND(0) = "Tree"
RAND(1) = "Star"
RAND(2) = "Jesus"
RAND(3) = "Present"
RAND(4) = "advent"
RAND(5) = "Calender"
RAND(6) = "Jinglebell"
RAND(7) = "skint"
RAND(8) = "lapland"
RAND(9) = "Santa"
RAND(10) = "raindeer"
RAND(11) = "Cookies"
RAND(12) = "Milk"
RAND(13) = "nothing"
RAND(14) = "play"
RAND(15) = "sack"
Next
secret = RAND(rng.Next(RAND.Count()))
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

Searching in Second column in DataGridView and Giving a yes or no answer

I am trying to figure out how to search from the first Textbox in the second column in the DataGridView and after searching, to list a yes or no answer if the State Abbreviation is a Northwest State in the second Textbox(T2). This is what I have so far.
Private Sub L_Click(sender As Object, e As EventArgs) Handles Locate.Click
Dim NW As String = "OR"
Dim NW2 As String = "WA"
Dim A As String = T1.Text
If A = "" Then
MessageBox.Show("Must Enter Abbreviation")
Else
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells.Item("State_Abbreviation").Value = T1.Text Then
If row.Cells.Item("State_Abbreviation").Value = NW Or NW2 Then
T2.Text = "Yes"
End If
T2.Text = "No"
End If
MessageBox.Show("Please Enter Valid Abbreviation")
Next
End If
End Sub
Trying to simplify your code....
Dim result as String = "No"
Dim cellValue = row.Cells.Item("State_Abbreviation").Value.ToString
If cellValue = T1.Text Then
If cellValue = NW Or cellValue = NW2 Then
result = "Yes"
End If
End If
T2.Text = result

data still saving on zero

in my code the data is saving if no.of days to complete=0.I want it should not happen
in my code a msg comes "time to complete and time to acknowledged can be only be posiitve" but still data saves.i dont want it to be saved !!
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Try
Dim i As Integer
If gvBanckmark.Rows.Count > 0 Then
For i = 0 To gvBanckmark.Rows.Count - 1
Dim lblBenchMarkID As Label = CType(gvBanckmark.Rows(i).FindControl("lblBenchMarkID"), Label)
Dim lblSubCategoryId As Label = CType(gvBanckmark.Rows(i).FindControl("lblSubCategoryId"), Label)
Dim ddlFrequencyTypeAcknowledge As DropDownList = CType(gvBanckmark.Rows(i).FindControl("ddlFrequencyTypeAcknowledge"), DropDownList)
Dim ddlFrequencyTypeComplete As DropDownList = CType(gvBanckmark.Rows(i).FindControl("ddlFrequencyTypeComplete"), DropDownList)
Dim txtTimeToAcknowledge As TextBox = CType(gvBanckmark.Rows(i).FindControl("txtTimeToAcknowledge"), TextBox)
Dim txtTimeToComplete As TextBox = CType(gvBanckmark.Rows(i).FindControl("txtTimeToComplete"), TextBox)
Dim objBenchMark As BO.BenchMark = New BO.BenchMark()
objBenchMark.BuildingID = Convert.ToInt32(ddlBuilding.SelectedValue)
objBenchMark.CategoryID = Convert.ToInt32(ddlCategory.SelectedValue)
objBenchMark.SubCategoryID = Convert.ToInt32(lblSubCategoryId.Text.ToString())
objBenchMark.FrequencyTypeToAcknowledge = ddlFrequencyTypeAcknowledge.SelectedValue.ToString()
objBenchMark.FrequencyTypeToComplete = ddlFrequencyTypeComplete.SelectedValue.ToString()
objBenchMark.TimeToAcknowledge = Convert.ToInt32(txtTimeToAcknowledge.Text.ToString())
objBenchMark.TimeToComplete = Convert.ToInt32(txtTimeToComplete.Text.ToString())
If lblBenchMarkID.Text.ToString() = 0 Then
objBenchMark.BenchMarkID = 0
objBenchMark.CreateByUserId = UserWrapper.GetCurrentUser().ContactID
BO.BenchMark.InsertBechMarkData(objBenchMark)
Else
objBenchMark.BenchMarkID = Convert.ToInt32(lblBenchMarkID.Text.ToString())
objBenchMark.ModifiedByUserId = UserWrapper.GetCurrentUser().ContactID
BO.BenchMark.UpdateBechMarkData(objBenchMark)
End If
Next i
End If
lblError.Visible = True
lblError.Text = "<a cssClass=""messageGood"">Benchmark data has been saved sucessfully.</a>"
Catch ex As Exception
lblError.Text = ex.Message
lblError.Visible = True
End Try
End Sub
Your data is not being saved, but the message telling you the data has been saved is being presented, despite it's saved or not.
Move thses lines inside the if-end if:
lblError.Visible = True
lblError.Text = "<a cssClass=""messageGood"">Benchmark data has been saved sucessfully.</a>"

Select from table Syntax with multiple paramaters

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