Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8411437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:25:20+00:00 2026-06-10T00:25:20+00:00

I have a Winforms TreeView and I wish to store the node data into

  • 0

I have a Winforms TreeView and I wish to store the node data into an SQLite table, so that I can restore the TreeView when the application starts up next time. I use the TreeView to drive most of the desktop application and use index pointer fields within a class attached to the TreeNodes to access other tables in the database when a user clicks on a node.

The problem I am trying to avoid is a lot of “round tripping” between the TreeView and the TreeNode_Table everytime a new TreeNode is created. It would be best if I know ahead of time what the next autoincrement primary key will be in the TreeNode_Table before I create the TreeNode. That way I can create and add the node to the tree then copy the data into a new TreeNode_Table row in a single pass. No round trips between the table and the TreeView to sync up values like the primary key, node level, node index, and other node and row values.

I tried:

string strFindLastRowInserted = @"SELECT MAX([id]) FROM [TreeNode_Table];";
var findLastRowInsertedCmd = new SQLiteCommand(strFindLastRowInserted, _cnn);
object resultObj = findLastRowInsertedCmd.ExecuteScalar();
findLastRowInsertedCmd.Dispose();
if( resultObj != null)
NodeTagDataObj.DataPtr = (long)resultObj + 1;    

But adding one to MAX([id]) breaks down if some of the most recently created nodes/rows have been previously deleted. I’m using the System.Data.Sqlite library to access the data file.

Thank you for your help. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:25:21+00:00Added an answer on June 10, 2026 at 12:25 am

    I finally figured this out by probing around within Sqlite’s table metadata. To reliably obtain the next autoincrement key value before the new row is created, the following SQL can be used:

    SELECT [seq] + 1 FROM [sqlite_sequence] WHERE [name] = 'my_table_name';
    

    I added and tested the following static helper method to my SqliteHelper class and everything works as expected and the logic survives recent row deletions, disconnections, and application restarts.

    SqliteHelper.cs:

    internal static long GetNextAutoincrementValue(SQLiteConnection cnn, 
                                                   string tableName)
    {
        long returnValue = -1;
        SQLiteCommand myCommand = cnn.CreateCommand();  
        myCommand.CommandText = 
            @"SELECT [seq] + 1 FROM [sqlite_sequence] WHERE [name] = @MyTableName;";
    
        SQLiteParameter myParam = new SQLiteParameter("@MyTableName",
                                                      System.Data.DbType.String);
        myParam.Value = tableName.Trim();
        myCommand.Parameters.Add(myParam);
        object resultObj = myCommand.ExecuteScalar();
        myCommand.Dispose();
        if( resultObj != null)
            returnValue = (long)resultObj;
        return returnValue;
    }
    

    I hope that others will find this useful…

    -DougC

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForms TreeView with one main node and several sub-nodes. How can
I want to make in a TreeView (winforms) that each node will have in
I have a TreeView control in my WinForms .NET application that has multiple levels
I have a winforms TreeView control that allows you to browse an object hierarchy.
I have a Winforms application (written in C#) that is deployed on a network
I have a winforms application and I am trying to create a method that
I'm experimenting with a treeview in a little C#/Winforms application. I have programatically assigned
In my winform application I have a treeview. To give the idea that a
In a WinForms application, I have a datagrid that is associated to a datasource.
I have UserControl that holds Infragistics Graph control. On the TreeView sub node's right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.