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

  • Home
  • SEARCH
  • 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 6953887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:32:07+00:00 2026-05-27T14:32:07+00:00

I wrote some code that should add database response to the List. it works

  • 0

I wrote some code that should add database response to the List. it works on my PC, but does not work when I deploy my program to another PC.
I pinned down to my method. It stops working when it hits

int tot = rs.Fields.Count;

Here is my code:

        public static List<List<String>> QUERY(String query, String dbPath)
    {   ADODB.Connection cn = new ADODB.Connection();
        ADODB.Recordset rs = new ADODB.Recordset();            ADODB.Command cmdSQLData = new ADODB.Command();
        List<List<String>> RETURNME = new List<List<String>>();
        string cnStrOld = "Driver={Microsoft Access Driver (*.mdb)}; Dbq=" + dbPath + ";Uid=;Pwd=;"; //does not work
        string  cnStr = @"Provider=Microsoft.JET.OLEDB.4.0; data source=" + dbPath;
            cn.ConnectionTimeout = 0;
            cn.Open(cnStr);
            cn.CommandTimeout = 0;
            rs.Open(query, cn);
        while (rs.EOF == false) //GET HEADERNAMES, ADD TO LIST
        {
            List<String> A = new List<string>();
            int tot = rs.Fields.Count;// calculating the amount of columns in the RS
            for (int i = 0; i < tot; i++) //iterating through all columns and checking it's name
            {
                A.Add(rs.Fields[i].Name.ToString());
            }
            RETURNME.Add(A);
            break;
        }
        while (rs.EOF == false)//GET DATA, ADD TO LIST
        {
            List<String> B = new List<string>(); //list of Data
            int tot = rs.Fields.Count;// calculating the amount of columns in the RS
            //Now we add query response
            for (int i = 0; i < tot; i++) //iterating through all columns and checking it's name
            {
                B.Add(rs.Fields[i].Value.ToString());
            }
            RETURNME.Add(B);
            rs.MoveNext();
        }
            rs.Close();
            cn.Close();
        return RETURNME;
    }

I use relative paths and they are tested OK. I also have my try-catch staements (I removed them from here to shrink the code) and they indicate no errors. Somehow program is capable of entering to “while (rs.EOF == false)” statement, so I assume that records are returned?

Could you please assist?


I ended up with the following solution:

        public static List<List<String>> QUERY_TEST(String query, String dbPath)
    {

        List<List<String>> RETURNME = new List<List<String>>();
        String cnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath + ";Jet OLEDB:Database Password=;";
        OleDbDataAdapter Data1 = new OleDbDataAdapter(query, cnStr);

        DataSet a = new DataSet();
        Data1.Fill(a);
        DataTable dt = a.Tables[0];

        //Adding column names to the first row on the list
        List<String> B = new List<string>();
        foreach (DataColumn dr in dt.Columns)
        {
            List<String> A = new List<string>();
            B.Add(dr.Caption.ToString());
        }
        RETURNME.Add(B);

        //Adding data to the columns
        foreach (DataRow dr in dt.Rows)
        {
            List<String> A = new List<string>();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                A.Add(dr[i].ToString());
            }
            RETURNME.Add(A);
            break;
        }
        return RETURNME;
    }
  • 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-05-27T14:32:08+00:00Added an answer on May 27, 2026 at 2:32 pm

    basically this is what you could do.. of course you need to replace certain things with your variable names where necessary

    public static List<string> QUERY(string query, string dbpath)
    {
       string  cnStr = @"Provider=Microsoft.JET.OLEDB.4.0; data source=" + dbPath;
       oleconn = new OleDbConnection(cnStr);
       List<string>lstColumns = null;
       string[] strarryColumnNames = {}; //keep this just like this it's how you can declare dynamic array
       string strCommaDelimColumns = string.Empty;
       OleDbDataReader drdrRecord = null;
       OleDbConnection oleconn = null;
    
       using(OleDbCommand  olecmd new OleDbCommand(query, cnStr))
       {
            olecmd.CommandTimeout = 60;
            olecmd.CommandType = System.Data.CommandType.Text;  
            oleconn.Open();
            //if you want to get the record count just setup a query with Select (count) as recCnt .... 
            //do the oledrd execute here.. then call oledrdr.Close(); and assign the other query string to another execute reader
            /* basically do something like this and replace what I have with what you need
                var drdrRecordCntReader = olecmd.ExecuteReader();
                oledrdr.Read();
                intRecCount = (int)oledrdr[recCnt];//value returned from the Select Count(*)
                if (intRecCount == 0)
                {
                    return false;
                }//if (intRecCount == 0)        
            */
            oledrdr = olecmd.ExecuteReader();
            lstColumns = new List<string>();
            //load the field header contents here 
            for (int intCounter = 0; intCounter < oledrdr.FieldCount; intCounter++)
            {
              lstColumns.Add(oledrdr.GetName(intCounter));
            }       
            strarrayColumnNames = lstColumns.ToArray();     
            strCommaDelimColumns = string.Join(",", strarryColumnNames);
            //use the same lstColumns to add the data no need for a second while loop
            //close the reader oledrdr
            //use it again
            try
            {
                 //outter loop - Read row record by record 
                for (int intCounter = 0; intCounter < intRecCount - 1; intCounter++) // figure out how to get record count
                {
                   rdrDataReader.Read();
                 //inner loop - read each field data within that row
                     for (int intFieldcnt = 0; intFieldcnt < intColumnCnt; intFieldcnt++)
                     {
                        //put your field data value code here
                     }//for (int intFieldcnt = 0; intFieldcnt < intColumnCnt; intFieldcnt++)
                 }
             }
             catch (Exception ex)
             {
    
             }
        }           
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wrote some javascript code that filled a drop down list based on
So, I know some Python but I thought that I should try to add
So far I have managed to write some code that should print the source
I wrote some code with a lot of recursion, that takes quite a bit
I've got some code that lies in a browser, and wrote C++ plugins for
I wrote some naive code(in the sense that it's synchronous calls) for a tableview
I am testing some weird-looking CSS code that I wrote (I'm using a mix
I tested this block of code and find that the GetInts method does not
I have wrote some code in order to add 100 x 100 cells to
I just wrote some code, looked it over, and wanted to cry. Lines that

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.