hi guys! help please..I have a Datatable with 3 columns (id,username,password) how will i check if a particular value say, 1 ,does exist in column id of my Datatable? Thanks in advance!
Printable View
hi guys! help please..I have a Datatable with 3 columns (id,username,password) how will i check if a particular value say, 1 ,does exist in column id of my Datatable? Thanks in advance!
Assuming ID is the primary key, call the Find method of the Rows collection. If a DataRow object is not returned then the key value doesn't exist.
Unfurtunately John the datatbale does not have a primary key. hence, ID is not a primary key.I just come around with this solution..
Do you have suggestion or comment about this? Thanks in advance.Code:public bool FuncIsSubjExistInSubjDataTable(string subjID)
{
for (int i = 0; i < dtSubject.Rows.Count; i++)
{
if (dtSubject.Rows[i][0].ToString() == subjID)
return true;
}
return false;
}
You can use a DataView in that case. If the table isn't bound to any controls then use the DefaultView, otherwise create your own DataView:Sorry, wrote that code before I realised I was in C# land. You get the idea though. You can sort a DataView on a column or columns and then get the index of the first row that has specific values in those columns.vb.net Code:
table.DefaultView.Sort = "ID ASC" If table.DefaultView.Find(id) = -1 Then 'The ID value does not exist. Else 'At least one row exists with that ID value. End If
yah no prob...Im somehow familiar with VB..Thanks for that john..