troubleshooting Question

using odbc datareader and calling a function and returning the datareader results c#

Avatar of sydneyguy
sydneyguyFlag for Australia asked on
.NET ProgrammingC#Visual C++.NET
2 Comments1 Solution847 ViewsLast Modified:
i am wishing to split my code so that i call the function that opens the OdbcDataReader and then returns the results of the found data. i have shown the code that is running in the fist module
where all the code exists on one function,
private void build_Accounts_Click(object sender, EventArgs e)

what i want to do is to split this function into two
1)
the main start and calling function
private void build_Accounts_Click(object sender, EventArgs e)
2)
the reader and with the returning results
private static SqlDataReader AccessDB(string sql, string ODBCDB)

i am not sure how to structure the header for the
private static ************   AccessDB(string sql, string ODBCDB)
and the return of the datareader results
thanks for any help you can give me



******************   working code *****************************
 private void build_Accounts_Click(object sender, EventArgs e)
        {
            int setrogo = testforminput();
            if (setrogo == 0)
            {
            }
            else
            {
                string BusinessType = BusinssType.Text;
                int recnum = 0;
                string LeadName = "";
                OdbcConnection DbConnection = new OdbcConnection("DSN=newvtiger22-11v2");
                //  Now open that OdbcConnection object:
                DbConnection.Open();
                // Now using the OdbcConnection, we will create an OdbcCommand:
                OdbcCommand DbCommand = DbConnection.CreateCommand();
                // Using that command, we can issue a Query and create an OdbcDataReader:
                DbCommand.CommandText = "SELECT account_no, accountid, accountname FROM vtiger_account" ;
                OdbcDataReader DbReader = DbCommand.ExecuteReader();
               // Now we have an OdbcDataReader, we can find the number and names of the columns in the result set, and display this on the console.
                OdbcDataReader DbReader = AccessDB("SELECT account_no, accountid, accountname FROM vtiger_account", "newvtiger22-11v2");
                int fCount = DbReader.FieldCount;
                Console.Write(":");
                for (int i = 0; i < fCount; i++)
                {
                    String fName = DbReader.GetName(i);
                    // String wName = DbReader.GetString(i);
                    Console.Write(fName + ":");
                }
                Console.WriteLine();

                //# Now we can read each row from the result set, and read each column in that row and display its value.
                string[] recinfo; // ARRAY TO HOLD THE REC INFO FOR POSTING OFF
                while (DbReader.Read())
                {
                    String col = "";
                    Console.Write(":");

                    // THIS GOES THROUGH THE BITS THAT ARE IN THE ARRAY
                    recinfo = new string[fCount];  // numbers is a fCount-element array
                    for (int i = 0; i < fCount;  i++)
                    {
                         Console.Write(col + ":");
                    }

                }
            }
            MessageBox.Show("Process Finished");

        }

************************************************************

*************  new code that i wish to split *********************
private void build_Accounts_Click(object sender, EventArgs e)
        {
            int setrogo = testforminput();
            if (setrogo == 0)
            {
            }
            else
            {
                string BusinessType = BusinssType.Text;
                int recnum = 0;
                string LeadName = "";

                OdbcDataReader DbReader = AccessDB("SELECT account_no, accountid, accountname FROM vtiger_account", "newvtiger22-11v2");
                int fCount = DbReader.FieldCount;
                Console.Write(":");
                for (int i = 0; i < fCount; i++)
                {
                    String fName = DbReader.GetName(i);
                    // String wName = DbReader.GetString(i);
                    Console.Write(fName + ":");
                }
                Console.WriteLine();

               
            }

        }
         catch (Exception ex)
        {
                            //   MessageBox.Show("cannot access and up date contactlinkedin");
                            // IF NO VALUE DO NOTHING AND LET IT GLOSS OVER
        }

         Console.Write(col + ":");
}


private static SqlDataReader AccessDB(string sql, string ODBCDB)
        {
            OdbcConnection DbConnection = new OdbcConnection("DSN=newvtiger22-11v2");
            //  Now open that OdbcConnection object:
            DbConnection.Open();
            // Now using the OdbcConnection, we will create an OdbcCommand:
            OdbcCommand DbCommand = DbConnection.CreateCommand();
            // Using that command, we can issue a Query and create an OdbcDataReader:
            DbCommand.CommandText = "SELECT account_no, accountid, accountname FROM vtiger_account"; //WHERE ((vtiger_account.prosch)='" + BusinessType + "') and ((contactlinkedin.done)=0)";
            OdbcDataReader DbReader = DbCommand.ExecuteReader();
            return DbReader;

        }
ASKER CERTIFIED SOLUTION
Swapnil
Software Architect
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros