Results 1 to 5 of 5

Thread: Check if Item exist on my datatable

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Check if Item exist on my datatable

    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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check if Item exist on my datatable

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Check if Item exist on my datatable

    Unfurtunately John the datatbale does not have a primary key. hence, ID is not a primary key.I just come around with this solution..
    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;
            }
    Do you have suggestion or comment about this? Thanks in advance.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check if Item exist on my datatable

    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:
    vb.net Code:
    1. table.DefaultView.Sort = "ID ASC"
    2.  
    3. If table.DefaultView.Find(id) = -1 Then
    4.     'The ID value does not exist.
    5. Else
    6.     'At least one row exists with that ID value.
    7. End If
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Check if Item exist on my datatable

    yah no prob...Im somehow familiar with VB..Thanks for that john..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width