How to use IIF command in code in rdl rdlc viewer? - rdlc

So I previously found this post
How to check multiple conditions in rdlc expression
which had this code inside:
Public Function GetGender(ByVal val as String) As String
Dim retVal as String = ""
If(val = "1")
retVal = "Male"
Else If (val = "2")
retVal = "???"
Else If (val = "3")
retVal = "???"
Else
retVal = "???"
End If
Return retVal
End Function
This code works, but instead of (val="1") I need something like (val = "th*") that it searches and replaces anything that contains th, for example

Looks like VB. Try val.StartsWith("th") – Caius Jard
This was the answer, thank you a lot Caius.!!
I am new to stackoverflow so I'm still learning how to use this site.

Related

Is there a neat and clean way to handle nulls with Yields?

While CommitReader.Read()
Yield New Commit() With {
.FirstValue = CommitReader.GetInt32(CommitReader.GetOrdinal("FirstValue")),
.SecondValue = CommitReader.GetString(CommitReader.GetOrdinal("SecondValue")).Trim(),
'Lots of values
End While
I know I can do something like this; however there are 24 properties and I would like to make this part as clean as possible
While CommitReader.Read()
new Commit (){
Dim index As Integer = reader.GetOrdinal("FirstValue")
If reader.IsDBNull(index) Then
FirstValue = String.Empty
Else
FirstValue = reader(index)
End If
index = reader.GetOrdinal("SecondValue")
If reader.IsDBNull(index) Then
SecondValue = String.Empty
Else
SecondValue = reader(index)
End If
}
End While
Is there a better way to handle this type of thing? I am mainly a C# developer so if the syntax is off a little sorry, I am winging it in VB.
It's a shame that SqlDataReader doesn't have the generic Field extension method like DataRow does, but you could define your own extension method (has to be in a module in VB.NET) to help with the null checks, perhaps something like this:
<Extension>
Function GetValue(Of T)(rdr As SqlDataReader, i As Integer) As T
If rdr.IsDBNull(i) Then
Return Nothing
End If
Return DirectCast(rdr.GetValue(i), T)
End Function
And use it something like this:
While CommitReader.Read()
Yield New Commit() With {
.FirstValue = CommitReader.GetValue(Of Integer?)(CommitReader.GetOrdinal("FirstValue")),
.SecondValue = CommitReader.GetValue(Of String)(CommitReader.GetOrdinal("SecondValue")),
'Lots of values
End While
I haven't tested this fully to make sure it handles all data types appropriately (may be worth looking at DataRowExtensions.Field to see how it does it).
Note that you are using String.Empty as the "null" value for strings, while this will use Nothing/null (I also had to remove the .Trim call to avoid NREs). If you want empty string instead, you could use (adding the Trim back in):
.SecondValue = If(CommitReader.GetValue(Of String)(CommitReader.GetOrdinal("SecondValue")), String.Empty).Trim()
You may also want to move the GetOrdinal calls out of the loop to improve performance.
Obviously you have repetition in your code if ... else ... condition.
So you can extract it in another method.
For your case generic extension method seems good candidate.
Public Module Extensions
<Extension>
Public Function GetValueOrDefault(Of T)(originalValue As object,
defaultValue As T) As T
If originalValue = DbNull.Value Then
Return defaultValue
End If
return DirectCast(originalValue, T)
End Function
End Module
Then use it:
While CommitReader.Read() = True
Dim temp = new Commit With
{
Dim index As Integer = reader.GetOrdinal("FirstValue")
FirstValue = reader(index).GetValueOrDefault(String.Empty)
Dim index As Integer = reader.GetOrdinal("SecondValue")
FirstValue = reader(index).GetValueOrDefault(String.Empty)
}
End While
You can create another overload which return "default" value for given type if it is DbNull
<Extension>
Public Function GetValueOrDefault(Of T)(originalValue As object) As T
Return originalValue.GetValueOrDefault(Nothing)
End Function
Nothing in vb.net is default value, for reference types it is null for Integer it is 0 for example.
For using this overload you need provide type parameter explicitly
While CommitReader.Read() = True
Dim temp = new Commit With
{
Dim index As Integer = reader.GetOrdinal("FirstValue")
FirstValue = reader(index).GetValueOrDefault(Of String)()
Dim index As Integer = reader.GetOrdinal("SecondValue")
FirstValue = reader(index).GetValueOrDefault(Of String)()
}
End While
Notice that your solution executing reader twice, for checking is it null and for reading value. This can cause "tiny" performance issue.
So in extension method above we read value only once and then check value for DbNull.
If you concatenate a string with a Null you get the string:
FirstValue = reader(index) & ""
Kind of "unprofessional" but saves a lot of coding time if all you are doing is converting a possible Null to an empty string. Easy to forget however, so later data dependent errors may pop up.

.Replace(String,String) VB function not works

I need to replace the end of a pathfile in VB. So I try this code :
Private Function getfiledata(ByVal fichier As String) As String
Dim fileReader As String
Dim FichierFinal As String
MsgBox(fichier)
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
FichierFinal = fichier.Replace("X002.pfx","_X002.pem")
FichierFinal = fichier.Replace("A005.pfx","_A005.pem")
MsgBox(FichierFinal)
fileReader = My.Computer.FileSystem.ReadAllText(FichierFinal)
Return fileReader
End Function
The first MsgBox function return me the following result :
C:/Users/Bruno/Documents/Visual Studio
2010/Projects/SerEbics/SerEbics/bin/Debug/Certificats/512250X002.pfx
But the second return me the same path :
C:/Users/Bruno/Documents/Visual Studio2010/Projects/SerEbics/SerEbics/bin/Debug/Certificats/512250X002.pfx
So I need this result :
C:/Users/Bruno/Documents/Visual Studio2010/Projects/SerEbics/SerEbics/bin/Debug/Certificats/512250_X002.pem
Thank you in advance !
Thomas
The three lines in a row using teh Replace function are not doing quite what you think. Each time, they are setting FichierFinal to something new. So they are not building on each other. Try replacing them with this:
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
FichierFinal = FichierFinal.Replace("X002.pfx","_X002.pem")
FichierFinal = FichierFinal.Replace("A005.pfx","_A005.pem")
You are not doing what you think. At each new line you cancelled the previous one
and defining FichierFinal to a new value.
This will works (plus no need to do this in 3 lines):
FichierFinal = fichier.Replace("E002.pfx","_E002.pem").Replace("X002.pfx","_X002.pem").Replace("A005.pfx","_A005.pem")
This happens because your last Replace restore the original name in the variable FichierFinal. You should execute your replaces only if the file ends with one of the expected strings.
If fichier.EndsWith("E002.pfx") Then
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
Else if fichier.EndsWith("X002.pfx") Then
FichierFinal = fichier.Replace("X002.pfx","_X002.pem")
Else if fichier.EndsWith("A005.pfx") Then
FichierFinal = fichier.Replace("A005.pfx","_A005.pem")
End If
In this way you execute the Replace just one time and not three times. Remember that every time you call Replace a new string is allocated and returned and depending on the context of your calls this could have effects on the performances of your code.
Try this code u wrong replace:
Private Function getfiledata(ByVal fichier As String) As String
Dim fileReader As String
Dim FichierFinal As String
MsgBox(fichier)
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
FichierFinal = FichierFinal.Replace("X002.pfx","_X002.pem")
FichierFinal = FichierFinal.Replace("A005.pfx","_A005.pem")
MsgBox(FichierFinal)
fileReader = My.Computer.FileSystem.ReadAllText(FichierFinal)
Return fileReader
End Function

How do I find a value in an arraylist?

I have an arraylist called arrdirectory. This arraylist contains a structure when query to database.
arrDirectory = New ArrayList
While rdr.Read
With udt_mydir
If Not IsDBNull("dirno") Then
.strdirno = (rdr("dirno"))
Else
.strdirno = "N/A"
End If
If Not IsDBNull("dirname") Then
.strdirname = (rdr("dirname"))
Else
.strdirname = "N/A"
End If
If Not IsDBNull(rdr("dir_image")) Then
.arrImg = rdr("dir_image")
Else
.arrImg = Nothing
End If
If Not IsDBNull(rdr("dir_logo")) Then
.arrLogo = rdr("dir_logo")
Else
.arrLogo = Nothing
End If
End With
arrDirectory.Add(udt_mydir)
End While
How do I find from the arraylist where my string is equal to udt_mydir.strdirname so I can get the whole data strdirno, arrImg and arrLogo?
Why are you using an ArrayList to begin with? Use List<>, its generic counterpart, so you won't need to box/unbox to access its items.
Anyhow, assuming you are on .NET 3.5, finding an element in an IEnumerable is pretty trivial with LINQ's Where(), Single() or SingleOrDefault().
I won't bother figuring out what the syntax is for VB, but here's how it would look in C#, assuming you really want to stick with that horrible ArrayList:
var x = arrDirectory.Cast<YourType>().SingleOrDefault(item => item.strdirname == "my string");

How can I check if filename contains a portion of a string in vb.net

I have a userform in 2008 vb express edition. A part number is created from user input via a concat string. I want to then check if a certain portion of the part number exists in the existing file names in a directory. Below is a more detailed explanation.
This is my code for creating a part number from the user input on the form.
L_PartNo.Text = String.Concat(CB_Type.Text, CB_Face.Text, "(", T_Width.Text, "x", T_Height.Text, ")", mount, T_Qty.Text, weep, serv)
I then have the following code to tell the user if the configuration (part no) they just created exists
L_Found.Visible = True
If File.Exists("Z:\Cut Sheets\TCS Products\BLANK OUT SIGN\" & (L_PartNo.Text) & ".pdf") Then
L_Found.Text = "This configuration exists"
Else
L_Found.Text = "This configuration does NOT exist"
End If
This is where I need help. The part no will look like this BX002(30x30)A1SS I want to compare 002(30x30) (just this part of the file name) to all the files in one directory. I want a yes or no answer to the existance and not a list of all matching files. The code below is everything I've tried, not all at the same time.
Dim b As Boolean
b = L_PartNo.Text.Contains(NewFace)
Dim NewFace As String = String.Concat(CB_Face.Text, "(", T_Width.Text, "x", T_Height.Text, ")")
Dim NewFace = L_PartNo.Text.Substring(2, 10)
If filename.Contains(NewFace) Then
lNewFace.Visible = False
Else
lNewFace.Visible = True
End If
The code below was a translation from the answer in C# but it does not work either
Dim contains As Boolean = Directory.EnumerateFiles(path).Any(Function(f) [String].Equals(f, "myfilethree", StringComparison.OrdinalIgnoreCase))
Here's an example of how you can do it without the fancy LINQ and Lambda which seem to be confusing you:
Public Function FileMatches(folderPath As String, filePattern As String, phrase As String) As Boolean
For Each fileName As String In Directory.GetFiles(folderPath, filePattern)
If fileName.Contains(phrase) Then
Return True
End If
Next
Return False
End Function
Or, if you need it to be case insensitive:
Public Function FileMatches(folderPath As String, filePattern As String, phrase As String) As Boolean
For Each fileName As String In Directory.GetFiles(folderPath, filePattern)
If fileName.ToLower().Contains(phrase.ToLower()) Then
Return True
End If
Next
Return False
End Function
You would call the method like this:
lNewFace.Visible = FileMatches(path, "*.pdf", NewFace)
Try this:
lNewFace.Visible = IO.Directory.GetFiles(path, "*.pdf").Where(Function(file) file. _
Substring(2, 10) = NewFace).FirstOrDefault Is Nothing
Consider that the substring function will throw an exception if its arguments exceed the length of the string it is parsing

Trim last 4 characters from string?

How can I trim MyString to be MyStr?
Thanks, google failed again :(
YourString.Left(YourString.Length-4)
or:
YourString.Substring(0,YourString.Length-4)
Rob's answer is mostly correct but the SubString solution will fail whenever the string has less than 4 characters in it. If the length goes past the end of the string an exception will be thrown. The following fixes that issue
Public Function TrimRight4Characters(ByVal str As String) As String
If 4 > str.Length Then
return str.SubString(4, str.Length-4)
Else
return str
End if
End Function
c#
string str = "MyString";
Console.WriteLine(str.Substring(0, str.Length - 3));
vb.net
dim str as string = "MyString"
Console.WriteLine(str.Substring(0, str.Length - 3))
vb.net (with VB6 style functions)
dim str as string = "MyString"
Console.WriteLine(Mid(str, 1, len(str) - 3))
This is what I used in my program (VB.NET):
Public Function TrimStr(str As String, charsToRemove As String)
If str.EndsWith(charsToRemove) Then
Return str.Substring(0, str.Length - charsToRemove.Length)
Else
Return str
End If
End Function
Usage:
Dim myStr As String = "hello world"
myStr = TrimStr(myStr, " world")
This is my first answer. Hope it helps someone. Feel free to downvote if you don't like this answer.