Solved

Convert Datatable to Dataset

Posted on 2014-02-26
4
349 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:Seven price
[X]
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
  • 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:Seven price
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:Seven price
ID: 39890232
tks
0

Featured Post

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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