I am new to vb well not new but used chsarp for years I cant figure out what's wrong with this statement. The debugger is not even hitting the return list part so I don't no what is up. I am using the same context to add files so i don't think its the way i declared it
Private threeSoftwareContext As New R3DeliveryEntities1
Public Function GetAlLFilesToBeProcessedByLocation(location As Int32, status As StatusTypes) As List(Of downloadedFile)
Dim list As New List(Of downloadedFile)
list = (From files In threeSoftwareContext.downloadedFiles Where files.location = 0 And files.status = status).ToList()
Return list
End Function
Exception message
i got it working with this but what i dont no in vb is how do I give it columns names
Dim retVal As List(Of downloadedFile)
Try
retVal = (From files In threeSoftwareContext.downloadedFiles Where files.location = 0).ToList()
Return retVal
Catch ex As Exception
MsgBox(ex.ToString())
End Try
Return retVal
Related
I am trying to upload a csv file that has two records in it.
The below code executed for two times and third time I got this exception----Conversion of type string " " to double not valid
I put a debugger where I found the values of two colums of excel sheet but third time I am getting this exception. Your help is highly appreciated.
Below is the code.
Public Function GetLocationInformation(stream As Stream) As List(Of CsvPropLocation) _
Implements ICsvHandling.GetLocationInformation
If stream Is Nothing Then Return Nothing
Dim locations = New List(Of CsvPropLocation)
Using reader = New StreamReader(stream)
Dim config = New CsvConfiguration(CultureInfo.InvariantCulture) With {
.HasHeaderRecord = True,
.IgnoreBlankLines = False
}
Using csv = New CsvReader(reader, config)
Using dataReader As New CsvDataReader(csv)
Dim dt = New DataTable()
dt.Load(dataReader)
For Each row As DataRow In dt.Rows
Dim propLocation As CsvPropLocation
'find or create a propLocation
If locations.Any(Function(x) x.nLocationNumber = row("LocNum")) Then ######Got exception here ######
propLocation = locations.First(Function(x) x.nLocationNumber = row("LocNum"))
Else
propLocation = New CsvPropLocation(row("LocNum"))
locations.Add(propLocation)
End If
'do building stuff.
Dim building = ParseRowIntoBuilding(row)
propLocation.AddBuilding(building)
Next
End Using
End Using
End Using
Return locations
End Function
Change your line to
Dim number As Double
If Double.TryParse(row("LocNum"), number ) AndAlso locations.Any(Function(x) x.nLocationNumber = number )
This way you make sure number will be evaluated only if it gets a valid value from row("LocNum")
Also keep in mind the Else part must be controlled as New CsvPropLocation(row("LocNum")) probably is expecting a valid Double which isnt inside locations so, change to:
Else If Double.TryParse(row("LocNum"), number ) 'You can check against number = 0
'if zero isn't a valid value for number
'(or initialize number to a known invalid value and check against it)
'if yuo didn't want a double try parse
propLocation = New CsvPropLocation(number)
locations.Add(propLocation)
End If
I hope someone can help me.
In short, I have this little code
I can't replace "Path from my pc" with a string/textbox in any way
The intention would be this:
enumerator = SpotifyBox.Text.Split.GetEnumerator
It doesn't give me errors, but it doesn't work as it should once the button starts
Dim enumerator As List(Of String).Enumerator = New List(Of String).Enumerator()
Dim class70 As Action(Of String())
ThreadPool.SetMinThreads(40, 40)
Dim strArrays As List(Of String()) = New List(Of String())()
Try
Try
enumerator = File.ReadLines(Path from pc).ToList().GetEnumerator()
While enumerator.MoveNext()
Dim current As String = enumerator.Current
If (If(Not current.Contains(":"), True, String.IsNullOrEmpty(current))) Then
Continue While
End If
strArrays.Add(current.Split(New Char() {":"c}))
End While
Finally
DirectCast(enumerator, IDisposable).Dispose()
End Try
int_5 = strArrays.Count
Catch exception1 As System.Exception
End Try
That is some crazy code and the question you're asking is not the question you need an answer to. Based on what you have posted, it seems that you have a file path in a TextBox named SpotifyBox and you want to read the lines from that file with some processing. In that case, get rid of all that craziness and do this:
Dim filePath = SpotifyBox.Text
Dim records As New List(Of String())
For Each line In File.ReadLines(filePath)
If line.Contains(":") Then
records.Add(line.Split(":"c))
End If
Next
That's it, that's all. You pretty much never need to create an enumerator directly. Just use a For Each loop.
I am trying to follow this video that is written in C# and convert it to vb (to Populate a dropdown box from SQL Server using a function)
here is the working C# code:
private list<product> getallprodcuts()
{
Try
{
using(PropSolWebDBEntities db= new PropSolWebDBEntities())
list<product> products = (from x in db.product select x).tolist;
}
catch(exception)
{
Return Null;
}
}
And the vb that I cant get to work:
Private Function getallproducts() As List( Of<product>)
Try
Dim db As New PropSolWebDBEntities
Dim products As list <product> = (from x in db.product select x).tolist
Return products
Catch ex As Exception
Return vbNull
End Try
End Function
What am I doing wrong?
List should be translated as List(of product).
return null should be translated as Return Nothing.
Use using instead of declaring the db variable.
I don't compile the code, but it's like this:
Private Function getallproducts() As List(Of product)
Try
Using db As New PropSolWebDBEntities
Dim products As list(Of product) = (From x In db.product Select x).ToList()
Return products
End Using
Catch ex As Exception
Return Nothing
End Try
End Function
You don't need the LINQ part since you are not filtering/grouping/ordering by. The Using block takes care of your context disposal for you. You just missed some syntax difference.
Private Function getallproducts() As List(Of product)
Dim results As List(Of product) = Nothing
Using db As New PropSolWebDBEntities
results = db.product.ToList
End Using
Return results
End Function
I am trying to use Parallel.ForEachLoop for a iterations of almost million records in My windows application. I am facing an error while fails on a convert to string on a string builder due to some threading problem although i use a object for lock.
I Tried looking at shared resource for Parallel.ForEach could not find a proper answer.
dtProd has 900 000 records
Dim sbFile As New StringBuilder
messagesLock As Object = New Object()
Dim sbRecord As New StringBuilder
Dim dtDet As New Data.DataTable
Dim dtProd As New Data.DataTable
Public Sub CreateFeedFile()
Try
GetData()
Dim temporaryEnumerable As IEnumerable(Of DataRow) = dtProd.Rows.Cast(Of DataRow)()
sbRecord.AppendLine(dtFeed(0).Item("HeaderText"))
Parallel.ForEach(temporaryEnumerable, Sub(dtDet)
RunLoop(DetCount)
End Sub)
sbRecord.AppendLine(dtFeed(0).Item("FooterText"))
Catch ex As Exception
Dim a = ex.Message.ToString()
End Try
End Sub
Private Sub RunLoop(ByRef DetCount As Integer)
For Each drDet As DataRowView In dvDet 'loop detail records of field values
.. .. .. Append info to sbRecord
Next
Try
SyncLock Me.messagesLock
sbFile.AppendLine(sbRecord.ToString())
sbRecord.Clear()
End SyncLock
Catch ex As Exception
Dim a = ex.Message.ToString() --Fails Here on the statement sbRecord.ToString()
End Try
The problem is that you have one StringBuilder (sbRecord) that is shared by the parallel threads. You need to move sbRecord to be a local variable inside RunLoop.
I think you also want the .AppendLine(...) calls in CreateFeedFile to be on sbFile rather than sbRecord.
I've seen a few pages on the net regarding this, but unfortunately the sample code is in C#. I can't make sense of most of it (and/or run it through a code translator) but I still need help making it work in VB.
My custom function is:
Public Shared Function GetFriendlyTitle(Title As String) As String
Dim ConvertedTitle As String = ""
Try
Dim oRE As Regex = New Regex("[^a-zA-Z0-9\s]")
ConvertedTitle = Trim(oRE.Replace(Title, "")).Replace(" ", "_")
Catch ex As Exception
LogError(ex)
End Try
Return ConvertedTitle
End Function
and here's the function I'm calling to return products:
Public Shared Function GetProductByTypeAndTitle(URLFriendlyTitle As String, ProductType As ProductType)
Try
'don't know why, but the server threw errors if I went c.Type=Type
Dim pt As Integer = CInt([Enum].Parse(GetType(ProductType), [Enum].GetName(GetType(ProductType), ProductType)))
Dim context As LionsSharePressEntities = New LionsSharePressModel.LionsSharePressEntities
return From p In context.Products Where GetFriendlyTitle(p.Title) = URLFriendlyTitle AndAlso p.Type = pt
Catch ex As Exception
LogError(ex)
Return nothing
End Try
End Function
It compiles fine, but hangs when I run it. It's that return line that does it.
Try to add Select statement:
return From p In context.Products
Where GetFriendlyTitle(p.Title) = URLFriendlyTitle
AndAlso p.Type = pt
Select p
This question is a bit old and already seems to have been resolved. But I'm still postings this in hopes that maybe it will help someone out with an alternate solution that has worked for me.
Also please note that I was not in a position to make my function called from w/in my LINQ query "Shared" making my situation slightly different. Which I thought all the more reason to post an alternate answer.
Public Function GetFriendlyTitle(Title As String) As String
Dim ConvertedTitle As String = ""
Try
Dim oRE As Regex = New Regex("[^a-zA-Z0-9\s]")
ConvertedTitle = Trim(oRE.Replace(Title, "")).Replace(" ", "_")
Catch ex As Exception
LogError(ex)
End Try
Return ConvertedTitle
End Function
When I ran into an issue calling a non shared user defined function from w/in a LINQ query this is how I resolved it using the OPs code snippet as an example.
Public Shared Function GetProductByTypeAndTitle(URLFriendlyTitle As String, _
ProductType As ProductType)
Try
'don't know why, but the server threw errors if I went c.Type=Type
Dim pt As Integer = _
CInt([Enum].Parse(GetType(ProductType), _
[Enum].GetName(GetType(ProductType), ProductType)))
Dim context As New LionsSharePressModel.LionsSharePressEntities
'// Here is the lambda that will call your GetFriendlyTitle function
Dim callUserFunc As Func(Of String, String) = _
Function(title As String)
Return GetFriendlyTitle(title)
End Function
'// Now call Invoke on the lambda and pass in the needed param(s) from within your LINQ query
return From p In context.Products _
Where callUserFunc.Invoke(p.Title) = URLFriendlyTitle AndAlso p.Type = pt
'//return From p In context.Products Where GetFriendlyTitle(p.Title) = URLFriendlyTitle AndAlso p.Type = pt
Catch ex As Exception
LogError(ex)
Return nothing
End Try
End Function