<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Import Users to MDID</title>
</head>
<body>
    <h1>Import Script</h1>
    <p>This page will import data from Beth's Carddata sources into MDID's User tables, creating new accounts and groups as required.</p>

<script runat="server" type="text/C#">
  private void Page_Load(object sender, System.EventArgs e)
  {
        String output = "";
        
        System.Net.WebRequest req = System.Net.WebRequest.Create("https://register.oucs.ox.ac.uk/upload/card/mdid/mdid.txt");
        System.Net.WebResponse resp = req.GetResponse();
        using (System.IO.Stream tsv = resp.GetResponseStream())
        {
            Response.Write(tsv.WriteLine());
        }
  }
 
 
  // DataSet ds = TextToDataSet.Convert("c:\test.txt", "MyNewTable", "\t");
</script>
 
</body>
</html>




/* 
System Aims:
- Read a file via http (hard coded address initially, add a user input later?)
- Parse the file and display on screen - stage 1
- Parse the file and write to user record - stage 2
- Parse the file and write to user and groups - stage 3
- Allow data to be selected for input - stage 4

Personal Aims:
- stage 0.1 - complile a simple valid page with code in!

*/

 
 



<script runat=server type="text/C#">
/* @@ 
using System;
using System.Data;
using System.IO;

namespace TestTextToDataSet
{
  public class TextToDataSet
  { 
    public TextToDataSet()
    {  }
  
  
    // This be test method number one for playing with the web request method. Aim to display the file on screen having been cut up
    public static String Display1()
    {
        String output = "";
        
        WebRequest req = WebRequest.Create("https://register.oucs.ox.ac.uk/upload/card/mdid/mdid.txt");
        WebResponse resp = req.GetResponse();
        using (Stream tsv = resp.GetResponseStream())
        {

        }
        
        return output;    
    }
  
  
  
  
  
  
   /// <summary>
    /// Converts a given delimited file into a dataset. 
    /// Assumes that the first line    
  /// of the text file contains the column names.
  /// </summary>
    /// <param name="File">The name of the file to open</param>    
  /// <param name="TableName">The name of the 
  /// Table to be made within the DataSet returned</param>
    /// <param name="delimiter">The string to delimit by</param>
   /// <returns></returns>  
  public static DataSet Convert(string File, string TableName, string delimiter)
    {   
    //The DataSet to Return
    DataSet result = new DataSet();
    
    //Open the file in a stream reader.
      StreamReader s = new StreamReader(File);
        
    //Split the first line into the columns       
     string[] columns = s.ReadLine().Split(delimiter.ToCharArray());
  
    //Add the new DataTable to the RecordSet
      result.Tables.Add(TableName);
    
    //Cycle the colums, adding those that don't exist yet 
    //and sequencing the one that do.
      foreach(string col in columns)
      {
        bool added = false;
        string next = "";
      int i = 0;
      while(!added)        
      {
        //Build the column name and remove any unwanted characters.
        string columnname = col + next;
         columnname = columnname.Replace("#","");
          columnname = columnname.Replace("'","");
          columnname = columnname.Replace("&","");
        
        //See if the column already exists
        if(!result.Tables[TableName].Columns.Contains(columnname))
          {
          //if it doesn't then we add it here and mark it as added
            result.Tables[TableName].Columns.Add(columnname);
            added = true;
          }
          else
        {
          //if it did exist then we increment the sequencer and try again.
            i++;  
          next = "_" + i.ToString();
          }         
      }
    }
    
    //Read the rest of the data in the file.        
     string AllData = s.ReadToEnd();
    
    //Split off each row at the Carriage Return/Line Feed
    //Default line ending in most windows exports.  
    //You may have to edit this to match your particular file.
    //This will work for Excel, Access, etc. default exports.
    string[] rows = AllData.Split("\r\n".ToCharArray());
 
      //Now add each row to the DataSet        
    foreach(string r in rows)
      {
      //Split the row at the delimiter.
        string[] items = r.Split(delimiter.ToCharArray());
      
      //Add the item
       result.Tables[TableName].Rows.Add(items);  
      }
    
    //Return the imported data.        
    return result;
    }
  }
 }
 
 
 */
 </script>
