title: Import Data from .CSV TO SQL using FileUpload?
body: <p>I have an .CSV file which contain reviews and I am to programatically transfer the contents over into SQL Database from an FileUpload control. The problem I have is that my control are working but I dont see any data being populated in my database. Here is my code:</p>
  
  <p>MarkUp</p>
  
  <pre><code>&lt;asp:FileUpload ID="FileUpload1" runat="server" /&gt;
  &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;&lt;/asp:ScriptManager&gt;
  &lt;asp:Button ID="Button1" runat="server"onclick="Button1_Click"                                                               Text="Button" /&gt;
  &lt;asp:Label ID="Label1" runat="server"&gt;&lt;/asp:Label&gt;
  
  
  
  Code Behind
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.OleDb;
  using System.Data.SqlClient;
  using System.Data.SqlTypes;
  
  public partial class _Default : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
      protected void Button1_Click(object sender, EventArgs e)
      {
          if (FileUpload1.HasFile)
          {
  
              string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
  
              if (fileExt == ".csv")
              {
                  OleDbConnection oconn = new     OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + FileUpload1 + ";     Extended Properties=Excel 8.0");
  
                  try
                  {
                      OleDbCommand ocmd = new OleDbCommand("SELECT * FROM [AudioPR$]",     oconn);
                      oconn.Open();
  
                      OleDbDataReader odr = ocmd.ExecuteReader();
                      string Device = "";
                      string Source = "";
                      string Reviewer = "";
                      string Datetime = "";
                      string Links = "";
                      string Content = "";
                      string Subject = "";
  
                      while (odr.Read())
                      {
  
                          Device = valid(odr, 0);
                          Source = valid(odr, 1);
                          Reviewer = valid(odr, 2);
                          Datetime = valid(odr, 3);
                          Links = valid(odr, 4);
                          Content = valid(odr, 5);
                          Subject = valid(odr, 6);
  
                          InsertDataIntoSql(Device, Source, Reviewer, Datetime, Links,     Content, Subject);
  
                      }
  
                      oconn.Close();
  
                   }
  
                   catch (Exception ee)
                  {
                      Label1.Text = ee.Message;
                      Label1.ForeColor = System.Drawing.Color.Red;
                  }
                  finally
                  {
                      Label1.Text = "Data Inserted Successfully";
                      Label1.ForeColor = System.Drawing.Color.Green;
  
                  }
  
              }
  
              else
              {
                  Label1.Text = "Only .csv files allowed!";
  
              }
  
          }
  
           else
              {
  
              Label1.Text = "You have not specified a file!";
  
              }
  
         }
  
  
  
      protected string valid(OleDbDataReader myreader, int stval)
  
      {
          object val = myreader[stval];
          if (val != DBNull.Value)
          {
  
              return val.ToString();
          }
          else
          {
              return Convert.ToString(0);
          }
  
  
      }
  
      public void InsertDataIntoSql(string Device, string Source, string Reviewer, string     Datetime, string Links, string Content, string Subject)
  
      {
          SqlConnection conn = new SqlConnection("Data Source=CI0000000879107\\BENSON;    Initial Catalog=PRClips Mail;Integrated Security=True");
          SqlCommand cmd = new SqlCommand();
          cmd.CommandText = "INSERT INTO AudioPR(Device, Source, Reviewer, Datetime,     Links, Content, Subject) VALUES(@Device, @Source, @Reviewer, @Datetime, @Links, @Content,     @Subject)";
          cmd.Parameters.Add("@Device", System.Data.SqlDbType.NVarChar).Value = Device;
          cmd.Parameters.Add("@Source", System.Data.SqlDbType.NVarChar).Value = Source;
          cmd.Parameters.Add("@Reviewer", System.Data.SqlDbType.NVarChar).Value =     Reviewer;
          cmd.Parameters.Add("@Datetime", System.Data.SqlDbType.Date).Value = Datetime;
          cmd.Parameters.Add("@Links", System.Data.SqlDbType.NVarChar).Value = Links;
          cmd.Parameters.Add("@Content", System.Data.SqlDbType.NVarChar).Value = Content;
          cmd.Parameters.Add("@Subject", System.Data.SqlDbType.NVarChar).Value = Subject;
          cmd.CommandType = System.Data.CommandType.Text;
          conn.Open();
          cmd.ExecuteNonQuery();
          conn.Close();
  
     }
  }
  </code></pre>
  
  <p>I realize that having an connection is kinda pointless when I could just reading the contents from the FileUpload but if anyone has an idea of what I am doing wrong or could guide to a proper direction that would be greatly appreciated. Thanks for your time</p>