Date format differs on Local Machine as well as Production Machine or Error : String was not recognized as a valid DateTime

Technology: ASP.Net 4.5

DateTime format conversions are a nightmare for the developers as I see.

But to convert a datetime in .Net is super easy!. All you have to do is go to your Web.Config file and add the following Globalization tag into it, with the intended UI Culture.

UI Culture for UK

<system.web>
 <globalization 
 requestEncoding="utf-8" 
 responseEncoding="utf-8" 
 culture="en-GB" 
 uiCulture="en-GB" />
 </system.web>

UI Culture for US

<system.web>
 <globalization 
 requestEncoding="utf-8" 
 responseEncoding="utf-8" 
 culture="en-US" 
 uiCulture="en-US" />
 </system.web>

Cheers!

Advertisements

How to Upload a file into ASP.Net DataSet using FileUpload control

Uploading a file into ASP.net is super easy!

In the below example, I am showing you how to upload an Excel Spreadsheet into ASP.Net dataset.

I have been using the ExcelDataReader control, which can be referenced using NuGet in Visual Studio 2013.

Issues While Uploading Files from non-IE browsers

There has been reports that, the non IE browsers, do not have a fully, qualified path of the File, when it is uploaded into Asp.Net.

As a workaround, we will have to copy the file into a temporary server map path. I have updated the code below in VB.Net and C#.Net

Finding the binaries

It is recommended to use Nuget. This is very important!

Install-Package ExcelDataReader

The current binaries are still on the codeplex site, but these will not be updated going forward. If there are enough requests for separate binary hosting other than nuget then we’ll come up with some other solution.

How to use

Client Markup :

<asp:Button Text=”Click” CssClass=”form-control” ID=”Button1″ runat=”server” OnClick=”Button1_Click”>

The below code is from the Button1_Click event handler

C# code :

// 1. Workaround, for uploading the Excel Spreadsheet from a non-IE browser. 
// It should work for IE also!!

string sServerMapPath = "";
 string sConcatenatedPath = "";

 sServerMapPath = Server.MapPath("~/");

// 2. Saving the file into Server Path
 FileUpload_Excel.SaveAs(sServerMapPath + FileUpload_Excel.FileName);

 sConcatenatedPath = sServerMapPath + FileUpload_Excel.FileName;

 FileStream stream = File.Open(sConcatenatedPath, FileMode.Open, FileAccess.Read);

 IExcelDataReader excelReader = default(IExcelDataReader);

//3. Reading from a binary Excel file ('97-2003 format; *.xls)
 if (FileUpload_Excel.FileName.EndsWith("*.xls")) {
 excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
 } else {
 excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
 }

//4. DataSet - The result of each spreadsheet will be created in the result.Tables
 DataSet result = excelReader.AsDataSet();

//5. DataSet - Create column names from first row
 excelReader.IsFirstRowAsColumnNames = true;

//Dim result As DataSet = excelReader.AsDataSet()

//6. Data Reader methods

 while (excelReader.Read()) {
//excelReader.GetInt32(0);
 }

//7: .Free Resources(IExcelDataReader Is IDisposable)
 excelReader.Close();

VB.NET Code:

 ' 1. Workaround, for uploading the Excel Spreadsheet from a non-IE browser. 
 ' It should work for IE also!!

 Dim sServerMapPath As String
 Dim sConcatenatedPath As String

 sServerMapPath = Server.MapPath("~/")

 ' 2. Saving the file into Server Path
 FileUpload_Excel.SaveAs(sServerMapPath + FileUpload_Excel.FileName)

 sConcatenatedPath = sServerMapPath + FileUpload_Excel.FileName

 Dim stream As FileStream = File.Open(sConcatenatedPath, FileMode.Open, FileAccess.Read)

 Dim excelReader As IExcelDataReader

 '3. Reading from a binary Excel file ('97-2003 format; *.xls)
 If FileUpload_Excel.FileName.EndsWith("*.xls") Then
 excelReader = ExcelReaderFactory.CreateBinaryReader(stream)
 Else
 excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream)
 End If

 '4. DataSet - The result of each spreadsheet will be created in the result.Tables
 Dim result As DataSet = excelReader.AsDataSet()

 '5. DataSet - Create column names from first row
 excelReader.IsFirstRowAsColumnNames = True

 'Dim result As DataSet = excelReader.AsDataSet()

 '6. Data Reader methods

 While excelReader.Read()
 'excelReader.GetInt32(0);
 End While

 '7: .Free Resources(IExcelDataReader Is IDisposable)
 excelReader.Close()