CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Need help on concept (getting row numbers)

    Hi again,
    the further I develop my small helper tool, the more details I need to get along with.


    My current problem:
    I read the columns from a MDB file and display them in some textboxes in the Form.
    Since the MDB file has some few rows, the main entry with the name is displayed in a treeview like this:

    MDB:

    ID | Name | Info
    ---------------------
    1 | Test | Info1
    2| TestA | Info2
    3 | TestB | Info3



    GUI (treeview):
    |---Test
    |---TestA
    |---TestB


    Depending on the treeviews selection I change the content of the textboxes.
    I do this so far by retrieving the SelectedNode.Index and look up the MDB for WHERE ID = index.
    This works fine.

    But I need a way to be independent of the SelectedNode.Index, cause I will implement some
    filters so the treeview contains not all available entries and the indexes will be messed up.


    How can I figure out the ID (or the row number) depending of a given Name (second column from MDB).

    For example: I want the ID from 'TestA'. How can I do this?
    I cannot use reader.GetString(0); to get the autonumbered values from "ID". It does not work. The very first valid column I can access is "Name" by reader.GetString(1) :-(

    Some hints?

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Need help on concept (getting row numbers)

    You should use GetInt32 (0) because the ID columns seems to be an integer not a string.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Need help on concept (getting row numbers)

    the node has a Tag property to store some information, you may use that to store the corresponding ID from your MDB.

    hth
    Busy

  4. #4
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Need help on concept (getting row numbers)

    sample vb code:
    Code:
            Dim dt As New DataTable()
    
            Using cn As New OleDbConnection("<Connection string>")
                cn.Open()
                Using cm As OleDbCommand = cn.CreateCommand()
                    cm.CommandType = CommandType.Text
                    cm.CommandText = "SELECT ID, [Name] FROM [<Table name>]"
                    Using rd As OleDbDataReader = cm.ExecuteReader(CommandBehavior.CloseConnection)
                        dt.Load(rd)
                        rd.Close()
                    End Using
                End Using
            End Using
    
            For Each dr As DataRow In dt.Rows
                treeView1.Nodes.Add(dr("Name").ToString()).Tag = dr("ID").ToString()
            Next
    and to get the corresponding ID from the selected node to be passed on to the command parameter:

    Code:
    cm.Parameters("@ID").Value = Convert.ToInt32(treeView1.SelectedNode.Tag)

    hth
    Busy

  5. #5
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Need help on concept (getting row numbers)

    thanks to all,

    getting Int32(0) worked fine. I do not know why I solved this issue not by my own :-/

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

Featured