Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DataSet to ArrayList?

Posted on 2004-08-19
4
Medium Priority
?
902 Views
Last Modified: 2012-08-14

Hello, new to .Net. I'm trying to present feedback to a user through data they will upload in an excel spreedsheet. I can get the file just fine onto a machine and display it through a datagrid. What I would really like to do is through it into memory, present it back to the user with different row styles depending on the information in the row (for errors) and also return an imagebutton with each row allow for the row to be editred in memory prior to ultimately being saved to the database.

Question is this: What is the best way to put the information from the excel sheet into memory, so that it can be spit back with dynamic style application and an additional WebControl?

I'm thinking I could use an ArrayList (becuase it's fast to update and flexible in size) but I don't know how to get all that into the arraylist and back into another DataList Control.

Here is what I have so far, thanks in advance!!

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;


namespace Com.Presentation
{
      /// <summary>
      /// Summary description for Upload.
      /// </summary>
      public class Upload : Com.Presentation.Components.BaseCode
      {
            protected System.Web.UI.WebControls.DataGrid DataGrid1;
            protected System.Web.UI.HtmlControls.HtmlInputFile File1;
            protected System.Web.UI.WebControls.Button cmdUpload;
            protected System.Web.UI.WebControls.Label lblMessage;
     
            string sFileDir = "C:\\";
            long lMaxFileSize = 100000;
     
            private void Page_Load(object sender, System.EventArgs e)
            {
            }
 
            private void DeleteFile(string sFileName)
            {
                  /* Delete file , if it exists and I've put it on the server */
                  if (sFileName.Trim().Length > 0)
                  {
                        FileInfo fi = new FileInfo(sFileName);
                        if (fi.Exists)
                        {    
                              fi.Delete();
                        }
                  }
            }
 
 
            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  InitializeComponent();
                  base.OnInit(e);
            }
             
            private void InitializeComponent()
            {  
                  this.cmdUpload.Click += new System.EventHandler(this.cmdUpload_Click);
                  this.Load += new System.EventHandler(this.Page_Load);
            }
            #endregion
 
 
            private void cmdUpload_Click(object sender, System.EventArgs e)
            {
          if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
                  {
                        /* get file name */
                        string sFileName = System.IO.Path.GetFileName(File1.PostedFile.FileName);
                        try
                        {
                              /* file can't be more than the declared size at begining of page */
                              if (File1.PostedFile.ContentLength <= lMaxFileSize)
                              {
                                    /* save it */
                                    File1.PostedFile.SaveAs(sFileDir + sFileName);
                                    lblMessage.Visible = true;
                                    lblMessage.Text= "got " + sFileDir + sFileName + "  loaded ok";

                                    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + File1.PostedFile.FileName + ";Extended Properties=Excel 8.0;";
                                    OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
                                    DataSet myDataSet = new DataSet();

                                    myCommand.TableMappings.Add("Table" , "ExcelInfo");
                                    myCommand.Fill(myDataSet);
                                    DataGrid1.DataSource = myDataSet.Tables[0].DefaultView;
                                    DataGrid1.DataBind();
                              }
                              else
                              {
                                    /* file too big, reject it */
                                    lblMessage.Visible = true;
                                    lblMessage.Text = "file size over " + lMaxFileSize;
                              }
                        }
                        catch(Exception)
                        {
                              lblMessage.Visible = true;
                              lblMessage.Text = "some kind of error";
                              DeleteFile(sFileDir + sFileName);
                        }
                  }
            }


      }
}


0
Comment
Question by:animated405
[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
4 Comments
 
LVL 3

Accepted Solution

by:
bigjim2000 earned 332 total points
ID: 11849841
Well, I'd make a struct that has members for each colum.

Then go through all the rows in your data set, and populate that struct, and add it to the arraylist.

This would be a MUCH more friendly solution compared to adding the data set rows themselves to the array....

-Eric
0
 
LVL 7

Assisted Solution

by:NipNFriar_Tuck
NipNFriar_Tuck earned 336 total points
ID: 11852242
I like using classes and strongly typed collections...

public class MyDataClass {
   private string field1;
   private int field2;
   // etc

   public string Field1 {
       get { return field1; }
       set { field1 = value; }
   }
   public string Field2 {
       get { return field2; }
       set { field2 = value; }
   }
   // etc

   // the rest of the class
}

public class MyDataCollection : CollectionBase {
   // see help on how to implement
}

The nice thing about using a strongly typed collection is that you can then assign the collection to the datasource for a datagrid.... i.e.

MyDataCollection myCollection = new MyDataCollection();
DataGrid1.DataSource = myDataCollection;

Lastly, the reasom that I like this approach is that if I have to write a function that is to work against the collection I can put that function in my collection class.  Some functions that I have put there are Load, Save, and Filter.  Basically, the load gets the data from the DB, creates the class and passes a record to that classes Load, and adds the new instance to itself.  The save loops through itself calling the save on each instance...

Hope this helps...
nipnfriar_tuck
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 332 total points
ID: 11852333
Realize, though, that Structures are value type variables, and ArrayLists expect reference types, so boxing/unboxing is required to add the structure to the ArrayList.  This is not a significant problem for small amounts of data, but can add considerable overhead if you have large number of records.

Boxing and Unboxing of Value Types in C#: What You Need to Know
http://www.c-sharpcorner.com/Code/2003/July/BoxNUnboxSSF.asp

My suggestion would be to use a Class definition instead of structure.

Bob
0
 

Author Comment

by:animated405
ID: 11855102

Thanks for the replies. I'm still kind of confused, but your comments are helping me look more in the right direction. I'm keeping this open as I'm sure I have more comments soon.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
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

704 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