SSRSS trying to see if the results of a LookUpSet are in a string - vb.net

I'm trying to list the results of a LookUpSet only if they are in another string.
Initially I used the following to get an array of strings seperated by a semi-colon.
Join(LookUpSet(...),";")
The next stage was to test if the strings were in the main string. I tried nesting the return value inside an IIF statement;
Join(LookUpSet(...,IIF(InStr(Fields!BigText.Value,Fields!Text.Value) > 0, Fields!Text.Value, Nothing),...),";")
I understand this doesn't work because the big string that I am comparing against is in my first dataset and the other is in my second dataset
Next I tried to use InStr on the results of the LookUpSet;
IIF(InStr(Fields!BigText.Value, LookUpSet()) > 0, LookUpSet(), Nothing)
This won't work either so I've tried to create custom code and am nearly there;
Code.CheckSetInString(Fields!BigText.Value, LookUpSet())
The code is below, when debugging I receive "Object reference not set to an instance of an object", but can't see anything that I've failed to declare.
How should I troubleshoot which object isn't declared?
The custom code is below for reference.
Function CheckSetInString(ByVal str As String, ByVal setofstrings As Object())
If setofstrings Is Nothing OrElse Not setofstrings.length > 0 Then
Return Nothing
End If
Dim returnedstrings AS [String]()
Dim i As Integer = 0
For Each item As String In setofstrings
If InStr(1, str, item, 1) > 0 Then
returnedstrings(i) = item
i += 1
End If
Next
Return returnedstrings
End Function
Any advice is appreciated,
Dan

Related

I keep getting "true" statement for the first match of email and the rest all are "False"

I am currently doing a program that will detect Email in a text file, i am able to bring out the email, but couldnt validate the email. The validation code is something look like this:
Dim truelist As New ArrayList
For i As Integer = 0 To ListBox2.Items.Count - 1
truelist.Add(ListBox2.Items(i))
Next
For Each item In truelist
Dim result As Boolean
Dim rgx As New Regex("^[_a-z0-9-]+(.[_a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,4})$")
If rgx.IsMatch(item) Then
MessageBox.Show(item, "true")
result = True
Else
MessageBox.Show(item, "false")
result = False
End If
Next
and my sample input is :
ys_host#hotmail.com
.kjdsd.#hotmail.com
.as.das.#hotmail.com
~!U#)(!#U#hotmail.com
idgohwoijgw12942149!#hotmail.com
ys_host#hotmail.com
Even though my 1st input the same with last input, but i will get false return from the last input
Your regex does not account for any space, and evaluates the characters from the start of the input (^) to the end ($). As such, any leading or trailing spaces in the input strings would fail to be matched.
Trim() is the VB command that removes spaces at the end or beginning of strings. LTrim and RTrim are specific commands that remove spaces at either end. Source: https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.trim(v=vs.110).aspx; while https://learn.microsoft.com/en-us/dotnet/standard/base-types/trimming indicates that String.Trim() works on whitespaces in general.
You could Trim() the inputs to your ListBox and this should solve the problem. The safer and better way in this context is to Trim() the inputs to the Regex - data validation at the most effective time in the code. The sample code (based on your code) would be:
Dim truelist As New ArrayList
For i As Integer = 0 To ListBox2.Items.Count - 1
truelist.Add(Trim(ListBox2.Items(i))) '*** Changed this line, added "Trim"
Next
For Each item In truelist
Dim result As Boolean
Dim rgx As New Regex("^[_a-z0-9-]+(.[_a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,4})$")
If rgx.IsMatch(item) Then
MessageBox.Show(item, "true")
result = True
Else
MessageBox.Show(item, "false")
result = False
End If
Next
The alternative syntax is truelist.Add(ListBox2.Items(i).Trim()).
As an aside, this code is ripe for setting up in a function - enabling re-use. Here is an example of the refactoring:
'[Main function elements ... Include Sub/Function header etc.]
Dim truelist As New ArrayList
For i As Integer = 0 To ListBox2.Items.Count - 1
truelist.Add(Trim(ListBox2.Items(i))) '*** Changed this line, added "Trim"
Next
For Each item In truelist
Dim result As Boolean
If IsValidEmailAddress(item) Then
' Do valid stuff
Else
' Do not-valid stuff
End If
Next
'[Main Sub/Function tail, include End Sub/Function as relevant]
Function IsValidEmailAddress(value as String) as Boolean
Dim rgx As New Regex("^[_a-z0-9-]+(.[_a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,4})$")
return rgx.IsMatch(item.Trim()) ' Note use of Trim here for encapsulated data validation.
' encapsulated data validation may or may not be what you want.
End Function

Searching Strings from a split list

Attempting to split and store strings from a listbox and search then search the contents of the text file I have stored them to, hopefully sorting them in different categories,
firstly I am getting an "Object reference not set to an instance of an object." error with this
Dim variable As String = Nothing
If listArray.SelectedIndex > 0 Then
variable = listArray.Items(listArray.SelectedIndex)
End If
Dim part As String() = variable.Split(New Char() {","c})
Dim line As String
For Each line In part
MessageBox.Show(line)
and secondly, would this be the right code to use for searching those separated strings?
For count As Integer = 0 To Logbook.listArray.Items.Count - 1
Dim searchIndex As String = Logbook.listArray.Items(count).ToString
If searchIndex.Contains(indexSearch.Text) Then
Logbook.listArray.SetSelected(0, True)
End If
Next
I'm pretty new to StackOverflow, my apologies if i'm not up to date with the website etiquette.
am getting an "Object reference not set to an instance of an object."
I guess you don't know that the first item is at index 0 and that SelectedIndex returns -1 if there is no item selected. That's why following code throws that exception:
Dim variable As String = Nothing
If listArray.SelectedIndex > 0 Then
variable = listArray.Items(listArray.SelectedIndex)
End If
Dim part As String() = variable.Split(New Char() {","c}) ' <--- variable Nothing if first item selected
Then you just have to use <> -1 or >= 0:
Dim variable As String = Nothing
If listArray.SelectedIndex >= 0 Then
variable = listArray.Items(listArray.SelectedIndex)
End If
According to the second part of the question(always ask only once), you haven't provided enough informations to understand what you want and what doesn't work with your code.

Object required error '424' when iterating over Collection

When I run my function, I get the following error:
Run-Time error '424': Object Required
And it highlights the following line of code:
For Each n In DataSet
Here is my full code:
'==============================|Find_Occurrences FUNCTION|=============================='
' Given a collection and a search value, return the number of occurrences of the search
' value in the collection, as an Integer. If it does not exist, return the integer 0
Function Find_Occurrences(DataSet As Collection, What As Variant) As Integer
Dim Count As Integer
Count = 0
Dim n As Variant
For Each n In DataSet ''''''''''''''''''Object required error'''''''''''''''''
'MsgBox (TypeName(What))
If (TypeName(n) = TypeName(What)) And (n = What) Then
Count = Count + 1
End If
Next n
Find_Occurrences = Count
End Function
Does anybody know what could be causing this, whether in my code or outside of my code?
The error was actually outside of the function, when the function is called. Instead of passing in a collection for DataSet, it passes in nothing.

Make ListView FindItemWithText search anywhere in text

Dim query as String = "Card" ' Just an example, I would call this with a function
Dim itemF As ListViewItem = ListView1.FindItemWithText(query, False, 0, True)
If (itemF IsNot Nothing) Then
itemF.Selected = True
Else
Alert("Nothing was found")
End If
So I am trying to use this code to add a search functionality. The problem is, this works if I'm trying to go from the beginning to the end. But if I wanted to search Card and find W_Card_Brt_Better, I won't get expected results. Though if I search, W_Card, the item will be selected.
ListView.FindItemWithText always searches using BeginsWith pattern - this is by design, as specified in the docs:
ListView.FindItemWithText Method (String) # MSDN
Finds the first ListViewItem that begins with the specified text value.
If you are trying to do anything more custom than that, I suggest you search against the underlying data source instead. You should generally avoid performing any kind of business logic/validation from the UI control directly.
In the business object, you can do a simple for loop, or build a dictionary, to make it search faster, and encapsulate this search functionality within a method or property, which you will call from the UI layer.
FindItemWithText finds the first ListViewItem ... that begins with the specified text value. If you are looking for Items/SubItems which simply CONTAINS the text you have to iterate and search yourself.
To make it like FindItemWithText, specify a start point and Bool for the SubItem search:
Function FindItemContainingText(lv As ListView, searchSubs As Boolean,
StartIndex As Integer,
searchTarget As String) As ListViewItem
If (StartIndex >= lv.Items.Count) Or (StartIndex < 0) Then
Return Nothing
End If
Dim FindMe As String = searchTarget.ToLowerInvariant
Dim subStopper As Integer = 0
For i As Integer = StartIndex To lv.Items.Count - 1
If searchSubs Then
subStopper = lv.Items(i).SubItems.Count - 1
Else
' Item.Text/Label can be read from SubItem(0)
' just look at it if not searching subI's
subStopper = 0
End If
For n As Integer = 0 To subStopper
If lv.Items(i).SubItems(n).Text.ToLowerInvariant.Contains(FindMe) Then
' return the LVI that contains the text
Return lv.Items(i)
End If
Next n
Next
' note!
Return Nothing
End Function

DataRow.SetColumnError(Int32, String) ignores Int32 value and always uses zero

Okay, this is doing my head in. I'm calling SetColumnError() on a DataRow object that has 20 columns, but no matter which ColumnIndex I use it sets the error text on column 0. The MSDN documentation makes it plainly clear that the error text is supposed to be set on the column that the ColumnIndex provides.
I'm trying to set error text on columns 1 and 2 (I normally use constants when doing this, I have just used the integers in this example code for simplicity). Why does the error text appear on column 0 and what should I be doing to get the text to show on columns 1 and 2? I'm not receiving IndexOutOfRangeException.
Here is the code I'm having trouble with.
Public Sub ValidateRows()
For Each dgvRow As DataGridViewRow In Me.DataGridView1.Rows
If dgvRow.DataBoundItem IsNot Nothing Then
Dim rowView As DataRowView = dgvRow.DataBoundItem
Dim rowData As MyDataSet.DocumentRow = rowView.Row
rowData.ClearErrors()
If rowData.Revision = rowData.Revision_old Then
rowData.SetColumnError(1, "You must change the revision")
End If
If rowData.InternalRevision = rowData.InternalRevision_old Then
rowData.SetColumnError(2, "You must change the internal revision")
End If
End If
Next
End Sub
I'm not sure, but I think the number of the column must be of the type "DataColumn".