Solved

Convert Datatable to Dataset

Posted on 2014-02-26
4
336 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now