none
Pushing a Tab Delimited Text File to a DataTable in Visual Studio C# RRS feed

  • Question

  • I am new to C# and Visual Studio. I am currently stuck on a problem in developing code to push a tab delimited text file into a DataTable. As of now the code pulls the txt. file from my laptop reads all the lines and gives me two results based on the conditions within the code. Now that I have completed that I am hoping to push the full txt. file into a datatable to use for later purposes. If somebody is able to help clarify any of my errors I would greatly appreciate it. I am posting the code below.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Data;
    
    namespace ConsoleApp66
    {
        class Program
        {
            static void Main(string[] args)
            {
                string filePath = @"C:\Rexel\Test.txt";
    
                List<string> Amount = File.ReadAllLines(filePath).ToList();
                string[] row;
                foreach (string line in Amount)
                {
                    row = line.Split('\t');
                    if (Convert.ToDecimal(row[12]) > 0 && Convert.ToDecimal(row[12]) < 25000)
                    {
                        Console.WriteLine("Payment Amount: " + row[12]);
                        Console.WriteLine("Status: Ready to Send");
                    }
                    else if (Convert.ToDecimal(row[12]) >= 25000)
                    {
                        Console.WriteLine("Payment Amount: " + row[12]);
                        Console.WriteLine("Status: Needs Approval");
                    }
    
                    public DataTable ConvertToDataTable(int numberOfColumns, string filePath1)
    
                    {
                        DataTable tbl = new DataTable();
    
                        for (int col = 0; col < numberOfColumns; col++)
                            tbl.Columns.Add(new DataColumn("Column" + (col + 1).ToString()));
    
                        string[] lines = System.IO.File.ReadAllLines(filePath1);
    
                        foreach (string line1 in lines)
                        {
                            var cols = line1.Split(':');
    
                            DataRow dr = tbl.NewRow();
                            for (int cIndex = 0; cIndex < 3; cIndex++)
                            {
                                dr[cIndex] = cols[cIndex];
                            }
    
                            tbl.Rows.Add(dr);
                        }
    
                        return tbl;
                    }
                }
            }
        }
    }

    Tuesday, November 13, 2018 5:07 PM

Answers

  • You have a method in a method and the inside is marked as public. Should be (and this is for syntax only as I don't know your logic). Also you never call the internal method.

    This will compile.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Data;
    
    namespace ConsoleApp66
    {
        class Program
        {
            static void Main(string[] args)
            {
                string filePath = @"C:\Rexel\Test.txt";
    
                List<string> Amount = File.ReadAllLines(filePath).ToList();
                string[] row;
                foreach (string line in Amount)
                {
                    row = line.Split('\t');
                    if (Convert.ToDecimal(row[12]) > 0 && Convert.ToDecimal(row[12]) < 25000)
                    {
                        Console.WriteLine("Payment Amount: " + row[12]);
                        Console.WriteLine("Status: Ready to Send");
                    }
                    else if (Convert.ToDecimal(row[12]) >= 25000)
                    {
                        Console.WriteLine("Payment Amount: " + row[12]);
                        Console.WriteLine("Status: Needs Approval");
                    }
    
                    DataTable ConvertToDataTable(int numberOfColumns, string filePath1)
                    {
                        DataTable tbl = new DataTable();
    
                        for (int col = 0; col < numberOfColumns; col++)
                            tbl.Columns.Add(new DataColumn("Column" + (col + 1).ToString()));
    
                        string[] lines = System.IO.File.ReadAllLines(filePath1);
    
                        foreach (string line1 in lines)
                        {
                            var cols = line1.Split(':');
    
                            DataRow dr = tbl.NewRow();
                            for (int cIndex = 0; cIndex < 3; cIndex++)
                            {
                                dr[cIndex] = cols[cIndex];
                            }
    
                            tbl.Rows.Add(dr);
                        }
    
                        return tbl;
                    }
                }
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by DB161998 Tuesday, November 13, 2018 9:20 PM
    Tuesday, November 13, 2018 8:38 PM
    Moderator

All replies

  • Hello,

    Is the code working or not working?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, November 13, 2018 5:22 PM
    Moderator
  • The code is not working, it is giving me one error CS0106 and a warning code of CS8321
    Tuesday, November 13, 2018 6:33 PM
  • You have a method in a method and the inside is marked as public. Should be (and this is for syntax only as I don't know your logic). Also you never call the internal method.

    This will compile.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Data;
    
    namespace ConsoleApp66
    {
        class Program
        {
            static void Main(string[] args)
            {
                string filePath = @"C:\Rexel\Test.txt";
    
                List<string> Amount = File.ReadAllLines(filePath).ToList();
                string[] row;
                foreach (string line in Amount)
                {
                    row = line.Split('\t');
                    if (Convert.ToDecimal(row[12]) > 0 && Convert.ToDecimal(row[12]) < 25000)
                    {
                        Console.WriteLine("Payment Amount: " + row[12]);
                        Console.WriteLine("Status: Ready to Send");
                    }
                    else if (Convert.ToDecimal(row[12]) >= 25000)
                    {
                        Console.WriteLine("Payment Amount: " + row[12]);
                        Console.WriteLine("Status: Needs Approval");
                    }
    
                    DataTable ConvertToDataTable(int numberOfColumns, string filePath1)
                    {
                        DataTable tbl = new DataTable();
    
                        for (int col = 0; col < numberOfColumns; col++)
                            tbl.Columns.Add(new DataColumn("Column" + (col + 1).ToString()));
    
                        string[] lines = System.IO.File.ReadAllLines(filePath1);
    
                        foreach (string line1 in lines)
                        {
                            var cols = line1.Split(':');
    
                            DataRow dr = tbl.NewRow();
                            for (int cIndex = 0; cIndex < 3; cIndex++)
                            {
                                dr[cIndex] = cols[cIndex];
                            }
    
                            tbl.Rows.Add(dr);
                        }
    
                        return tbl;
                    }
                }
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by DB161998 Tuesday, November 13, 2018 9:20 PM
    Tuesday, November 13, 2018 8:38 PM
    Moderator