Solved

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

Posted on 2014-02-05
4
3,993 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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