Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Convert Datatable to Dataset

Posted on 2014-02-26
4
341 Views
Last Modified: 2014-02-26
How can I read from my datatable make it into a dataset.
private object ReadToEnd(string filePath)
        {
            
                string path = Server.MapPath("~/UploadFiles/" + ["fileName"] + ".CSV");
                DataTable dtDataSource = new DataTable();
                string[] fileContent = File.ReadAllLines(path);
                if (fileContent.Count() > 0)
                {
                    //Create data table columns
                    string[] columns = fileContent[0].Split(',');
                    for (int i = 0; i < columns.Count(); i++)
                    {
                        dtDataSource.Columns.Add(columns[i]);
                    }
                    //Add row data
                    for (int i = 1; i < fileContent.Count(); i++)
                    {
                        string[] rowData = fileContent[i].Split(',');
                        dtDataSource.Rows.Add(rowData);
                    }
                }
                return dtDataSource;
            
        }

Open in new window



 gv_GridView.DataSource = (DataTable)ReadToEnd(Request.QueryString["fileName"] + ".CSV");
                gv_GridView.DataBind();
                lbl_ErrorMsg.Visible = false;

Open in new window

0
Comment
Question by:sevensnake77
  • 2
4 Comments
 
LVL 25

Assisted Solution

by:SStory
SStory earned 250 total points
ID: 39889424
http://msdn.microsoft.com/en-us/library/ss7fbaez%28v=vs.110%29.aspx

DataSets contain DataTables
You use a DataAdapter to do it. Read the link above for more info.
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 39889531
ok but the reason is I want to take what I have and send it to a database.

  private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
        {
            DataTable csvData = new DataTable();
            try
            {
              using(TextFieldParser csvReader = new TextFieldParser(csv_file_path))
                 {
                    csvReader.SetDelimiters(new string[] { "," });
                    csvReader.HasFieldsEnclosedInQuotes = true;
                    string[] colFields = csvReader.ReadFields();
                    foreach (string column in colFields)
                    {
                        DataColumn datecolumn = new DataColumn(column);
                        datecolumn.AllowDBNull = true;
                        csvData.Columns.Add(datecolumn);
                    }
                    while (!csvReader.EndOfData)
                    {
                        string[] fieldData = csvReader.ReadFields();
                        //Making empty value as null
                        for (int i = 0; i < fieldData.Length; i++)
                        {
                            if (fieldData[i] == "")
                            {
                                fieldData[i] = null;
                            }
                        }
                        csvData.Rows.Add(fieldData);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return csvData;
        }
      }

Open in new window

0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 39889966
Use the same method on the DataTable that you use on a DataSet.

If you update your DataSet with the Update method, also use the Update method on the DataTable.

A DataSet is an object used to create relation between tables. If you do not have relations, you do not need a DataSet.
0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 39890232
tks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question