Tuesday, March 15, 2011

CSV File BULK Upload using ASP.NET & SQL

if (CSVFileUpd.HasFile)
{
       string filePath = CSVFileUpd.PostedFile.FileName;
       string filenameStor = CSVFileUpd.FileName;
       string filenameType = System.IO.Path.GetExtension(CSVFileUpd.PostedFile.FileName);
       if (filenameType.ToString().Trim().ToLower() == ".csv")
       {
                System.IO.Stream str = default(System.IO.Stream);
                str = CSVFileUpd.PostedFile.InputStream;
                System.IO.StreamReader sr = new System.IO.StreamReader(str);
                string[] tokens = null;
                 string line = null;
                 // string token = null;
                 //Response.Write("<table>");
                 System.Data.DataTable dt = new System.Data.DataTable("CSVFileData");
                // Define 3 columns
                 System.Data.DataColumn dc;
                 dc = new System.Data.DataColumn("Id");
                dt.Columns.Add(dc);
                dc = new System.Data.DataColumn("Name");
                dt.Columns.Add(dc);
                dc = new System.Data.DataColumn("Prefix");
                dt.Columns.Add(dc);
                do
               {
                  line = sr.ReadLine();
                  if (line == null)
                   break; // TODO: might not be correct. Was : Exit Do
                   if (line == "")
                   {
                      lblMessage.Text = "File content is empty!";
                        break;
                   }
                  //Response.Write("<tr>");
                 tokens = line.Split(',');
                 if (line.Split(',').Length != 3)
                {
                      lblMessage.Text = "Invalid file! File should have 3 columns.";
                      break;
                 }
                if (tokens[0].Length > 0 && tokens[1].Length > 0)
               {
                     dt.Rows.Add(tokens[0], tokens[1], tokens[2]);
                }
              } while (!(line == null));
             // Response.Write("</table>");
            sr.Close();
            lblMessage.Text = CSVFileUpload(dt);
      }
      else
     {
         lblMessage.Text = "Please select .CSV/.csv  file!";
    }

protected string CSVFileUpload(System.Data.DataTable dtCSV)
{
using (System.Data.SqlClient.SqlBulkCopy bulk = new System.Data.SqlClient.SqlBulkCopy("Data Source=LOCALHOST\MSSQLSERVER;Initial Catalog=Reporting_Testing;Integrated Security=True"))
{
try
{
bulk.DestinationTableName = "MyDetails";
bulk.WriteToServer(dtCSV)
return "CSV File uploaded successfully!";
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
}

Tuesday, March 08, 2011

Error: “Sys” is Undefined Error

This is an javascript error will display in the browser because of some references missing in web.config file of your application and specially when you host your application to the server.
To resolve this please follow the below steps
1. Open the web.config file
2.Copy the below code to inside of


Http handlers are the .NET Components that implement the System.Web.IHttpHandler.
They are similar to ISAPI extensions,but one difference is the Http handles can be called directly by using their file name in the URL.
3.Copy the below code inside of


HTTP modules are .NET components that implements the System.Web.IHttpModule interface.
They are plug into the ASP.NET request processing pipeline by registering themselves for certain events