Sai Stuff to Developers

September 3, 2014

Problem with ASP.NET using MySQL Connecting through ODBC Driver in some Hosting Servers

Filed under: DotNet — tosaik @ 9:48 am
Tags: , , , , , , ,

Recently i hosted one of my asp.net 4.0 application which is using MySQL as backend and i decided to use MySQL .NET connector to access the MySQL server. I hosted one of the local server and its works fine but i got some consistency issues with my local server and decided to shift my application to GoDaddy which they ensure our application will be 24 X 7 in active.

I moved the application successfully but when i try to run the application through browser i cannot able to connect to database when i dig in to the issue through my logs i came to know that GoDaddy doesn’t support MySQL .NET connector DLL because GoDaddy will run only components and application which are Fully Trusted. When i read the blogs regarding this DLL i came to know Oracle has built this component as partial trusted (medium trust) which means i cannot use anymore with my application to host in GoDaddy. But here i have 2 options

  1. Need to place my MySql .Net connector component to the GAC of GoDaddy server.
  2. Need to remove using MySql .Net connector component in my application.

In the above options, option-1 GoDaddy doesn’t allow me to do so. Now i have only option-2, i removed code related to MySql .Net connector component and removed the reference to it and i try to connect mySQL database through ODBC (Object Database Connector). Which i successfully implemented in my application and working fine on my local computer and i updated the same over the hosting server (in my case GoDaddy), Interestingly still i am unable to connect to the Database, Now when i log my application to find the root cause of the issue, following are the error its throwing:

Request for the permission of type ‘System.Data.Odbc.OdbcPermission, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.

OOPS, i realized that GoDaddy also need my application to be fully trusted which i need to configure in my application via. web.config in System.web section as shown below:

 

<trust level="Full"/>

Once i changed this and made my application full trusted with this simple configuration change, My Application working like a charm.

The reason behind on writing this post is, i worked nearly 5 hrs to fix this issue and i cannot find any direct help from any blogs so i decided to keep this information in my blog to make others time save.

Thank You,

Happy Coding…..

 

August 23, 2012

Solution for Parsing an Excel file in C# and the cells seem to get cut off at 255 characters

Filed under: DotNet — tosaik @ 8:10 am
Tags: , , , , ,

Problem: An excel sheet containing say 10 rows and in couple of columns in couple of rows having data with more than 255 characters. When I try to upload this file in ASP.Net application using traditional OLEDB provider I cannot saw the whole data in particular cell means which is chopping out or we can say trimming to 255 characters when loaded in to my dataset object.

Solution: Having done sleepless nights I found some solutions to fix this issue… those are presented below

Solution-1:

If your application is used only on one computer then you can directly go to the following registry settings and change the TypeGuessRows  value.

HKLM\Software\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows

64 bit systems:

HKLM\SOFTWARE\wow6432node\microsoft\jet\4.0\engines\excel\TypeGuessRows

By setting this value to zero, the all lines of your spreadsheet are scanned for type guessing, rather than the default of 8. If any text fields longer than 255 chars are encountered, then those columns are deemed to be memo fields.

Note that you are still not 100% guaranteed to get the right data types, depending on your data.

Note also the HKLM scope of this key though – it will affect every OleDB Excel import by any process on that machine and this lead to degrade performance depending on the size of the data.

Solution-2:

A second way to work around this problem (without modifying the registry) is to make sure that rows with fields, which have data 255 characters or greater, are present in the first 8 rows (default value of TypeGuessRows is 8) of the source data file.

Solution-3:

This is the recommended solution by me as there is no need to change any registry or take care to have those lengthy data to be in first 8 rows. Instead we have a tool called NPOI which can be download from npoi.codeplex.com.

Using this dll we can upload the spreadsheet without worrying of chopping your data and also it has many features like creating the spreadsheet on fly including charts, reports etc.., for more information you can find on this site npoi.codeplex.com.

Anyway reading data using this NPOI is different from the traditional OLEDB provider. Please find the following method which with return a Data Table object by sending the File Path and the respective SheetName as input.

 
public static DataTable getExcelData(string FileName, string strSheetName)
    {
        DataTable dt = new DataTable();
        HSSFWorkbook hssfworkbook;
        using (FileStream file = new FileStream(FileName, FileMode.Open, FileAccess.Read))
        {
            hssfworkbook = new HSSFWorkbook(file);
        }

        ISheet sheet = hssfworkbook.GetSheet(strSheetName);
        System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
        
        while (rows.MoveNext())
        {
            IRow row = (HSSFRow)rows.Current;

            if (dt.Columns.Count == 0)
            {
                for (int j = 0; j < row.LastCellNum; j++)
                {
                    dt.Columns.Add(row.GetCell(j).ToString());
                }

                continue;
            }

            DataRow dr = dt.NewRow();
            for (int i = 0; i < row.LastCellNum; i++)
            {
                ICell cell = row.GetCell(i);

                if (cell == null)
                {
                    dr[i] = null;
                }
                else
                {
                    dr[i] = cell.ToString();
                }
            }
            dt.Rows.Add(dr);
        }

        return dt;
    }

 May be I presented the solutions straight forward without more explanation or discussion but presently my motto is to provide you the reasonable and permanent solution for those who suffering with similar problem.

Hope this research and the code helps you a lot.. if so please drop a comment below which may be more encroached for me..

 

Happy Coding  🙂

Create a free website or blog at WordPress.com.

%d bloggers like this: