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…..

 

October 1, 2013

Launched New Technology Site developersfusion.com

Hi Friends,

I have recently launched a new Technology WebSite www.developersfusion.com .

 

About Developers Fusion

Developers Fusion is a place where the professionals can share their experience and views contributing knowledge to the developer’s community. A successful professional, learns, explores, matures and finally contributes.

The developersfusion.com works with an idea, that has a big vision of bringing Technology to the cozy of your room wherever you are, and make learning comprehensive and exploring Technology real simple and easy.

We are dedicated in providing our Readers quality implementation, education, documentation and solutions. Our ultimate goal is to provide our readers with appropriate solution to their technical questions and needs. The developersfusion.com personifies the passion for Software Technology, delivers power in the technology and the skills associated with it, and enhances the productivity of professionals who shape the software field.

The developersfusion.com believes that it is vital to make learning Technology easy and effective and help its readers, technical breakthroughs in their future career along with other sessions that make them more informed about things that happen around them.

The Articles section lists articles published from different professionals enabling our readers to learn what’s new in the market and also the experience of the author. The FAQs section enables readers to prepare for answering the interview questions shot at them.

Our Vision

Bringing Technology to the cozy of every developer room wherever you are, and make learning comprehensive and exploring Technology real simple and easy.

Our Goal

Our ultimate goal is to provide our readers with appropriate solution to their technical questions and needs.

 

Hope all you make my new website a grand success by visiting posting and participating in forums etc…,

 

Regards,

Sai Kumar K

April 23, 2013

Finding count of Prime Numbers contain for a given Number

Filed under: DotNet,OOPS Languages — tosaik @ 11:44 am
Tags: , , , ,

Recently I need to add some of this kind of functionality in my on-going project and for that i wrote some simple method to returning the count of prime Numbers contain for given input number…. so i just want to have this snippet in front of you… Hope it will help as.. as it is simple but while implementing it kills our brain surly 🙂


static int getNumberOfPrimes(int N) {
	
    int count = 0;
    bool res = false;
    for(int i=2;i<=N;i++)
    {
        res = false;
        for(int j=2;j<=i;j++)
        {
            if(i%j == 0 && i!=j)
            {
                res = true;
            }
        }
        if(!res)
            count++;
    }
    
    return count;

}

Hope it will help for you 🙂

December 4, 2012

Unobtrusive Validation in Framework 4.5 Data Validations

Filed under: DotNet — tosaik @ 7:41 am
Tags: , , , , ,

Today I was working on ASP.NET web application on framework 4.5; I came across a strange error which I never experienced in my past working on earlier environments of dot net framework and that is error titled

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for ‘jquery’. Please add a ScriptResourceMapping named jquery(case-sensitive).

When I dig in to this issue and found the root cause of this issue and here my case study about this issue…

Unobtrusive Validation is a new concept in Framework 4.5, and this is by default enables when we create a new ASP.NET web application by adding one of the key in a Web.Config->appsettings section as below

 <add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />

What exactly this validation does for us

Forms with complex data including validators tend to generate too much JavaScript code in the page, which can represent about 60% of the code. With unobtrusive validation enabled, your HTML code will look cleaner and tidier.

For example we will compare the HTML code generated by both configurations.

Case-1:

Steps to create the sample application without unobtrusive validation enabled (default for all earlier versions)

1)      Open Visual studio 2012 IDE and create a new Web Application as shown below

UV1

 

 

 

 

 

 

 

 

2)      Now Press F5 to start the web application.

3)      Right-click on the browser page, and select View Source option to open the HTML code generated by the application.

4)      Scroll through the page source code and notice that ASP.NET has injected JavaScript code in the page to perform the validations and show the error list.

UV2

5)      Select the Script tab and expand the “{your current page}.aspx” combo to see all the referenced scripts. Notice that there are no references to the jQuery library. The validations are performed using the Microsoft Ajax Libraries together with JavaScript code injected within the page.

Case-2:

Steps to create the sample application with unobtrusive validation enabled (default for all new version(s))

1)      Now you will enable unobtrusive validation. Open Web.Config and locate ValidationSettings:UnobtrusiveValidationMode key in the AppSettings section. Set the key value to WebForms.

<appSettings>
    <add key="aspnet:uselegacysynchronizationcontext" value="false" />
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms"/>
</appSettings>

2) Open the Global.asax.cs file and add the following using statement

using System.Web.SessionState;
using WebFormsLab.Model;
using System.Web.ModelBinding;

3)      Within the Application_Start method of the Global.asax.cs file, add the following code

ScriptResourceDefinition myScriptResDef = new ScriptResourceDefinition();
myScriptResDef.Path = "~/Assets/Scripts/jquery-1.7.1.min.js";
myScriptResDef.DebugPath = "~/Assets/Scripts/jquery-1.7.1.js";
myScriptResDef.CdnPath = "http://code.jquery.com/jquery-1.7.1.min.js";
myScriptResDef.CdnDebugPath = "http://code.jquery.com/jquery-1.7.1.js";
ScriptManager.ScriptResourceMapping.AddDefinition("jquery", null, myScriptResDef);

4)      Open Site.Master. Add the code below to include a ScriptManager on the page to include a script reference to the jQuery client library.

<form runat="server">
<asp:ScriptManager ID="uxScriptManagerMasterPage" runat="server" EnableCdn="False">
<Scripts>
    <asp:ScriptReference Name="jquery" />
</Scripts>
</asp:ScriptManager>


5)      Now Press F5 to start the web application.

6)      Right-click on the browser page, and select View Source option to open the HTML code generated by the application. Notice that by enabling the unobtrusive validation ASP.NET has replaced the injected JavaScript code with data- *attributes.

UV3

Don’t want this feature enabled?

Simply add the following setting in your AppSettings section in your web.config if it doesn’t exist. If already exist make value as “none” as shown below

 <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />

Conclusion:

With Unobtrusive validation we reduce the unnecessary JavaScript code generation for all validation controls by asp.net runtime. As if we enable this validation the asp.net runtime by default get and use the jquery environment to perform these validations through this we can have least overhead of unwanted JavaScript code in our HTML page and it also effect the improvement in the performance and take less time in rendering and loading your pages.

I Hope you got advantage of this article…

Please drop a comment if you like it…

Thanks Happy Coding  🙂

November 30, 2012

Error: the exec task needs a command to execute in visual studio

Filed under: DotNet — tosaik @ 6:02 am
Tags: , ,

Got Error:

the exec task needs a command to execute in visual studio

Solution:

This is a common issue in visual studio and this error could be a cause when we add/remove the Pre-build event command line(s) and in Post-build event command line(s)…

Generally visual studio gives every line as a new command line to the command prompt to execute with “EXEC” command, if we by mistake keep any empty line… visual studio also send this empty line as a new command line and command prompt throws an error as it expects a valid command but what we sent is not a valid command to it…

as you can see the below image as i selected the empty line which by mistakenly entered in the project->properties Build Events Tab section…

ExecError

Hope this solution work for you….

Happy Coding 🙂

November 19, 2012

BadImageFormatException: Could not load file or assembly ‘x’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

Filed under: DotNet — tosaik @ 12:01 pm
Tags: , , , , ,

Exception:

BadImageFormatException: Could not load file or assembly ‘x’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

Solution:

Hi Recenty i had faced this exception and when i digg in to this issue…. and i forgot that when we are using any COM Interop DLL components in our .EXE application we need to target the respective .EXE application platform as x86

Please change the target platform to x86 in the properties section and build again before using it..

 

 

 

 

 

 

 

Hope this solution works for you too………

October 22, 2012

Download: Visual Studio Express 2012

Filed under: DotNet — tosaik @ 11:02 am
Tags: , , , ,

Visual Studio Express 2012(Free)

Microsoft Visual Studio Express 2012 for Web is a free and robust development environment for building, testing, and deploying web applications across the Microsoft Web Platform. After installation, you can try this product for up to 30 days. You must register to obtain a free product key for ongoing use after 30 days.

Download Here

Visual Studio 2012 Express for Windows Desktop(Free)

You can use Visual Studio Express 2012 for Windows Desktop to build powerful desktop apps in C#, Visual Basic, and C++. You can target client technologies such as Windows Presentation Foundation (WPF), Windows Forms, and Win32. After installation, you can try this product for up to 30 days. You must register to obtain a free product key for ongoing use after 30 days.

Download Here

Happy Coding 🙂

October 15, 2012

Converting a String to Title Case or Camel Case in C#

Filed under: DotNet — tosaik @ 6:28 am
Tags: , , ,

Now I am going to show how we can convert a string to a Title case or we also say it as Camel Case. For example I have a string as shown here


string name = "sai kUmar";

Now I need to convert the above string to Title case or we also say it as Camel Case, for this we have a special class called TextInfo which exist in the System.Globalization namespace. This special class has a special method called ToTitleCase() and which accepts only one string parameter and we need to pass our string need to convert through this parameter as shown below..


System.Globalization.TextInfo myTI = new System.Globalization.CultureInfo("en-US", false).TextInfo;
name = myTI. ToTitleCase(name);

Output: Sai Kumar

Note: Please provide your Current Culture while initializing TextInfo class object.

Hope this will helps you…

Happy Coding 🙂

September 20, 2012

Using jQuery allowing Only Alphanumeric Characters in a Textbox

Filed under: jQuery — tosaik @ 7:41 am
Tags: , , , ,

Requirement

Need to restrict the users by entering other than Alphanumeric Characters, if any user enters Non- Alphanumeric Characters then need to trim those Characters and display a message beside the respective textbox saying only Alphanumeric Characters are allowed…

 

Solution:

We have many solutions but using jQuery is the best way to achieve, with this we can use more flexible and reliable Regular expression technology with jQuery to achieve the above requirement.

 

Script Code:

 
 $(function () {

            $("#span_txtFirstName").hide();

            $("[id*='txtFirstName']").keyup(function () {

                $("#span_txtFirstName").hide();

                if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
                    this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
                    $("#span_txtFirstName").toggle(500);
                }
            });

HTML Code:

 
<asp:TextBox ID="txtFirstName" runat="server" Width="200px" CssClass="styled_input"></asp:TextBox>
<span id="span_txtFirstName" class="RegistrationNumericDiv">oops! Only AlphaNumberic Charecters are Accepted.</span>

Explanation:

In the above HTML Code is the normal code as I used asp.Net TextBox control to present this demo, but we can use any technology here as our browser parser can understand only HTML tags so every technology has their own parser to parse their respective language code to HTML code. Next line we have a SPAN tag which is used to hold the message to the user whenever user types a non-alphanumeric character in the respective textbox and this SPAN tag hide/show will be handled in the given jQuery code itself.

As we can see we use the regular expression to validate each character enter by the user and if anything wrong we replace those character(s) with empty string (‘’) and we show the message by toggling the  SPAN tag..

That’s it we are done with our requirement J

Thank you

Happy Coding J

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  🙂

Next Page »

Blog at WordPress.com.

%d bloggers like this: