Explanation of code in c#

Page 1 of 1

14 Replies - 961 Views - Last Post: 29 November 2013 - 11:38 PM Rate Topic: -----

#1 vivek20055  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Explanation of code in c#

Posted 28 November 2013 - 02:59 AM

Hi,

I need to know what this code is doing in c#

private List<string> GetExcelSheetName(OleDbConnection connection)
        {
            List<string> listSheetNames = new List<string>();
            try
            {
                DataTable dtSheet = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                foreach (DataRow drSheet in dtSheet.Rows)
                {
                    if (drSheet["TABLE_NAME"].ToString().Contains("$"))//checks whether row contains '_xlnm#_FilterDatabase' or sheet name(i.e. sheet name always ends with $ sign)
                    {
                        listSheetNames.Add(drSheet["TABLE_NAME"].ToString());
                       
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Could not read Excel sheet names\nDetails: " + ex.Message);
            }
            return listSheetNames;
        }


Thanks,
John

Is This A Good Question/Topic? 0
  • +

Replies To: Explanation of code in c#

#2 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 359
  • View blog
  • Posts: 1,527
  • Joined: 08-April 09

Re: Explanation of code in c#

Posted 28 November 2013 - 03:04 AM

I have seen you post several "what does this code do" topics, so i will answer them all current and future, and the answer is learn to debug.
When you do that you will learn what is going on behind the scenes of what your code does, this is my most sincere honest request, learn to debug. There are debugging tutorials here on DIC.
Here is one by the DIC mentor andrewsw
Was This Post Helpful? 1
  • +
  • -

#3 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Explanation of code in c#

Posted 28 November 2013 - 06:06 AM

Perhaps as well you might learn to read the method names. The method is called "GetExcelSheetName", so chances are it gets the name of an Excel sheet.
Was This Post Helpful? 1
  • +
  • -

#4 vivek20055  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: Explanation of code in c#

Posted 28 November 2013 - 06:11 AM

Hi,

I need to know how it is beng used in the following code???

why I am not getting the result when I am using something like this in the code, which also returns listSheetNames.

private List<string> GetExcelSheetName(OleDbConnection connection)
        {
            List<string> listSheetNames = new List<string>();
            listSheetNames[0] = "0";
            listSheetNames[1] = "1";
            listSheetNames[2] = "2";



            return listSheetNames;
        }


Thanks
John

This post has been edited by vivek20055: 28 November 2013 - 07:21 AM

Was This Post Helpful? 0
  • +
  • -

#5 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 359
  • View blog
  • Posts: 1,527
  • Joined: 08-April 09

Re: Explanation of code in c#

Posted 28 November 2013 - 06:23 AM

listSheetNames contains list of string, the code is using first element of that array as parameter in sql query(which is unsafe) and result of that query will be stored in dataset and connection will be closed.
Was This Post Helpful? 0
  • +
  • -

#6 vivek20055  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: Explanation of code in c#

Posted 28 November 2013 - 06:33 AM

Hi,

Thanks for your reply.without doing all this I just want to fill the adapter with dataset.

private DataSet GetExcelData(string ExcelPath, string ExcelFileName)
        {
            object misValue = System.Reflection.Missing.Value;
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Excel._Worksheet xlWorksheet = xlWorkBook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;


            string Provider = string.Empty;
            string ExtendedProperties = string.Empty;
            List<string> listSheetNames = new List<string>();
            DataSet dataSet = new DataSet();
            OleDbConnection connection = null;
            try
            {
                // for 97-03 Excel file
                if (Path.GetExtension(ExcelFileName).Equals(".xls"))
                {
                    Provider = "Microsoft.Jet.OLEDB.4.0";
                    ExtendedProperties = "Excel 8.0;HDR=Yes;IMEX=1";
                }
                // for 2007 Excel file
                else if (Path.GetExtension(ExcelFileName).Equals(".xlsx"))
                {
                    Provider = ".xlsx";
                    ExtendedProperties = "Excel 12.0;HDR=Yes;IMEX=1";
                }
                try
                {
                    connection = new OleDbConnection("provider =" + Provider + "; Data Source='" + ExcelPath + ExcelFileName + "';" + "Extended Properties=\"" + ExtendedProperties + "\";");
                    connection.Open();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error in opening the file " + ExcelFileName + "\nDetails: " + ex.Message);
                }

                
               //OleDbDataAdapter command = new OleDbDataAdapter("Select * From [" + listSheetNames[0] + "]", connection);
                OleDbDataAdapter command = new OleDbDataAdapter();
                
                
                //command.SelectCommand ;
                command.Fill(dataSet);
                connection.Close();

               
            }
            catch (Exception ex)
            {
                throw new Exception("Error in retrieving data from the file " + ExcelFileName + "\nDetails: " + ex.Message);
            }
            return dataSet;
        }


But when I am trying to fill the adapter it is showing some exception showing. You must initialize the .SelectCommand
before using it?

I used command.SelectCommand ;. But it is stil showing error.

What should be assigned to it?

Thanks
John
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Explanation of code in c#

Posted 28 November 2013 - 08:44 AM

*
POPULAR

What you really need is to stop trying to build this excel reading program you're working on. You really haven't taken any time to actually learn the C# language or underlying concept. Your posts are pretty much all the same: some bit of code you trolled the Internet for then asking others to explain it to you, and to explain what changes need to be made to make it work with the rest of the random code chunks you found on the net.

Enough already. If you aren't intested in actually learning to engineer your software then take up another hobby/career choice.

I can't speak for anyone else here but I won't be answering any more of these "explain me this code I copied" questions. If you should decide to start learning and ask questions related to concepts you are reading about I'd be happy to help you learn. But right now it's clear that isn't your goal.
Was This Post Helpful? 6
  • +
  • -

#8 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 359
  • View blog
  • Posts: 1,527
  • Joined: 08-April 09

Re: Explanation of code in c#

Posted 28 November 2013 - 09:21 AM

He will learn once the book arrives, its gonna be awesome. :)
Was This Post Helpful? 2
  • +
  • -

#9 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 704
  • View blog
  • Posts: 3,019
  • Joined: 03-December 12

Re: Explanation of code in c#

Posted 28 November 2013 - 09:38 AM

Just this month I have purchased 4 books, with plans on getting another when the black Friday crowd clears out. He has shown no interest in learning anything including reading others post on why it does work. I may not be as knowledgeable as others but I won't be helping until some attempt is made with him doing his own research.
Was This Post Helpful? 1
  • +
  • -

#10 vivek20055  Icon User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: Explanation of code in c#

Posted 28 November 2013 - 09:56 AM

Hi,

I am saying truth. This is my order. I booked through the link you have given me.


Hello Vivek Alaparthi,
Thank you for shopping with us. We'd like to let you know that Amazon has received your order, and is preparing it for shipment. Your estimated delivery date is below. If you would like to view the status of your order or make any changes to it, please visit Your Orders on Amazon.com.

Your estimated delivery date is:
Tuesday, December 3, 2013 -
Thursday, December 12, 2013

Your shipping speed:
AmazonGlobal Expedited Shipping

Your Orders
Your order will be sent to:
Vivek Alaparthi
Värdsholmsgatan 1A, apt 1519
Södertälje, Stockholm 15132
Sweden

Get a $10 gift card upon approval for the Amazon.com Store Card
Order Details
Order #104-6505280-8415427
Placed on Saturday, November 16, 2013


Thanks
John
Was This Post Helpful? 0
  • +
  • -

#11 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 704
  • View blog
  • Posts: 3,019
  • Joined: 03-December 12

Re: Explanation of code in c#

Posted 28 November 2013 - 10:14 AM

I will give you the advice my mentor gave me when I started.

"Stop doing your own shit and learn the basics first. You WILL get ahead of yourself and not know how to fix it. Get a book. Read it. Work the examples. Understand what the book presents then play with it to see where you can take it. AFTER you understand the basics you can start small projects, not before."

I didn't want to listen until he stopped helping. Then I had no choice. I understand more now because of it than I did when I would skip chapters just to figure out a solution to my present problem at that time.

You may not listen, but it has served me well and I found I fall back on the basics in everything I do.
Was This Post Helpful? 1
  • +
  • -

#12 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Explanation of code in c#

Posted 28 November 2013 - 03:45 PM

Also why do you keep calling yourself 'John' all the time?
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,158
  • Joined: 05-May 12

Re: Explanation of code in c#

Posted 28 November 2013 - 07:14 PM

Hopefully the folks over at CodeProject can tide you over until your book arrives. It looks like they are still replying... http://www.codeproje...ns&mid=10408451

(Link courtesy of JackOfAllTrades.)

Anyway, if you took Michael26's advice in post #2, you would quickly be able to tell why your code in post #4 is not working.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,158
  • Joined: 05-May 12

Re: Explanation of code in c#

Posted 28 November 2013 - 07:21 PM

View Postvivek20055, on 28 November 2013 - 08:33 AM, said:

Hi,

Thanks for your reply.without doing all this I just want to fill the adapter with dataset.

private DataSet GetExcelData(string ExcelPath, string ExcelFileName)
        {
:
                //command.SelectCommand ;
:
        }

[/spoiler]
But when I am trying to fill the adapter it is showing some exception showing. You must initialize the .SelectCommand
before using it?

I used command.SelectCommand ;. But it is stil showing error.


No, you didn't. The line of code of commented out. If you were taking time to learn C#, you would know that commented out code does not execute.

View Postvivek20055, on 28 November 2013 - 08:33 AM, said:

What should be assigned to it?


Did you even take time to try to research on MSDN? See here: http://msdn.microsof...(v=vs.110).aspx

Oh yeah. I forgot. You are using us as your personal research team.
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Explanation of code in c#

Posted 29 November 2013 - 11:38 PM

Enough beating on the OP. This thread shows no more promise of productivity. Closing thread
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1