?
Solved

Convert Datatable to Dataset

Posted on 2014-02-26
4
Medium Priority
?
352 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 1000 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 1000 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

764 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