Solved

VB.net Datatable ->  check if column contains value before adding it

Posted on 2014-02-05
4
3,890 Views
Last Modified: 2014-02-05
Hello all,

I have a datable and I want to check if the column contains a value before adding a row...

so...

If scorestable.Columns("HomeTeam") contains value = "Myteam" then

'Do something

else

 scorestable.Rows.Add(StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText), div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2) , thescorestext)

End if

That way I don't get repeat values
0
Comment
Question by:runnerjp2005
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 39835202
Assuming that you mean you need to check the value of each row in that column, how about:
For each dr as DataRow in scorestable.Rows
  If dr.Item("HomeTeam").ToString.Contains("Myteam") Then
       ' value found - do something, e.g.
       Trace.Warn("Value Exists")
    Exit For
  End If
Next

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39835205
using linq:
  Dim count = scorestable.Rows.Cast(Of DataRow)().Where(Function(n) n("HomeTeam").ToString() = "Myteam").Count()
if count = 0 then

'add row
scorestable.Rows.Add(StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText), div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2) , thescorestext)

else
'Do something
end if

Open in new window

0
 

Author Comment

by:runnerjp2005
ID: 39835247
i used the below code but its not adding anything to the table

dim DVScores as new dataview(scorestable)

For each dr as DataRow in DVScores
  If dr.Item("HomeTeam").ToString.Contains(StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText)) Then
       ' value found - do something, e.g.
	   else
	   
   scorestable.Rows.Add(StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText), div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2) , thescorestext)
		
    Exit For
  End If
Next

Open in new window

0
 

Author Closing Comment

by:runnerjp2005
ID: 39835261
Works great although not used Linq before- something I will look into thanks
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

713 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