Remove Roslyn support from ASP.NET Project

Friends,

VS2015 has been launched and if you’re using .net Framework 4.5.2 to create projects using VS2015, you might have noticed that the project templates use Roslyn by default. In this post we will see How To Remove Roslyn support from ASP.NET Project.

If you’re not aware of Roslyn, it is a set of open-source compilers and code analysis APIs for C# and Visual Basic .NET languages from Microsoft. You can see the code here.

Many hosting providers still have not upgraded their servers and hence do not support Roslyn. this is quite common in shared hosting servers. If you upload a project with Roslyn references, you will get unwanted errors on the website. To resolve this issue, we will need to remove Roslyn from the project template. We will see how this can be done in this post.

It’s a 2 step-process to remove Roslyn from your project –

  1. Remove Nuget Packages – “Microsoft.CodeDom.Providers.DotNetCompilerPlatform” & “Microsoft.Net.Compilers
  2. Cleanup web.config file.

To remove Nuget packages, use the following commands from Nuget Package Console –

uninstall-package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
uninstall-package Microsoft.Net.Compilers

After you do this, your web.config file should be auto-updated. In case it is not, look for the below code in web.config file and if it is found, delete this piece of code.


Build your project and upload to the server. The errors related to Roslyn should be gone 🙂

Hope this post helps you! Cheers! 🙂

How To Create a TextBox Accepting Only Numbers in Windows Forms

Friends,

Many times we need to have a textbox on our windows forms where we need to accept only numbers from the end users. .Net does provide us a NumericUpDown control but it is not always handy. In this post, we will see how we can create a TextBox accepting only numbers in Windows Forms for entries.

To enable any TextBox number only, handle the “KeyPress” event handler of the TextBox and write the below code in the handler. Considering the ID of TextBox control is txtNumeric, the code will be like as below –

        private void txtNumeric_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar); //&& !char.IsControl(e.KeyChar);
        }

In VB.Net, the same code can be written as below –

        Private Sub txtNumeric_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles txtNumeric.KeyPress
            e.Handled = Not char.IsDigit(e.KeyChar)
        End Sub

If you see the code above, we have checked if the entered character is a number or not and have manually marked the Handled property of the event handler to true.

Hope you like this post. Cheers!

Solution: ‘Microsoft.Jet.OLEDB.4.0’ provider is not registered on the local machine

Friends,

While working with Access, many times you may get the famous exception “Microsoft.Jet.OLEDB.4.0′ provider is not registered on the local machine“. This normally happens that your code runs fine on your development machine and when you try the run it on production server, this error comes. IN this post, we will see a solution to the same.

This happens because there is no 64 bit version of Jet. To resolve this, you need to do the following steps –

  • Select the Project in Solution Explorer and right-click on it to open its Properties
  • Select Build Tab from the Properties window, choose x86 as “Platform Target” on the screenjet-error
  • Save the project & recompile.

Hope this helps you! Keep learning & sharing! Cheers

Change Height of TextBox Control in Windows Forms

Friends,

From the title, this seems to be a very simple post that every novice programmer would know but changing the height of a textbox control in a Windows Forms Application is bit tricky that thought. You must be wondering that setting the Size.Height property of the control would do this, but as soon as you change this, it reverts back to the original default size. If not, go give a try and let me know if it works instantly. In this post, we will see ways on how to achieve this. There are few ways you can increase the height of the textbox. They are –

Option 1: Use MultiLine Property

You can set the MultiLine property of the TextBox control to True and then increase the height of the TextBox. This may not be desirable in some cases when you want to restrict users to enter only single line in the textbox.

textbox-2

Option 2: Change Designer.cs file

Open the Designer.cs file and scroll down to the place where your textbox properties are set. Add the following lines to set the height of the TextBox. However, if you are using this approach, beware that you changes get lost as soon as you make some change in the Form designer and hence this approach is highly recommended to be NOT used.

Option 3: Use MinimumSize Property

This is the most robust way I have found so far. To use this approach, you need to make 2 changes in your Textbox properties.

  1. Set MinimumSize Property of the TextBox to your desired size(Height & Width)
  2. Clear the Size Property of the TextBox to blank and hit Enter.

textbox

As you do this, your TextBox will resize to your desired Height & Width.

Hope you like this post! Keep learning and sharing ! Cheers!

Export Data From GridView to Excel 2007/2010 in ASP.Net

Friends,

In this post, we will see how can we export data from GridView to Excel 2007/2010 format in ASP.Net in less than 5 minutes. Without much delay, let’s get started –

  • Add EPPlus package using Nuget.export-gridview-excel-1
  • Include OfficeOpenXml and OfficeOpenXml.Table namespaces at the top.
  • In your event handler, write the below code.
  • You’re done.

VB.Net Code:

       Response.Clear()
       Response.Charset = ""
       Response.ContentEncoding = System.Text.Encoding.UTF8
       Response.Cache.SetCacheability(HttpCacheability.NoCache)
       Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
       Response.AddHeader("content-disposition", "attachment;filename=GridData.xlsx")

       Dim dt As DataTable = CType(gridView.DataSource, DataTable)
       Using pck As New ExcelPackage()

          Dim wsDt As ExcelWorksheet = pck.Workbook.Worksheets.Add("Sheet1")
          wsDt.Cells("A1").LoadFromDataTable(dt, True, TableStyles.None)
          wsDt.Cells(wsDt.Dimension.Address).AutoFitColumns()

          Response.BinaryWrite(pck.GetAsByteArray())
       End Using

       Response.Flush()
       Response.End()

C# Code:

       Response.Clear();
       Response.Charset = "";
       Response.ContentEncoding = System.Text.Encoding.UTF8;
       Response.Cache.SetCacheability(HttpCacheability.NoCache);
       Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
       Response.AddHeader("content-disposition", "attachment;filename=GridData.xlsx");

       DataTable dt= gridView.DataSource as DataTable;
       using(ExcelPackage pck As New ExcelPackage())
       {
         ExcelWorksheet wsDt = pck.Workbook.Worksheets.Add("Sheet1");
         wsDt.Cells("A1").LoadFromDataTable(dt, True, TableStyles.None);
         wsDt.Cells(wsDt.Dimension.Address).AutoFitColumns();

         Response.BinaryWrite(pck.GetAsByteArray());
       }

       Response.Flush();
       Response.End();

Explanation:

In the above code, what we are doing is first of all, clearing the Response and setting the ContentType and Header for the Response object. This is to be noted that “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet” is the type to be set for Excel 2007. Once these are set, we are getting the Data Source of the GridView in a DataTable. Then, we use an object of ExcelPackage class to create a worksheet and load the DataTable into the worksheet from Cell A1. Calling the AutoFitColumns()  ensures that the Excel file written is auto set to fit the column contents. Once the data is written and the excel file is created in memory, we write the same back to the browser. This allows users to either open the Excel file or save it their hard disks.

Hope you like this post! Keep learning & sharing! Cheers!