C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Databinding »

VB.NET - Search For Records in Data Table


Posted Date: 20 Jun 2009    Resource Type: Code Snippets    Category: Databinding
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 10



The following VB.Net code is used to find for a particular record in the data table based on specific column.

The Find Method of Data Table does the trick.

It will return the Matching Data Row if matching record exists.



Imports System.Data.SqlClient 'Required name space for sql connection

Private Sub PopulateDataGrid()

'Variables Declaration
Dim ConnString As String
Dim conn As SqlConnection

Dim da As SqlDataAdapter
Dim ds As New DataSet
Dim SqlCmd As SqlCommand


ConnString = "Server = localhost;Database = EmployeeDB; Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

da = New SqlDataAdapter("Select * from Employees", conn)
da.Fill(ds, "Employees")

ds.Tables(0).PrimaryKey = New DataColumn(ds.Tables(0).Columns("FirstName"))
DataGrid1.DataSource = ds.Tables(0)



End Sub

'Check For employee with the First Name as the passed name
Private Function CheckForEmployee (ByVal searchName As String) As Boolean


Dim drMatchRow As DataRow

'Find For Particular Employee With the given name
drMatchRow = ds.Tables(0).Rows.Find(searchName)


If drMatchRow Is Nothing Then
Return False
Else
lblMessage.Text = drFind.Item("EmployeeID")
Return True
End If


End Function





CheckForEmployee ("Jack") will return "2001" - Employee Id of Jack


Just for simplicity I have given one key column. But in real scenario, we need to use combination of Columns as Key.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
VB.NET - Search For Records in Data Table  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: VB.NET: Comparison of data tables based on Specific Column
Previous Resource: Storing and Retrieving Images/Files In Sql Server - VB.NET
Return to Discussion Resource Index
Post New Resource
Category: Databinding


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use