Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding and displaying a particular row from the dataset

Status
Not open for further replies.

jennyek2000

Programmer
Sep 2, 2003
43
0
0
GB
I type in the following code in order to display a particular row in some bound text boxes. SearchIndex is a variable that represents the jobNumber of a row selected on a datagrid on a different screen and I want to be able to display the record on the text boxes.

bmjobs.Position = mydataset.Tables("Jobs").Rows.Find("JobNumber = '" & myfrmSearchjobs.searchIndex & "'").ToString

However, when I run this code. It says that "Table doesnt have a primary key" which it does! (in fact, JobNumber IS the primary key).

Can anyone tell me what to do and why this is happening?
 
Jenny

If your primary key is defined correctly, you dont need to include the fieldname in your find command. Is your Primary key defined as a String or Integer?

In an example where it is defined as an Integer, the following code will suffice.

Code:
'To Find the Row
findrow = ds.Tables("Jobs").Rows.Find(LenPkey)
'To access the row
dim myvar as Integer = findrow("LenPKey")
'To make changes to the row
findrow("cutstatus") = LenTempCut


WTrueman
...if it works dont mess with it
 


Hi

when you say you have a primary key set up, have you told the data table what the primary key is ?

ie, you specify the primarykey property for the datatable?

Code:
Dim myColArray(1) As DataColumn
myColArray(0) = MyDataTable.Columns("My_Primary_Key_Field")
MyDataTable.PrimaryKey = myColArray

You should then be able to do a find on the datatable, in this case I only have one table in the dataset, so I reference it as tables(0) eg

Code:
MyDataSet.Tables(0).Rows.Find(SomeIDValue)


 
Hi jenny
its the code in which u can selcect the row depending upon the ColumnName and DataTable in which to search
Regards
Nouman
Public Function getRow(ByVal strColumnName As String, ByVal strColumnValue As String, ByVal dtBindedDataSet As DataTable) As Object
Try
Dim findRow() As DataRow
Dim strSql As String
If IsNumeric(strColumnValue) Then 'If its is digit
strSql = strColumnName & " = '" & Trim(strColumnValue) & "'"
Else
strSql = strColumnName & " like '" & Trim(strColumnValue) & "%'"
End If

findRow = dtBindedDataSet.Select(strSql)
If findRow.Length = 0 Then
getRow = Nothing
Else
getRow = findRow
End If
Catch exp As Exception
MsgBox(exp.Message, MsgBoxStyle.Information, "Information")
End Try
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top