Solved

Vb.net Datatable -> if statement to check if the value exists then amend the table

Posted on 2014-02-05
3
1,985 Views
Last Modified: 2014-02-05
Hi ALL,

Below if the code i have so far and I have 2 questions really....

1) i have an error "BC30311: Value of type 'System.Collections.Generic.IEnumerable(Of System.Data.DataRow)' cannot be converted to 'Boolean'." on row If scorestable.Rows.Cast(Of DataRow)().Where(Function(n) n("Score").ToString() = thescorestext then


2) Can i do this with less code and make it quicker/neater?

I have commented the below code to show what i wanted it to do....

      
 	'Check to see if anyrows in HomeTeam contain a value
	 Dim count = scorestable.Rows.Cast(Of DataRow)().Where(Function(n) n("HomeTeam").ToString() = StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText)).Count()
'Find the count
if count = 0 then

'add row to the datatable as no results where found
scorestable.Rows.Add(StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText), div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2) , thescorestext)

else
'A matching result was found so now check to see if the Scores section of the result matches the value - thescorestext

If scorestable.Rows.Cast(Of DataRow)().Where(Function(n) n("Score").ToString() = thescorestext then
'Yes it does match the score so we just need to update the column of the row with the correct time
Dim scoresRow() As Data.DataRow
'Make sure i have selected the correct team
scoresRow = scorestable.Select("HomeTeam = StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText)")
'Update the time row'
scoresRow(0)("Time") = div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2)

else

'The score does not match meaning i need to update both the score and the time

Dim scoresRow() As Data.DataRow
'Make sure i have selected the correct team
scoresRow = scorestable.Select("HomeTeam = StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText)")
'update score
scoresRow(0)("Score") = thescorestext
'Update time
scoresRow(0)("Time") = div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2)
end if

end if

Open in new window

0
Comment
Question by:runnerjp2005
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39835405
Hi runnerjp2005;

In the following If statement the test needs to return a Boolean type.

If scorestable.Rows.Cast(Of DataRow)().Where(Function(n) n("Score").ToString() = thescorestext then

Open in new window

The Test
scorestable.Rows.Cast(Of DataRow)().Where(Function(n) n("Score").ToString() = thescorestext

Open in new window

is missing a ")" at the end of the statement. But even if you put a ")" it returns a Collection of DataRows and NOT a Boolean. You need to change the statement so that it returns a Boolean something like the following.
scorestable.Rows.Cast(Of DataRow)().Where(Function(n) n("Score").ToString() = thescorestext).Count > 0

Open in new window

0
 

Author Comment

by:runnerjp2005
ID: 39835508
now i get  Identifier expected on scoresRow = scorestable.Select("HomeTeam = StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText)")
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39835706
Try following


scorestable.Rows.Cast(Of DataRow)().Where(Function(n) n("Score").ToString())(0)("Score") = thescorestext

Another option is to use dataview

scorestable.DefaultView.RowFilter = "Score='" & thescorestext & "'"
if scorestable.DefaultView.Count > 0 Then
   'score found
else
   'not found
End If
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can Angular be used with classic asp.net? 3 29
Validating textboxes in red square borders. 2 32
vb.net sendkey 4 17
Set the FileVersion of a website dll 1 20
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question