How to send more than one value to single parameter? - vb.net

I'm trying to send three string "huis" to crystal report parameter
he only work if i select one checkbox
i wanna crystal report give me what is selected on checkbox(huis)
select * from Mess22 where Cont in {?#huis}
Dim huis As String
If CheckBox1.Checked = True Then
huis = CheckBox1.Text
End If
If CheckBox2.Checked = True Then
If huis = "" Then
huis = CheckBox2.Text
Else
huis = huis & "," & CheckBox2.Text
End If
End If
Aver5.Load("Avert5.rpt")
Aver5.SetParameterValue("#huis", huis.ToString)
AvF.CR2.ReportSource = Aver5
AvF.ShowDialog()

Change the record selection formula to:
Cont in Split({?#huis}, ",")
This would turn the string into an array for comparison.
The other option is to design the parameter as a multi-value parameter and use the API to add the values one by one.

Related

how to check if dataset contains specific value in VB.net

I have a dataset that contains multiple values. I want to take those rows from that dataset that contains "the specific value" and firstly I want to display those in a MessageBox.
Furtheron, I try to view them in a datagridview called ErrorsDgV.
I already searched this topic and found a good function, but unfortunately, all I get from the MessageBox is an empty box.
ErrorsDgV.DataSource = Srchdataset.Tables("blubb")
LineLabel.Text = "Lines: " &
Srchdataset.Tables("blubb").Rows.Count.ToString
ErrorsDgV.Sort(ErrorsDgV.Columns(1), System.ComponentModel.ListSortDirection.Ascending)
ErrorsDgV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
ErrorsDgV.Columns(1).DefaultCellStyle.Format = "dd/MM/yyyy HH:mm:ss.fff"
Dim answer As String = ""
Dim SearchRows() As Data.DataRow
SearchRows = Srchdataset.Tables("blubb").Select("Data = 'the specific value'")
answer = ""
For k As Integer = 0 To SearchRows.Length - 1
If answer = "" Then
answer = SearchRows(k).Item("Data")
Else
answer = answer & vbNewLine & SearchRows(k).Item("Data")
End If
Next
MsgBox(" " & answer)
I debugged also and got to know that SearchRows is empty, even if the specific value is inlcuded in that DataSet.

Extracting the First (Oldest) Value from Dataset Based on Column Value

I don't have a great deal of experience working with DataSets and haven't been able to find the best way of achieving what I want to achieve.
I basically create a DataSet using a SQL Query and then I am trying to find a Specific Value in the 'Field' column and then if there is a 'Y' in the 'Flag' (as apposed to a 'N') Column on the same Row then I want it to change a check box's state to Checked as well as updating a labels text.
What I have seems to work however if no data is returned I get the below error:
Object reference not set to an instance of an object
If I change the code slightly from .FirstOrDefault() to .First() I get this error:
Sequence contains no elements
The part of the code that appears to be causing the problem is listed below. If you need to know anything else I will add it in.
Dim sSQL As String
sSQL =
<SQL>
SELECT MAX(UpdateTime) AS UpdateTime FROM AdminCS_Data_Current
WHERE UpdateUser = |##UpdateUser|
</SQL>
sSQL = Replace(sSQL, "##UpdateUser", AdminCB.Text)
Me.LastUserUpdate.Text = "Last Action: " & Format(ReturnDatabaseValue(sSQL, "UpdateTime", "Data"), "dd/MM/yyyy HH:mm:ss")
Dim EmployeeDataset As New DataSet
Try
sSQL =
<SQL>
SELECT * FROM AdminCS_Data_Current
WHERE UpdateUser = |##UpdateUser| AND CONVERT(DATE, UpdateTime) = CAST(GETDATE() AS DATE)
ORDER BY UpdateTime ASC
</SQL>
sSQL = Replace(sSQL, "##UpdateUser", AdminCB.Text)
EmployeeDataset = ReturnDataSet(sSQL, "Data")
If EmployeeDataset IsNot Nothing Then
Dim eData = EmployeeDataset.Tables(0)
If (eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()("Flag")) IsNot Nothing Then
If eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()("Flag").ToString.Trim = "Y" Then
TShtY.CheckState = CheckState.Checked
TShtTime.Text = Format(eData.Select("Field = 'Timesheets Checked'").First()("UpdateTime"), "HH:mm:ss")
Else
TShtN.CheckState = CheckState.Checked
End If
End If
' The above two IF statements would be repeated several times on each change of "Field"
End If
It would appear that this code has introduced not just iunefficiency but also a bug:
If (eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()("Flag")) IsNot Nothing Then
If eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()("Flag").ToString.Trim = "Y" Then
TShtY.CheckState = CheckState.Checked
TShtTime.Text = Format(eData.Select("Field = 'Timesheets Checked'").First()("UpdateTime"), "HH:mm:ss")
Else
TShtN.CheckState = CheckState.Checked
End If
End If
It should have been written like this in the first place:
Dim row = eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()
If row IsNot Nothing Then
If row("Flag").ToString.Trim = "Y" Then
TShtY.CheckState = CheckState.Checked
TShtTime.Text = Format(row("UpdateTime"), "HH:mm:ss")
Else
TShtN.CheckState = CheckState.Checked
End If
End If
Easier to read, more efficient and avoids that nasty bug.
Also, I'd much rather see this:
Dim row = eData.Select("Field = 'Timesheets Checked'").FirstOrDefault()
If row IsNot Nothing Then
If row("Flag").ToString.Trim = "Y" Then
TShtY.Checked = True
TShtTime.Text = CDate(row("UpdateTime").ToString("HH:mm:ss")
Else
TShtN.Checked = True
End If
End If
You should never use the CheckState of a Checkbox unless it's tri-state, which maybe yours are but I doubt it. As for Format, we're not in VB6 anymore Toto.

Adding to the same string

I have a variable install = "6 " and I need to add it, like this:
If CheckBox6.Checked = True Then
install = &"6 "
Else
If CheckBox7.Checked = True Then
install = &"7 "
End If
End If
I need the output to be "6 7".
If you want to add another string to your variable you need to use the & or + opperator, but you need to specify what you want to add and where you want to add (the new string to).
Here is an example:
Dim myString as String
myString = "Hello" 'You variable now holds the string "Hello"
myString = myString & " World!" 'Your variable now holds the string "Hello World!"
MessageBox.Show(myString) 'Will show a message box with the text "Hello World!"
However, you also have a second problem. Since the concatenation is being done in an If/Else block, only one or the other will ever get executed. In order to execute both in succession, you need move the second concatenation out of the Else and put it into its own If block:
If CheckBox1.Checked Then
myString = myString & "Hello "
End If
If CheckBox2.Checked Then
myString = myString & "World! "
End If
MesssageBox.Show(myString) 'Shows the text "Hello World!" if both are checked
I think you need separate IF logic:
If CheckBox6.Checked = True Then
install = &"6 "
End If
'Else <--- Comment else
If CheckBox7.Checked = True Then
install = &"7 "
End If
End If
So If both check boxes are checked you will get "6 7".

Linq Dynamic Error with Dynamic Where clause

I have a four dropdownlists that are used to filter/order a gridview.
If any of the first 3 dropdowns have a selected value other than 0 the the WhereFlag is set to True. The fourth dropdown is used to dictate which column the grid is ordered by.
My code for databinding the gridview uses System.Linq.Dynamic and is shown below...
Dim dc As New DiaryDataContext
If WhereFlag = False Then
'This section works...
Dim AList = dc.D_AreaSubAreas _
.OrderBy(ddl_SortBy.SelectedValue)
Dim dl As List(Of D_AreaSubArea)
dl = AList.ToList
dl.Insert(0, New D_AreaSubArea With {.Ref = 0,
.Area = "",
.SubArea = "",
.Allocation = "Allocation...",
.Redundant = False})
gv_AreaSubArea.DataSource = dl
gv_AreaSubArea.DataBind()
'Gridview successfully binds
'If ddl_SortBy value is changed... Gridview binds OK.
Else
'This section gives error...
Dim WhereBuild As New StringBuilder
If ddl_AreaFilter.SelectedIndex <> 0 Then
WhereBuild.Append("Area = '" & ddl_AreaFilter.SelectedValue & "'")
AndFlag = True
End If
If ddl_SubAreaFilter.SelectedIndex <> 0 Then
If AndFlag = True Then
WhereBuild.Append(" AND ")
End If
WhereBuild.Append("SubArea = '" & ddl_SubAreaFilter.SelectedValue & "'")
AndFlag = True
End If
If ddl_AllocFilter.SelectedIndex <> 0 Then
If AndFlag = True Then
WhereBuild.Append(" AND ")
End If
WhereBuild.Append("Allocation = '" & ddl_AllocFilter.SelectedValue & "'")
End If
'ERROR HERE
Dim AList = dc.D_AreaSubAreas _
.Where(WhereBuild.ToString) _
.OrderBy(ddl_SortBy.SelectedValue)
'END ERROR
Dim dl As List(Of D_AreaSubArea)
dl = AList.ToList
dl.Insert(0, New D_AreaSubArea With {.Ref = 0,
.Area = "",
.SubArea = "",
.Allocation = "Allocation...",
.Redundant = False})
gv_AreaSubArea.DataSource = dl
gv_AreaSubArea.DataBind()
End If
The error I get is with the dynamic where clause. The error I get is
ParseException is unhandled by user code. Character Literal must contain exactly one character
It points to the query AList in the Else fork. The only difference between it and the query in the If fork is the addition of the Where clause... but I have been unable to deduce what is wrong with my code. AHA.
The error points to the place where the LINQ query is evaluated and, as the message says, the problem is that a character is expected but several characters were provided.
Check whenever ddl_AreaFilter.SelectedValue, ddl_SubAreaFilter.SelectedValue or ddl_AllocFilter.SelectedValue actually contain a character or a string. If they contain more than one character, you should replace ' with \" when building the where condition, for instance:
WhereBuild.Append("Area = """ & ddl_AreaFilter.SelectedValue & """")
EDIT
You must make sure that the type of the value contained in each SelectedValue string match the corresponding database type. For instance, if the database column is a numeric type, the string content will be casted to a numeric type.
When you specify the value in the comparison using quotes, you are indicating that the type on the right side of the comparison is either character or string (depending on whenever you use single or double quotes, respectively).
So, what are the Area, SubArea and Allocation types in the database?
Single character: the value in your query should be around single quotes: Area = 'value'
Strings (for instance, varchar):you must use double quotes: Area = "value"
Other: then you should use no quotes: Area = value

Combining VBA and formulas to check for unique output

Ok, I've got this formula which generates usernames based on a list of first and last names. Now, while this works, I want the cells to refer to my own VBA function instead. But, I still want to use the original formula because of much less code.
I've got this formula:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LOWER(LEFT(table[[#This Row];[Firstname:]])&table[[#This Row];[Lastname:]]);"æ";"a");"ø";"o");"å";"a")
This basically generates the username. But, I want to run this through a separate function, to find out if the username is already taken. And if it is, it should generate a slightly different user name.
I was thinking something along these lines:
Public Function genUserName(ByVal strFirstName, strLastName As String)
Dim strUsername As String
Set objDomain = GetObject("WinNT://grunnarbeid2.local")
objDomain.Filter = Array("User")
'FormulaR1C1 = "=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LOWER(LEFT(tableFaste[[#This Row];[Fornavn:]])&tableFaste[[#This Row];[Etternavn:]]);""æ"";""a"");""ø"";""o"");""å"";""a"")"
'strUsername = ActiveCell.FormulaR1C1
blnFound = False
For Each objUser In objDomain
If objUser.Name = strUsername Then
blnFound = True
Exit For
End If
Next
genUserName = strUsername
End Function
So, how do I combine these?
I would suggest limiting the functionality of genUserName to just checking uniqueness, and pass the result of your existing formual into it:
Public Function genUserName(ByVal strUsername As String)
Set objDomain = GetObject("WinNT://grunnarbeid2.local")
objDomain.Filter = Array("User")
blnFound = False
For Each objUser In objDomain
If objUser.Name = strUsername Then
blnFound = True
Exit For
End If
Next
genUserName = strUsername
End Function
then call it from a cell like
=genUserName(SUBSTITUTE( ... ))