Too many arguments to '' in VB.net - vb.net

I'm trying to save from list field that have more than one data on it, how to save all of it at once?
I've try this code
Dim detail As New Detail
Dim detailBr As New DetailBridge
Dim i As Integer
For i = 0 To lstProduct.Items.Count - 1
detail = detailBr.Insert(Convert.ToInt32(ddlGroup.SelectedValue), lstProduct.Items(i).Value) 'error was here
Next
but I got an error in lstProduct.Items(i).Value the error said
Too many arguments to '...'
I'm not sure what the error is.
can anyone help? Thanks for advice.
UPDATE : detailBr is class and the code is
Public Function Insert(ByVal GroupID As Integer, ByVal ProductID As String) As Boolean
Dim iResult As Integer
Dim arrColumn() As String = {"GroupID", "ProductID"}
Dim arrValue() As Object = {GroupID, ProductID}
oConn.Open()
Dim SQLString As String = GenInsert("DetailGroup", arrColumn, arrValue)
Try
iResult = SCommand.Execute(SQLString, oConn)
Catch ex As Exception
Throw ex
Finally
oConn.Close()
End Try
If iResult > 0 Then
Return True
Else
Return False
End If
End Function

The problem here is with the GenInsert Function. Its last two arguments are arrays.
Dim arrColumn() As String = {"GroupID", "ProductID"}
Dim arrValue() As Object = {GroupID, ProductID}
Dim SQLString As String = GenInsert("DetailGroup", arrColumn, arrValue)
A procedure can define only one parameter array, and it must be the last parameter in the procedure definition. MSDN
In simple words you can have only one parameter as array in GenInsert function either arrColumn or arrValue
However, to solve your current problem you can use two dimensional array as parameter as in passing-two-dimensional-array-through-functions and MSDN: Arrays as Return Values and Parameters

Related

System.NullReferenceException when doing For...Each on Dictionary Array

I may be incorrectly using the term "Dictionary Array", so please tell me the correct terminology if that is the case. In my code below, what I am referring to as a dictionary array is Dim PdfIndividual(individualCount).
Essentially I am trying to get a lambda function to return a count after it iterates through my dictionary array and finds the value I am trying to get a count for.
I am limited in the framework where this code will eventually run which is why I am using a lambda function in the first place. So while I am sure the code I have presented may not be ideal, I am really just interested in understanding why the error occurs, versus just getting it to work.
Dim relationshipTypeCount As Func(Of Dictionary(Of String, String)(), String, Integer) =
Function(PdfIndividualArray, relationshipType)
Dim dictValue As String = ""
Dim relTypeCountResult As Integer = 0
For Each pdfIndDict As Dictionary(Of String, String) In PdfIndividualArray
pdfIndDict.TryGetValue("RelationshipType", dictValue)
If dictValue = relationshipType Then
relTypeCountResult += 1
End If
Next
Return relTypeCountResult
End Function
Dim individualCount As Integer = 5
Dim relType As String = ""
Dim PdfIndividual(individualCount) As Dictionary(Of String, String)
For i As Integer = 0 To individualCount
If i = 0 Then
relType = "primary"
ElseIf i <= 3 Then
relType = "secondary"
ElseIf i > 3 Then
relType = "guest"
End If
PdfIndividual(i) = New Dictionary(Of String, String)
PdfIndividual(i).Add("RelationshipType", relType)
Dim relCount As Integer = relCount = relationshipTypeCount(PdfIndividual, relType)
Next
The error occurs on the dictionary element in the For...Each of my relationshipTypeCount lambda function.
The error text is:
System.NullReferenceException: 'Object reference not set to an
instance of an object.'
pdfIndDict was Nothing.
Screenshot of the error exception
Depending on your targeted .NET version, you can use the following null-conditional form which is more concise:
For Each pdfIndDict As Dictionary(Of String, String) In PdfIndividualArray
pdfIndDict?.TryGetValue("RelationshipType", dictValue)
If dictValue = relationshipType Then relTypeCountResult += 1
Next pdfIndDict
BTW, you can also use the following null check form:
If pdfIndDict IsNot Nothing Then ...
After debugging further I realized that my issue was that I was iterating over an "array" that had 6 elements, however 5 of the elements were nothing at the time of the first call of the lambda function. So I just needed to add a check that the dictionary element I was handling was not nothing.
For Each pdfIndDict As Dictionary(Of String, String) In PdfIndividualArray
If Not IsNothing(pdfIndDict) Then
pdfIndDict.TryGetValue("RelationshipType", dictValue)
If dictValue = relationshipType Then
relTypeCountResult += 1
End If
End If
Next

OverFlowException on Executescalar on vb.net

I'm working in asp.net with vb.net and in the backend I'm trying to select something from the database.
But whenever I ask to execute the query it gives an error which says 'OverflowException Ocuured'. The query that is made works perfectly in my SQL Manager tool. Any ideas what can be the problem.
(it gives the problem on the line under 'try' so the 'returnedId = com.ExecuteScalar' line)
Function selectEIDCardnumber(ByVal name As String) As Integer
Dim con As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("testdatabase").ConnectionString)
Dim selecter As String = "SELECT EIDCardNumber FROM [dbo].[_User] where DisplayName = #name"
Dim com As New SqlCommand(selecter, con)
com.Parameters.AddWithValue("#name", name)
con.Open()
Dim returnedId As Integer = 0
Try
returnedId = com.ExecuteScalar
Catch ex As Exception
Response.Redirect("oops.aspx")
End Try
con.Close()
Return returnedId
End Function
The result of com.ExecuteScalar is larger than the max int value.
Int32.TryParse(com.ExecuteScalar, returnedId)
By defining your returnedId and Function As type Integer you are telling VB that the value will be a whole number between -2,147,483,648 and 2,147,483,647. Assuming the data in your database is correct, the solution is to change the returnedId and Function types to Int64, which holds numbers up to 9,223,372,036,854,775,807.
If you're sure that the EIDCardNumber column is an integer type column then try changing Dim returnedId As Integer = 0 into Dim returnedId As Long = 0 and if the problem persists try changing the value of selecter to "SELECT top 1 EIDCardNumber FROM [dbo].[_User] where DisplayName = #name"

Getting value from a custom object exception error

When I try to assign _val I get "Object does not match target type."
I verified both PropertyInfo's are system.string. I also found examples of this syntax. Thanks for any help.
Private Function SetAttributesForSplitFiles(ByVal _file As String, ByVal _depHeader As HeaderParse)
Dim _fileMask As New FileMaskExtension()
Dim _type As Type = GetType(HeaderParse.depBackupFileProperties)
For Each _prop As Reflection.PropertyInfo In GetType(OutputMgr.Interface.FileMaskExtension).GetProperties()
Dim _headerProperty As PropertyInfo = _type.GetProperty(_prop.Name)
Dim _val = _headerProperty.GetValue(_depHeader)
_prop.SetValue(_fileMask, _val, Nothing)
Next
SendFileTODepcon(_fileMask, _file)
End Function
It was because the custom class I was using was two layers deep.
This worked.
Dim _val As Object = _headerProperty.GetValue(_depHeader.FileProperties)

Count occurance of specific words in a text file in vb.net

I'm trying to count the number of an item in a text file, by counting each instance the item was entered into the file earlier on in the program.
I already have the text read from the file and in a text box. The problem is that my current code was just counting the characters in the textbox and not the number of times my desired word was in the file.
For Each desiredword As String In txtContentofFile.Text
intdesiredword = intdesiredword + 1
txtdesiredwordcount.Text = intdesiredword
Next
This counts the characters in the textbox instead of counting the number of desired words. I tried repeatedly before asking help and searched extensively, but I just don't understand what's wrong with my code. Please help :)
You can use Split Function :
C#:
int count = txtContentofFile.Text.Split(desiredword).Length - 1;
VB.net:
Dim count As Integer = txtContentofFile.Text.Split(desiredword).Length - 1
I prefer to use Regular Expressions in this type of situation. They are very tricky to understand but they are extremely powerful and typically faster than other string manipulation techniques.
Dim AllMatchResults As MatchCollection
Try
Dim RegexObj As New Regex(desiredword)
AllMatchResults = RegexObj.Matches(txtContentofFile.Text)
If AllMatchResults.Count > 0 Then
' Access individual matches using AllMatchResults.Item[]
Else
' Match attempt failed
End If
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try
In your case you are looking for the value from AllMatchResults.Count.
Using a great Regular Expression tool like RegexBuddy to build and test the expressions is a great help too. (The above code snippet was generated by RegexBuddy!)
Try this:
Dim text As String = IO.File.ReadAllText("C:\file.txt")
Dim wordsToSearch() As String = New String() {"Hello", "World", "foo"}
Dim words As New List(Of String)()
Dim findings As Dictionary(Of String, List(Of Integer))
'Dividing into words
words.AddRange(text.Split(New String() {" ", Environment.NewLine()}, StringSplitOptions.RemoveEmptyEntries))
findings = SearchWords(words, wordsToSearch)
Console.WriteLine("Number of 'foo': " & findings("foo").Count)
Function used:
Private Function SearchWords(ByVal allWords As List(Of String), ByVal wordsToSearch() As String) As Dictionary(Of String, List(Of Integer))
Dim dResult As New Dictionary(Of String, List(Of Integer))()
Dim i As Integer = 0
For Each s As String In wordsToSearch
dResult.Add(s, New List(Of Integer))
While i >= 0 AndAlso i < allWords.Count
i = allWords.IndexOf(s, i)
If i >= 0 Then dResult(s).Add(i)
i += 1
End While
Next
Return dResult
End Function
You will have not only the number of occurances, but the index positions in the file, grouped easily in a Dictionary.
Try the following code
Function word_frequency(word_ As String, input As String) As Integer
Dim ct = 0
Try
Dim wLEN = word_.Length
Do While input.IndexOf(word_) <> -1
Dim idx = input.IndexOf(word_) + wLEN
ct += 1
input = input.Substring(idx)
Loop
Catch ex As Exception
End Try
Return ct
End Function

Very simple ssis script transformation giving Object reference not set to an instance of an object

I have an ssis package that takes a flat file and dumps it to SQL. During that process, 150 columns need to have '-' and '.' removed from them. I have done this using a Script Transformation.
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
Dim column As IDTSInputColumn90
Dim rowType As Type = Row.GetType()
Dim columnValue As PropertyInfo
For Each column In Me.ComponentMetaData.InputCollection(0).InputColumnCollection
columnValue = rowType.GetProperty(column.Name)
Dim strCol As String = columnValue.GetValue(Row, Nothing).ToString()
If Not String.IsNullOrEmpty(strCol) Then
strCol = strCol.Replace("-", "").Replace(".", "")
columnValue.SetValue(Row, strCol, Nothing)
End If
Next
End Sub
I however get the following error when running it
Object reference not set to an instance of an object.
at ScriptComponent_f20c21e11e3348378ef0bbe6b65e9c05.ScriptMain.Input0_ProcessInputRow(Input0Buffer Row)
at ScriptComponent_f20c21e11e3348378ef0bbe6b65e9c05.UserComponent.Input0_ProcessInput(Input0Buffer Buffer)
at ScriptComponent_f20c21e11e3348378ef0bbe6b65e9c05.UserComponent.ProcessInput(Int32 InputID, PipelineBuffer Buffer)
at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.ProcessInput(Int32 inputID, PipelineBuffer buffer)
I can't figure out why this is happening.. Any thoughts?
Edit I've discovered that this line is the problem
Dim strCol As String = columnValue.GetValue(Row, Nothing).ToString()
I was able to get it to work for my dataset. I skipped the cast to string type and just left it as object type based on what I assume to be the source of your script. It's not pretty but it seems to get the job done.
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
Dim column As IDTSInputColumn90
Dim rowType As Type = Row.GetType()
Dim columnValue As PropertyInfo
For Each column In Me.ComponentMetaData.InputCollection(0).InputColumnCollection
columnValue = rowType.GetProperty(column.Name)
Dim meta As Integer = column.ExternalMetadataColumnID
Try
Dim Obj As Object = columnValue.GetValue(Row, Nothing)
If Not String.IsNullOrEmpty(Obj) Then
Obj = Obj.Replace("-", "").Replace(".", "")
columnValue.SetValue(Row, Obj.ToString(), Nothing)
End If
Finally
' Rejoice in our null-ness
End Try
Next
End Sub
Source data
Starting with this data
SourceSSN,DestinationSSN,Amount
,111-11-0000,5000.00
111-22-3333,111-22-3334,5000.00
121-22-3333,121-22-3334,5000.00
123-22-3333,123-22-3334,5000.00
124-22-3333,124-22-3334,5000.00
The above script makes this