7 Replies - 310 Views - Last Post: 30 April 2013 - 09:13 AM Rate Topic: -----

#1 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 22-April 13

Column pulling

Posted 29 April 2013 - 12:21 PM

I've been working on this project. I have the column names written to a text file and the data is supposed to go with it. Only problem is that when I try and get the data for formatting, the column name reappears several times. Here is an example of how the relevant code is set up:
for (int i = 0; i < Columns.Count; i++) {
                        if (cfsNumber.Length < FIFTY)
                        { //Starts if
                            spacing = FIFTY - cfsNumber.Length;
                            sw.Write(cfsNumber.PadRight(spacing) + "|");
                            sw.Flush();
                        } //Ends if
                        else
                        { //Starts else
                            sw.Write(cfsNumber.Substring(ZERO, FIFTY) + "|");
                            sw.Flush();
                        } //Ends else
                    }
The if statement is within a switch based off of the variable "i" in the for condition. A sample of the variable that is within the if statement is as follows:
string cfsNumber = data.Column["CFSNUMBER"].ToString();
I don't know what I'm missing and I've tried looking on MSDN and other sites. I've read a book by Jon Skeet on C# so I have the basic syntax down and how things work to a certain point but I'm not sure what is going on with the way the compiler is interpreting it. Would anyone know a possible solution to this problem? It has driven me up the wall and I've been working on it for days.

Is This A Good Question/Topic? 0
  • +

Replies To: Column pulling

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Column pulling

Posted 29 April 2013 - 01:20 PM

Where are you trying to output any data here? All I see is that you loop for every column in the table, then output the string "cfsNumber" either padded or a substring each time. Is that a string holding your column names? Maybe I'm just not getting what you're trying to do in that loop.
Was This Post Helpful? 0
  • +
  • -

#3 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 22-April 13

Re: Column pulling

Posted 29 April 2013 - 05:24 PM

View PostMrShoes, on 29 April 2013 - 01:20 PM, said:

Where are you trying to output any data here? All I see is that you loop for every column in the table, then output the string "cfsNumber" either padded or a substring each time. Is that a string holding your column names? Maybe I'm just not getting what you're trying to do in that loop.

Yes the cfsNumber is a string but it's supposed to hold the data within the column. I've never worked with DataTable's much and I'm trying to output the data to a text file via a file and stream writer. I've got the column names to output to the text as this is a back up to a text file. I would think that data.Column["ColumnName"].ToString() would call in all the data within that column to a string and output to the text file but that didn't happen as so. It only did the column names repeatedly. Like HGT HGT HGT HGT HGT, etc.
Was This Post Helpful? 0
  • +
  • -

#4 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Column pulling

Posted 30 April 2013 - 05:42 AM

How is "data" assigned? It seems more like a DataRow to me - the first row of the file, hence the column indexed "CFSNUMBER" has returned the header for that column. So, you have just one string in the cfsNumber variable, and you're repeating it once for each column in the datatable.

Again, some of this is guess work. Can you show me where you're reading the text file contents in?
Was This Post Helpful? 0
  • +
  • -

#5 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 22-April 13

Re: Column pulling

Posted 30 April 2013 - 06:42 AM

View PostMrShoes, on 30 April 2013 - 05:42 AM, said:

How is "data" assigned? It seems more like a DataRow to me - the first row of the file, hence the column indexed "CFSNUMBER" has returned the header for that column. So, you have just one string in the cfsNumber variable, and you're repeating it once for each column in the datatable.

Again, some of this is guess work. Can you show me where you're reading the text file contents in?

I'm not reading a file. I'm writing to the file. The writing works. The variable data is a DataTable that I'm trying to pull the information from to write to the text file.

And yes I have one variable for cfsNumber that is supposed to read all of the information in that column.
Was This Post Helpful? 0
  • +
  • -

#6 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 22-April 13

Re: Column pulling

Posted 30 April 2013 - 06:50 AM

I'm going to also take a guess that SQL style syntax for a DataTable doesn't work correctly either for data pulling. I used SQL to pull the data off the server and then I put it in the DataTable.
Was This Post Helpful? 0
  • +
  • -

#7 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Column pulling

Posted 30 April 2013 - 08:41 AM

OK. So again I'm making some assumptions, but if "data" is a DataTable type, then what you're calling ToString() on is the column indexed "CFSNUMBER". As you see on the MSDN Docs (here: http://msdn.microsof...n.tostring.aspx - the link function isn't working!) calling ToString() on a column will return its "expression" if it has one, or its "ColumnName" property.

What you'll need to do is iterate through every value in that column then either build a string using those values, or a string array.
Was This Post Helpful? 1
  • +
  • -

#8 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 74
  • Joined: 22-April 13

Re: Column pulling

Posted 30 April 2013 - 09:13 AM

View PostMrShoes, on 30 April 2013 - 08:41 AM, said:

OK. So again I'm making some assumptions, but if "data" is a DataTable type, then what you're calling ToString() on is the column indexed "CFSNUMBER". As you see on the MSDN Docs (here: http://msdn.microsof...n.tostring.aspx - the link function isn't working!) calling ToString() on a column will return its "expression" if it has one, or its "ColumnName" property.

What you'll need to do is iterate through every value in that column then either build a string using those values, or a string array.

This was helpful. Thank you so much for your help and patience. I will implement this solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1