0

I have created a web form in asp.NET, unfortunately i do not have the pleasure of being able to use SQL due to some restrictions (internal). I had the idea of exporting the data to a CSV file every time the user clicks submit.

The purpose of this is so that we have a list of computer names, softwares installed, Serial #, ect to keep track.

I have never done anything like this, as i am used to SQL (beginner level), is this even possible, and how would i do it (in code form). I've googled it and all i'm getting is grid view and other forms that i don't want to use, even those seem more complicated than what i want to do. Any help would be appreciated.

I'm using a C# back end to code this.

To clarify, this will be a single CSV file on our network, this web form will only be used internally. So every time someone clicks the submit button it will add a new "row" to the same CSV file. So in the end we have one CSV file with a list of computer names and other information.

user1574685
  • 181
  • 1
  • 2
  • 11
  • Possible duplicate of http://stackoverflow.com/questions/13244875/generate-csv-file-in-asp-net – Johnv2020 Aug 27 '15 at 16:19
  • Close, but that looks like they want to create a new CSV file every time the submit button is clicked. – user1574685 Aug 27 '15 at 16:24
  • 1
    You could use a CSV file, but structured data is easier to process if you store it in XML or JSON instead. There's excellent built in libraries for both of those. – mason Aug 27 '15 at 16:36
  • I would structure it into XML data. Save it as XML within your file. Excel is based on XML and will read it like CSV. – wolfeh Aug 27 '15 at 16:41
  • You don't have to do all of the writing yourself. Read about how to serialize/deserialize your objects. Here is an example on how to do so with XML: https://support.microsoft.com/en-us/kb/316730 – Hanlet Escaño Aug 27 '15 at 17:13

1 Answers1

1

Typically you have a model class that represents the data. Here's one:

public class Computer
{
    public string SerialNumber {get; set;}
    public string Name {get; set;}
    public List<string> InstalledSoftware {get; set;}
}

Now that you have a class that can represent the data, it's just a matter of saving or serializing it. You don't have access to a SQL Server database. That's fine. There's other options. You can store it in a structured file format. CSV is not good for this, as you might have multiple pieces of InstalledSoftware per computer, and it's hard to properly handle that with CSV. But other text based formats such as XML and JSON are perfect for this. You can also use "NoSQL" type databases such as MongoDB or RavenDB. You may also be able to use SQLite, which is very lightweight.

Let's start off with some sample data.

List<Computer> computers = new List<Computer>();
computers.Add(new Computer(){
    SerialNumber="ABC123",
    Name="BOB-LAPTOP",
    InstalledSoftware = new List<string>(){
        "Notepad", "Visual Studio", "Word"
    }
});
computers.Add(new Computer(){
    SerialNumber="XYZ456",
    Name="JASON-WORKSTATION",
    InstalledSoftware = new List<string>(){
        "Notepad++", "Visual Studio Code", "Word"
    }
});
computers.Add(new Computer(){
    SerialNumber="LMN789",
    Name="NANCY-SURFACE3",
    InstalledSoftware = new List<string>(){
        "Outlook", "PowerPoint", "Excel"
    }
});

Then it's just a matter of saving the data. Let's try with XML:

var xmlSerializer = new XmlSerializer(typeof(Computer));       
using(var stringWriter = new StringWriter())
{
    using (var xmlWriter = XmlWriter.Create(stringWriter))
    {
        xmlSerializer .Serialize(writer, computers);
        var xml = stringWriter.ToString();
        File.WriteAllText(Server.MapPath("~/App_Data/computers.xml"));
    }
}

Or with JSON:

var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(computers);
File.WriteAllText(Server.MapPath("~/App_Data/computers.json"));

Using MongoDB:

var client = new MongoClient(connectionString);  
var server = client.GetServer();  
var database = server.GetDatabase("ComputerDB");  
var computerCollection= database.GetCollection<Computer>("Computers");
foreach(var computer in computers)
{  
    computerCollection.Insert(computer);
} 

Note, I have not tested this code. There's likely bugs. My goal is to give you a starting point, not necessarily 100% working code. I haven't serialized XML in a while and I typically use JSON.NET instead of the built in JavaScriptSerializer.

Also note that if there's any possibility that two users might access the data at the same time, you'll need to take care with the XML and JSON approaches to avoid two people trying to write to the file at once. That's where MongoDB would be better, but you'll have to install the server software somewhere.

mason
  • 31,774
  • 10
  • 77
  • 121