Link Exception/Error - vb.net

For a long time, I have been try to do this:
Private Sub Search()
'DataGrid1.ItemsSource = From k In Globals.Batchs.BatchList Where (CType(k.Category, String).ToLower().Contains(SearchByCategory.Text)) Select k
Dim tot As List(Of WorkMateLib.BatchLib.BatchItem) = Globals.Batchs.BatchList
If Not SearchByCategory.Text = "" Then
tot = From k As WorkMateLib.BatchLib.BatchItem In tot Where CType(k.Category, String).ToLower().Contains(SearchByCategory.Text) Select k
End If
If Not SearchByCourse.Text = "" Then
tot = From k In tot Where (CType(k.CourseName, String).ToLower().Contains(SearchByCourse.Text))
End If
If Not SearchByName.Text = "" Then
tot = From k In tot Where (CType(k.BatchName, String).ToLower().Contains(SearchByName.Text))
End If
If Not SearchByYear.Text = "" Then
tot = From k In tot Where (CType(k.DateStarted, Date).Year.ToString.ToLower.Contains(SearchByYear.Text))
End If
DataGrid1.ItemsSource = tot
End Sub
But is keep on throwing the following exception:
Unable to cast object of type 'WhereSelectListIterator`2[WorkMateLib.BatchLib.BatchItem,WorkMateLib.BatchLib.BatchItem]' to type 'System.Collections.Generic.List`1[WorkMateLib.BatchLib.BatchItem]'.
Could you please review and let me know where I am going wrong please.
Following is the code for the classes:
Namespace BatchLib
Public Class BatchItem
Dim _BatchID As String
Dim _BatchName As String
Dim _Category As String
Dim _CourseID As String
Dim _CourseName As String
Dim _StartDate As Date
Dim _StartDateFormated As String
Dim _Deleted As Boolean
#Region "Property"
Property BatchID
Get
Return _BatchID
End Get
Set(value)
_BatchID = value
End Set
End Property
Property BatchName
Get
Return _BatchName
End Get
Set(value)
_BatchName = value
End Set
End Property
Property Category
Get
Return _Category
End Get
Set(value)
_Category = value
End Set
End Property
Property CourseID
Get
Return _CourseID
End Get
Set(value)
_CourseID = value
End Set
End Property
Property DateStarted
Get
Return _StartDate
End Get
Set(value)
_StartDate = value
End Set
End Property
Property Deleted
Get
Return _Deleted
End Get
Set(value)
_Deleted = value
End Set
End Property
Property CourseName
Get
Return _CourseName
End Get
Set(value)
_CourseName = value
End Set
End Property
Property StartDateFormated
Get
Return _StartDateFormated
End Get
Set(value)
_StartDateFormated = value
End Set
End Property
#End Region
End Class
Public Class Batchs
Public BatchList As New List(Of BatchItem)
Public Function Contains(ByVal Batchname As String, ByVal CourseID As String)
For Each k As BatchItem In BatchList
If k.CourseID = CourseID And k.BatchName = Batchname Then
Return True
Exit Function
End If
Next
Return False
End Function
Public Function Contains(ByVal Batchname As String, ByVal CourseID As String, ByVal BatchID As String)
For Each k As BatchItem In BatchList
If k.CourseID = CourseID And k.BatchName = Batchname Then
If k.BatchID = BatchID Then
Else
Return True
Exit Function
End If
End If
Next
Return False
End Function
End Class
End Namespace
Thank you for your efforts. I am new to linq, so I may not be that good at it, your assistant and making me understand the mistake is heartily welcomed.

Exception does say exacly whats ur problem. Linq expression does not return List and its not assignable to List. Try surounding your whole expression in brackets and use ToList()

Related

"Expression of type "" is not queryable" error

I'm attempting to get an assignment done for class and have hit this error:
Error 1 Expression of type 'Homework_Code_Examples.Form1.State' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider. D:\Users\Stryke\Documents\Visual Studio 2013\Projects\Homework Code Examples\Homework Code Examples\Form1.vb 11 32 Homework Code Examples
I've never encountered this before and searching via Google, MSDN and these forums all tell me that I must target the correct .NET framework for my program, however I am completely lost as to which framework I should target as the recommended answers all give the same error upon being recompiled. Currently, I am using .NET Framework 4.5 in Visual Studio 2013.
I don't know if it helps at all or not, but here is my code so that you may see what it is I'm trying to achieve...
Public Class Form1
Dim states As State
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
states = New State()
Dim query = From st In states
Let name = states.Name
Let density = states.Density()
Order By density Descending
Select name, density
End Sub
Private Sub stateInfo_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles stateInfo.CellContentClick
End Sub
Class State
Private m_name, m_abbr, m_date As String
Private m_area, m_pop As Integer
Public Property Name()
Get
Return m_name
End Get
Set(value)
m_name = value
End Set
End Property
Public Property Abbreviation()
Get
Return m_abbr
End Get
Set(value)
m_abbr = value
End Set
End Property
Public Property joinDate()
Get
Return m_date
End Get
Set(value)
m_date = value
End Set
End Property
Public Property landArea
Get
Return m_area
End Get
Set(value)
m_area = value
End Set
End Property
Public Property Population
Get
Return m_pop
End Get
Set(value)
m_pop = value
End Set
End Property
Public Function Density()
Dim popDensity As Integer
popDensity = Population / landArea
Return popDensity
End Function
End Class
End Class
Linq needs to iterate through multiple objects. In your case, it can be list or collection of State class.
Example with List(Of Type)
Change line
Dim states As State
to
Dim states As List(Of State)
and
states = New State()
into
states = New List(Of State)
Example with array
Change line
Dim states As State
to
Dim states() As State
and
states = New State()
into
ReDim states(10) 'or use a variable inside
Here's a sample of how to populate your data. I also fix some error in your query. Compare your query to mine to see what I changed.
' initialize a list with some values
Dim states = New List(Of State) From {
New State With {.Name = "Ohio", .Population = 10000, .landArea = 10},
New State With {.Name = "Texas", .Population = 20000, .landArea = 300},
New State With {.Name = "Florida", .Population = 5000, .landArea = 1000}
}
' query the list
Dim query = From st In states
Let name = st.Name
Let density = st.Density()
Order By density Descending
Select name, density
I also improve your State class to use proper data type instead of object.
Class State
Private m_name, m_abbr, m_date As String
Private m_area, m_pop As Integer
Public Property Name As String
Get
Return m_name
End Get
Set (value As String)
m_name = value
End Set
End Property
Public Property Abbreviation As String
Get
Return m_abbr
End Get
Set(value As String)
m_abbr = value
End Set
End Property
Public Property joinDate As String
Get
Return m_date
End Get
Set(value As String)
m_date = value
End Set
End Property
Public Property landArea As Integer
Get
Return m_area
End Get
Set(value As Integer)
m_area = value
End Set
End Property
Public Property Population As Integer
Get
Return m_pop
End Get
Set(value As Integer)
m_pop = value
End Set
End Property
Public Function Density() As Double
Dim popDensity As Double
popDensity = Population / landArea
Return popDensity
End Function
End Class

How to access class properties outside the constructor

I'm trying to retrieve my other football attributes (crossing, dribbling, finishing, ball control) that haven't been requested as arguments in the constructor- reportID, playerID and comments are all that are required.
There are over 20 football attributes, so I've just included the first few for readability.
Report Class:
Public Class Report
Public Sub New(ByVal reportID As Integer, ByVal playerID As Integer, ByVal comments As String)
_ReportID = reportID
_PlayerID = playerID
_Comments = comments
End Sub
Private _ReportID As Integer = 0
Private _PlayerID As Integer = 0
Private _Comments As String = ""
Private _Crossing As Integer = 0
Private _Dribbling As Integer = 0
Private _Finishing As Integer = 0
Private _BallControl As Integer = 0
Public Property ReportID() As Integer
Get
Return _ReportID
End Get
Set(ByVal value As Integer)
_ReportID = value
End Set
End Property
Public Property PlayerID() As Integer
Get
Return _PlayerID
End Get
Set(ByVal value As Integer)
_PlayerID = value
End Set
End Property
Public Property Comments() As String
Get
Return _Comments
End Get
Set(ByVal value As String)
_Comments = value
End Set
End Property
Public Property Crossing() As Integer
Get
Return _Crossing
End Get
Set(ByVal value As Integer)
_Crossing = value
End Set
End Property
Public Property Dribbling() As Integer
Get
Return _Dribbling
End Get
Set(ByVal value As Integer)
_Dribbling = value
End Set
End Property
Public Property Finishing() As Integer
Get
Return _Finishing
End Get
Set(ByVal value As Integer)
_Finishing = value
End Set
End Property
Public Property BallControl() As Integer
Get
Return _BallControl
End Get
Set(ByVal value As Integer)
_BallControl = value
End Set
End Property
End Class
Below I realise I'm only adding reportID, playerID and comments to my typeList, which is why I'm getting all 0's for my other attributes. How do access the attributes as well?
Retrieving the data:
Private Function retrieveReport() As List(Of Report)
Dim typeList As New List(Of Report)
Dim Str As String = "SELECT * FROM Report ORDER BY PlayerID"
Try
Using conn As New SqlClient.SqlConnection(DBConnection)
conn.Open()
Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
Using drResult As SqlClient.SqlDataReader = cmdQuery.ExecuteReader()
While drResult.Read
typeList.Add(New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments")))
End While
End Using 'Automatically closes connection
End Using
End Using
Catch ex As Exception
MsgBox("Report Exception: " & ex.Message & vbNewLine & Str)
End Try
Return typeList
End Function
Private Sub setReport()
For Each rpt As Report In retrieveReport()
'*****General Information
UC_Menu_Scout1.txtComments.Text = rpt.Comments
'*****Technical
UC_Menu_Scout1.UcAttributes1.lblXCrossing.Text = rpt.Crossing
UC_Menu_Scout1.UcAttributes1.lblXDribbling.Text = rpt.Dribbling
UC_Menu_Scout1.UcAttributes1.lblXFinishing.Text = rpt.Finishing
UC_Menu_Scout1.UcAttributes1.lblXBallControl.Text = rpt.BallControl
UC_Menu_Scout1.UcAttributes1.lblXPassing.Text = rpt.Passing
UC_Menu_Scout1.UcAttributes1.lblXHeadingAccuracy.Text = rpt.HeadingAccuracy
UC_Menu_Scout1.UcAttributes1.lblXMarking.Text = rpt.Marking
UC_Menu_Scout1.UcAttributes1.lblXTackling.Text = rpt.Tackling
'*****Mental
UC_Menu_Scout1.UcAttributes1.lblXAggression.Text = rpt.Aggression
UC_Menu_Scout1.UcAttributes1.lblXPositioning.Text = rpt.Positioning
UC_Menu_Scout1.UcAttributes1.lblXAnticipation.Text = rpt.Anticipation
UC_Menu_Scout1.UcAttributes1.lblXComposure.Text = rpt.Composure
UC_Menu_Scout1.UcAttributes1.lblXVision.Text = rpt.Vision
UC_Menu_Scout1.UcAttributes1.lblXTeamwork.Text = rpt.Teamwork
UC_Menu_Scout1.UcAttributes1.lblXWorkRate.Text = rpt.WorkRate
'*****Physical
UC_Menu_Scout1.UcAttributes1.lblXPace.Text = rpt.Pace
UC_Menu_Scout1.UcAttributes1.lblXBalance.Text = rpt.Balance
UC_Menu_Scout1.UcAttributes1.lblXJumping.Text = rpt.Jumping
UC_Menu_Scout1.UcAttributes1.lblXStrength.Text = rpt.Strength
UC_Menu_Scout1.UcAttributes1.lblXStamina.Text = rpt.Stamina
Next
End Sub
I imagine it's not that hard, so any help would be appreciated please!
To add values to the properties, use With:
typeList.Add(New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments")) With {.BallControl = drResult("BallControl"), .Dribbling = drResult("Dribbling")})
You need to assign the property values that you aren't providing to the constructor as arguments, or you can add arguments to the constructor. Try this -- instead of calling the constructor like this:
typeList.Add(New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments")))
instead, do this:
dim rep = New Report(drResult("ReportID"), drResult("PlayerID"), drResult("Comments"))
.Crossing = drResult("Crossing")
'additional property assignments
With rep
End With
typeList.Add(rep)

Image Search with Bing/Azure in VB.NET

I'm trying to do a Bing Image Search Application (Azure Version), and i can't make progress. The code language is vb.net. Basically what i'm doing is trying to edit this code, that actually worked. Any Solutions ?
Function ExecuteQuery() As Boolean
Dim esito As Boolean = False
Try
Dim query As String = System.Web.HttpUtility.UrlEncode("Inception Movie")
Dim skip As String = "10"
Dim urlBase As New Uri("https://api.datamarket.azure.com")
Dim accountKey As String = "tymv8z6jFSdo4eQ3vsS5r8SZAmFtA24e6dmfyvaLh3U"
Dim credentials As New NetworkCredential(accountKey, accountKey)
Dim dsc As New System.Data.Services.Client.DataServiceContext(urlBase)
dsc.Credentials = New NetworkCredential(accountKey, accountKey)
Dim urlSearch As Uri = New Uri(("https://api.datamarket.azure.com/Bing/Search/Image?Query=%27" + query + "%27&$skip=" + skip))
Dim webResults = dsc.Execute(Of WebResult)(urlSearch)
For Each result As WebResult In webResults
ListBox1.Items.Add(result.Title)
ListBox1.Items.Add(result.Description)
singleValue = singleValue + 1
Next
esito = True
Catch ex As Exception
esito = False
End Try
Return esito
End Function
Partial Public Class WebResult
Private _ID As Guid
Private _Title As [String]
Private _Description As [String]
Private _DisplayUrl As [String]
Private _Url As [String]
Private _MediaUrl As [String]
Public Property ID() As Guid
Get
Return Me._ID
End Get
Set(ByVal value As Guid)
Me._ID = value
End Set
End Property
Public Property Title() As [String]
Get
Return Me._Title
End Get
Set(ByVal value As [String])
Me._Title = value
End Set
End Property
Public Property Description() As [String]
Get
Return Me._Description
End Get
Set(ByVal value As [String])
Me._Description = value
End Set
End Property
Public Property DisplayUrl() As [String]
Get
Return Me._DisplayUrl
End Get
Set(ByVal value As [String])
Me._DisplayUrl = value
End Set
End Property
Public Property Url() As [String]
Get
Return Me._Url
End Get
Set(ByVal value As [String])
Me._Url = value
End Set
End Property
Public Property MediaUrl() As [String]
Get
Return Me._MediaUrl
End Get
Set(ByVal value As [String])
Me._MediaUrl = value
End Set
End Property
End Class
I now solved the puzzle, founded a simple way to do this thing just using an class file:
1 - Just download this file: http://www.getcodesamples.com/src/15958EA3/F43E1E1A
2 - Then add the same file to your project
3 - Add the following code:
Dim strBingKey As String = "xxxaccountkeyxxx"
Dim bingClass As New Bing.BingSearchContainer(New Uri("https://api.datamarket.azure.com/Bing/Search/"))
bingClass.Credentials = New NetworkCredential(strBingKey, strBingKey)
Dim query = bingClass.Image("Inception" + "Movie", Nothing, "en-us", "Off", Nothing, Nothing, Nothing)
Dim results = query.Execute()
For Each result In results
ListBox1.Items.Add(result.Title)
ListBox1.Items.Add(result.MediaUrl)
Console.WriteLine(result.Thumbnail) 'ThumbNail type, need to convert to use in result list
Next
Thank you all for the patience!

Error says x is not a member of y, but it is

I have a sp that I added to my linq designer, which generated the result class:
Partial Public Class web_GetTweetsByUserIDResult
Private _userid As Integer
Private _tweetid As Integer
Private _TweeterFeed As String
Public Sub New()
MyBase.New
End Sub
<Global.System.Data.Linq.Mapping.ColumnAttribute(Storage:="_userid", DbType:="Int NOT NULL")> _
Public Property userid() As Integer
Get
Return Me._userid
End Get
Set
If ((Me._userid = value) _
= false) Then
Me._userid = value
End If
End Set
End Property
<Global.System.Data.Linq.Mapping.ColumnAttribute(Storage:="_tweetid", DbType:="Int NOT NULL")> _
Public Property tweetid() As Integer
Get
Return Me._tweetid
End Get
Set
If ((Me._tweetid = value) _
= false) Then
Me._tweetid = value
End If
End Set
End Property
<Global.System.Data.Linq.Mapping.ColumnAttribute(Storage:="_TweeterFeed", DbType:="NVarChar(100)")> _
Public Property TweeterFeed() As String
Get
Return Me._TweeterFeed
End Get
Set
If (String.Equals(Me._TweeterFeed, value) = false) Then
Me._TweeterFeed = value
End If
End Set
End Property
End Class
However, in this one section of code where I am trying to use the "TweeterFeed" member of the result class I am getting the error, "Error 4 'TweeterFeed' is not a member of 'System.Data.Linq.ISingleResult(Of web_GetTweetsByUserIDResult)'."
My code in this section is, :
<WebMethod()> _
Public Function GetTweetsByUserID(ByVal userID As Integer) As List(Of SimpleTweet)
Dim result As New List(Of SimpleTweet)
Dim urlTwitter As String = "https://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name={0}&count=20"
Dim lq As New lqDFDataContext
Dim var = lq.web_GetTweetsByUserID(userID).ToList()
If Not var Is Nothing Then
For Each twitterfeed In var
Dim listURL As String = String.Format(urlTwitter, var.TweeterFeed)
Dim tweetXML As XmlDocument = utils.GetXMLForURL(listURL)
Dim tweetnodelist As XmlNodeList = tweetXML.ChildNodes(1).ChildNodes
For Each node As XmlNode In tweetnodelist
Dim tweet As New SimpleTweet
tweet.CreatedAt = node.SelectSingleNode("created_at").InnerText
tweet.HTMLText = utils.ReturnTextWithHRefLink(node.SelectSingleNode("text").InnerText)
tweet.ID = node.SelectSingleNode("id").InnerText
tweet.Name = node.SelectSingleNode("user/name").InnerText
tweet.ScreenName = node.SelectSingleNode("user/screen_name").InnerText
tweet.Text = node.SelectSingleNode("text").InnerText
tweet.UserID = node.SelectSingleNode("user/id").InnerText
tweet.ProfileImageURL = node.SelectSingleNode("user/profile_image_url_https").InnerText
result.Add(tweet)
Next
Next
End If
Return result
End Function
Does anyone have any idea what is going on? As far as I see "TweeterFeed" is clearly a member of the class, I can't figure out why I would be getting this error.
You're using var.TweeterFeed when you should be using twitterFeed.TweeterFeed. twitterFeed is a result extracted from var which is a sequence of results.
Using a more descriptive variable name than var would probably have made this clearer to you :)
I have this class
Public Class Tamano
Private pWidth As Integer
Private pHeight As Integer
Public Property Width As Integer
Public Property Height As Integer
End Class
I got the compilation error message 'Height' is not a member of 'TamaƱo' in IIS
To fix it, add Set and Get to the properties and it compiles.
Public Class Tamano
Private pWidth As Integer
Private pHeight As Integer
Public Property Width As Integer
Get
Return pWidth
End Get
Set(value As Integer)
pWidth = value
End Set
End Property
Public Property Height As Integer
Get
Return pHeight
End Get
Set(value As Integer)
pHeight = value
End Set
End Property
End Class
This might not be directly related to your question but It might help someone else.

joining 3 table and return

I have 3 table which are Timetable, Schedule and Consultation slot
and i wish to combine them so that all the data are able to retrieve easily
Timetable are having
1. TimetableID
2. Lecture ID
3. ClassVenue
4. ClassStartTime
5. ClassEndTime
Schedule
1. ScheduleID
2. LectureID
3. ScheduleVenue
4. ScheduleStartTime
5. ScheduleEndTime
Consultation Slot
1. ConsultationID
2. LectureID
3. StudentID
4. ScheduleID
5. remark
Here is my code in metadata
Partial Public Class CombinationOfTSC
<Key()> _
Public Property LectureID() As String
Get
Return m_LectureID
End Get
Set(value As String)
m_LectureID = value
End Set
End Property
Private m_LectureID As String
Public Property StudentID() As String
Get
Return m_StudentID
End Get
Set(value As String)
m_StudentID = value
End Set
End Property
Private m_StudentID As String
Public Property ttID() As String
Get
Return m_ttID
End Get
Set(value As String)
m_ttID = value
End Set
End Property
Private m_ttID As String
Public Property ttClassVenue() As String
Get
Return m_ttClassVenue
End Get
Set(value As String)
m_ttClassVenue = value
End Set
End Property
Private m_ttClassVenue As String
Public Property ttClassStartTime() As DateTime
Get
Return m_ttClassStartTime
End Get
Set(value As DateTime)
m_ttClassStartTime = value
End Set
End Property
Private m_ttClassStartTime As DateTime
Public Property ttClassEndTime() As DateTime
Get
Return m_ttClassEndTime
End Get
Set(value As DateTime)
m_ttClassEndTime = value
End Set
End Property
Private m_ttClassEndTime As DateTime
Public Property scID() As String
Get
Return m_scID
End Get
Set(value As String)
m_scID = value
End Set
End Property
Private m_scID As String
Public Property scVenue() As String
Get
Return m_scVenue
End Get
Set(value As String)
m_scVenue = value
End Set
End Property
Private m_scVenue As String
Public Property scStartTime() As DateTime
Get
Return m_scStartTime
End Get
Set(value As DateTime)
m_scStartTime = value
End Set
End Property
Private m_scStartTime As DateTime
Public Property scEndTime() As DateTime
Get
Return m_scEndTime
End Get
Set(value As DateTime)
m_scEndTime = value
End Set
End Property
Private m_scEndTime As DateTime
Public Property cID() As String
Get
Return m_cID
End Get
Set(value As String)
m_cID = value
End Set
End Property
Private m_cID As String
Public Property cRemark() As String
Get
Return m_cRemark
End Get
Set(value As String)
m_cRemark = value
End Set
End Property
Private m_cRemark As String
End Class
In my domain class the code will be
Public Function GetCoTSC(lectureID As String) As IQueryable(Of CombinationOfTSC)
Dim CSC As IQueryable(Of CombinationOfTSC) = From c In ObjectContext.ConsultationSlots Join s In ObjectContext.Schedules On c.ScheduleID Equals s.ScheduleID Join t In ObjectContext.TimeTables On t.LectureID Equals s.LectureID Where c.LectureID = s.LectureID = t.LectureID Select New CombinationOfTSC() With { _
.cID = c.ConsultationID, _
.cRemark = c.Remark, _
.StudentID = c.StudentID, _
.LectureID = s.LectureID, _
.scStartTime = s.ScheduleStartTime, _
.scEndTime = s.ScheduleEndTime, _
.scID = s.ScheduleID, _
.scVenue = s.ScheduleVenue, _
.ttID = t.TimeTableID, _
.ttClassVenue = t.ClassVenue, _
.ttClassStartTime = t.ClassStartTime, _
.ttClassEndTime = t.ClassEndTime}
Return CSC
End Function
I'm having error at c.LectureID = s.LectureID = t.LectureID
I wish to get the combination of 3 tables through the lectureID since there are lecture ID on 3 tables.
Anyone helps pls.
I don't believe you can have x=y=z. Have you tried seperating these out into pairs?
try Where c.LectureID = s.LectureID and s.LectureID = t.LectureID in stead of Where c.LectureID = s.LectureID = t.LectureID