For loop skipping rows in DataSet - vb.net

My Application uses For...Next loops to read a spreadsheet into a DataSet and then display information from it based on the results of search conditions (search term and a date range).
I'm having a problem with the data where, if I run a search that should return the first 400 rows in the spreadsheet, I'm only getting around 200 results. I know the search should return the 400 rows because I checked it in the spreadsheet before running the search.
I think my problem might be caused by my date comparisons. I think the problem might be that I'm comparing String value, so if anyone can show me a more efficient way of comparing dates, then that'll be great.
Here's my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ListBox1.Items.Count <> 0 Then : ListBox1.Items.Clear() : End If
Dim j As Integer = 0
If TextBox1.Text.Length = 4 Then
For i As Integer = 0 To CallData.Tables(0).Rows.Count - 1
'MsgBox(CallData.Tables(0).Rows(i)(2) & " - FROM( " & DateTimePicker1.ToString & " ) TO( " & DateTimePicker2.ToString & " )")
If CallData.Tables(0).Rows(i)(3).ToString = TextBox1.Text _
And CallData.Tables(0).Rows(i)(2).ToString > DateTimePicker1.Value.ToString _
And CallData.Tables(0).Rows(i)(2).ToString < DateTimePicker2.Value.ToString Then
ListBox1.BeginUpdate()
ListBox1.Items.Add(CallData.Tables(0).Rows(i)(2).ToString)
ListBox1.EndUpdate()
j = j + 1
End If
Next
Label1.Text = j & " records found."
End If
End Sub

When you compare strings to each other it doesn't have anything to do with dates, it compares them alphabetically.
You can just cast the object from the row to a date and then compare.
Change:
CallData.Tables(0).Rows(i)(2).ToString > DateTimePicker1.Value.ToString
CallData.Tables(0).Rows(i)(2).ToString < DateTimePicker2.Value.ToString
to:
CType(CallData.Tables(0).Rows(i)(2), DateTime) > DateTimePicker1.Value
CType(CallData.Tables(0).Rows(i)(2), DateTime) < DateTimePicker2.Value
Btw, I'm no vb expert but I guess this code should do the trick :-)

Related

How do I pull the whole XML tag when comparing richtextboxes?

I have a program that loads xml files into richtextboxes and is supposed to compare them. The differences between the two files is then spit out into a third richtextbox. I used the method shown in this SO post from 2014. It works as far as loading the difference between the files into the third textbox. However, I was wondering if anyone could help me get the entire related XML tag to show instead of just the single line.
I would like the whole XML tag to show instead of just the one line highlighted here.
This is the result I'm getting of the comparison
This is the code I used from the above mentioned post.
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim txt1(RichTextBox1.Text.Split(" ").Length) As String
Dim txt2(RichTextBox3.Text.Split(" ").Length) As String
txt1 = RichTextBox1.Text.Split(" ")
txt2 = RichTextBox3.Text.Split(" ")
Dim diff1 As String = "" 'Differences between 1 and 3
Dim diff2 As String = "" 'Differences between 3 and 1
For Each diff As String In txt1
If Array.IndexOf(txt2, diff.ToString) = -1 Then
diff1 += diff.ToString & " "
End If
Next
For Each diff As String In txt2
If Array.IndexOf(txt1, diff.ToString) = -1 Then
diff2 += diff.ToString & " "
End If
Next
RichTextBox4.Text = diff1 & diff2
End Sub

Same number is generated in RNG in VB.NET

I am attempting to make a random number generator in VB 16 in Visual Studio, but every time I run this I keep getting 71, I've tried making it public, sharing it and several other things, but it won't work. I am trying to make a program that has the user guess a randomly generated number, and continue guessing until they get it, but for some reason the exact same number is chosen each time. It won't work properly specifically in window app forms. How do I get a random number each time?
Public Shared Randomize()
Dim value As Integer = CInt(Int((100 * Rnd()) + 1))
Public Sub EnterBtn_Click(sender As Object, e As EventArgs) Handles EnterBtn.Click
Dim entervalue As String = EnterTxt.Text
Dim chances As Integer
Select Case entervalue
Case > value
ResTxt.Text = "Too big"
chances += 1
Case < value
ResTxt.Text = "Too small"
chances += 1
Case = value
ResTxt.Text = "Well done, you got it in " & chances & " tries"
End Select
End Sub
You were close! Here's a working example modifying your original logic:
Private random As Random = New Random()
Private value As Integer
Private chances As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
value = random.Next(1, 100)
chances = 0
End Sub
Private Sub EnterBtn_Click(sender As Object, e As EventArgs) Handles EnterBtn.Click
Select Case EnterTxt.Text
Case > value
chances += 1
ResTxt.Text = "Too big"
Case < value
chances += 1
ResTxt.Text = "Too small"
Case = value
chances += 1
ResTxt.Text = "Well done, you got it in " & chances & " tries"
'and reset for next attempt
value = random.Next(1, 100)
chances = 0
End Select
End Sub
Since your code is not correct it is hard to pinpoint the problem. It is also not clear what the code is supposed to do.
Try this
Private Shared PRNG As New Random ' add this
value = PRNG.Next(1, 101)'this will set value to a random number between 1 and 100 inclusive
Here's some skeleton code for you:
Dim rnd As New Random()
For i as Integer = 0 to 10
Console.WriteLine("{0,15:N0}", rnd.Next())
Next
Notice the rnd.Next() thing. Hope it helps.

Adding 1 everytime i use button vb.net

Basically, I need 2 make a student number and after every new entry I put the student number should +1 so 20182(or 3 if male)001 will be 20182(or 3 if male)002 after I push the button and it must keep +1 but once it reaches the 10th registered student the format changes to 20182(3 if male)010.
I have done everything but make the number +1 every time I use the button
so basically the answer must be:
Student Number is 20182001
Surname , Name
contact details
but, I done everything besides the 001, 002,003 till 010 part so if anybody could help I would be thankful
Public Class Form1
Public Number As Integer = 2018000
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strSurname As String
Dim strFullName As String
Dim strContact As String
Dim strGender As String
Dim x As Integer
'IF statement'
If Me.cboGender.SelectedItem = "Female" Then
Number = 2018300
Else
End If
If Me.cboGender.SelectedItem = "Male" Then
Number = 2018200
Else
End If
'Finding The Student Number'
Dim i As Integer = 0
Do While (i < 1)
i = i + 1
Loop
If i = 201820010 Then
Number = 201800
Else
If i = 201830010 Then
Number = 201800
End if
End If
'Add Items To ListBox'
ListBox1.Items.Add("Student number: " & Number & i)
ListBox1.Items.Add(txtSurname.Text & " , " & txtFullName.Text)
ListBox1.Items.Add(txtContact.Text)
ListBox1.Items.Add("============================================")
End Sub
End Class
Not sure what your code was doing, but based on your requirements:
Public baseFemale As Integer = 20182000
Public baseMale As Integer = 20183000
Public autoNumber As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Number as Integer;
autoNumber = autoNumber + 1
If Me.cboGender.SelectedItem = "Female" Then
Number = baseFemale + autoNumber
Else
Number = baseMale + autoNumber
Else
'Add Items To ListBox'
ListBox1.Items.Add("Student number: " & Number & i)
ListBox1.Items.Add(txtSurname.Text & " , " & txtFullName.Text)
ListBox1.Items.Add(txtContact.Text)
ListBox1.Items.Add("============================================")
End Sub
Additionally you may want to check for autoNumber exceeding 999 - but I leave that as an exercise for you.

Detect the sign in number input

how can i detect if the input number in textbox contains "-"
so i can change the output into positive number and
if not contain "-" the output number is become negative
i'm using Vb.net 2010 tnx in advance for who wants to help
Dim output As String
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If getbyte(TextBox1.Text, 0) = Chr(45) Then ' check whether the first character is - or not
output = New String((From c As Char In TextBox1.Text Select c Where Char.IsDigit(c)).ToArray())
else
output="-" & textbox1.text
End If
msgbox (CInt(output))' will give the number
End Sub
Getbyte function to take each character from a string based on the position
Private Function getbyte(ByVal s As String, ByVal place As Integer) As String
If place < Len(s) Then
place = place + 1
getbyte = Mid(s, place, 1)
Else
getbyte = ""
End If
End Function
if you want to convert the -eve number to positive for calculation you can use
You have couple of options, two of them are:
1) Use StartsWith functions:
If Textbox1.Text.Trim().StartsWith("-"))Then
' It is a negative number
End If
2) If you just need to toggle the number sign, then:
Dim number as Integer = Integer.Parse(Textbox1.Text) ' Preferrably use Integer.TryParse()
number *= -1 ' Toggle the number sign
Using option2, i get:
Dim txta1 As New TextBox
txta1.Text = "-2"
Dim number As Double = Double.Parse(txta1.Text) ' Preferrably use Integer.TryParse()
number *= -1 ' Toggle the number sign
Dim s As String = number & " | "
Output: 2 |

For Loops And Arrays

I need a little help. My code is work 99% correctly except for one little thing.
I'm making what's called "Cafeteria Survey" which tallies responses from a ComboBox, which the user inputs them self.
The issue here is that it tallies (places a *) the number 1 less than the number I chose in the ComboBox.
If I add + 1 on the end of SelectedIndex it places the * with the correct number, but it doesn't do it for #10
responses(ratingComboBox.SelectedIndex) += 1
Any help would be fantastic. Thanks in advance.
Here's my code:
Public Class CafeteriaSurveyForm
Dim choices As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim responses(0 To 11) As Integer
Dim responseCounter As Integer = 0
' displays histogram
Sub DisplayHistogram()
outputTextBox.Text = ("Rating" & vbTab & "Frequency")
For i As Integer = 0 To choices.GetUpperBound(0)
For ii As Integer = 1 To responses(i)
outputTextBox.Text &= ("*")
Next
outputTextBox.Text &= (vbNewLine & choices(i) & vbTab)
Next
End Sub ' DisplayHistogram
Private Sub CafeteriaSurveyForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ratingComboBox.DataSource = choices
End Sub
Private Sub submitButton_Click(sender As System.Object, e As System.EventArgs) Handles submitButton.Click
responseCounter += 1
responses(ratingComboBox.SelectedIndex) += 1
DisplayHistogram()
End Sub
End Class ' CafeteriaSurveyForm
Your display function is a little backward. Here is what you have now:
For i As Integer = 0 To choices.GetUpperBound(0)
For ii As Integer = 1 To responses(i)
outputTextBox.Text &= ("*")
Next
outputTextBox.Text &= (vbNewLine & choices(i) & vbTab)
Next
For every loop it is writing the asterisks to the previous line (because the new line is done after). If you increase the selected index it wrote them in the correct location but never got to write the asterisks for #10 because it exited the for loop before it got a chance.
This is what it should be:
For i As Integer = 0 To choices.GetUpperBound(0)
outputTextBox.Text &= (vbNewLine & choices(i) & vbTab)
For ii As Integer = 1 To responses(i)
outputTextBox.Text &= ("*")
Next
Next
Or even
For i As Integer = 0 To choices.GetUpperBound(0)
outputTextBox.Text &= vbNewLine & choices(i) & vbTab & New String("*", responses(i))
Next
Now the choices and responses arrays are synced using the same indexes and are accessed during the same loop iteration.
Maybe the error is that Responses(i) is defined to be integers in the range 1 to 11. If that range isn't inclusive that when you increment Responses(i) by 1 for the response 10 you will fall out of that range.
Try this out to see if it gives you the expected output.
Remove the responseCounter variable. It doesn't do anything significant (not any I see anyway).
Change the responses declaration to Dim responses(9) As Integer.
VB.NET uses zero (0) as the lower bound of an array (and almost every other type of collection) so the statement will create an array of integer with ten (10) elements.
Change the For ii As Integer = 1 to responses(i) loop block to outputTextBox.Text &= New String('*', responses(i))