Thursday, September 27, 2012

DBNull check and some other “Tips” from VB.net to c# project

In my previous blog post I have shared an easy tool to convert vb.net project. I have done a successful conversion of my projects data Layer from vb.net to c#, turned out it was awesome and less painful. So far every thing seems okay.

Then I found a small anomaly. In project reference I can see that “Microsoft.VisualBasic” library is referenced. That doesn’t sound right. And I deleted the reference and recompile the project. Some of the class thrown some compile error, and I had to fix them manually.

image

And today, I want to share those learning with the community. If you “Google” c# to vb.net cheat sheet you would get some very good cheat sheets.

VB.net Information.IsDBNull

There is my first lesson: When I deleted the “Microsoft.VisualBasic” I found out this little piece of code Information.IsDBNull:

protected static object SafeDisplayValue(object Value)
{
if (Information.IsDBNull(Value))
{
return null;
}
else
{
return Value;
}
}

So what's the equivalent c# code? bellow.


protected static object SafeDisplayValue(object Value)
{
if (Convert.IsDBNull(Value))
{
return null;
}
else
{
return Value;
}
}

Pretty interesting right, why IsDBNull check got into System.Convert namespace. Any way a good lesson learned.


Constants.vbNewLine


image


Any guess? yes that’s right “Environment.NewLine”


DateAndTime.Today


image


Yes that’s right DateAndTime.Today = DateTime.Now


I have more to share will share in next post.

Thursday, September 20, 2012

An easy tool to convert vb.net project to c# project in a flash

I would like to share a very interesting and helpful tool that made my life easier and hope in some corner another developer will be beneficial with it.
Recently we had a solution from one of our customer where we had around 50 project mixed and matched with VB.net and c# projects. It was getting hectic for our developers to switch context and syntax.
I finally took liberty to find out an easy way to convert the projects. With little bit of goggling I would this awesome tool that made my developer’s life easy.

VB.Net to C# Converter 3.02

image
It you can choose to select one single file or entire project or even a solutions, isn’t it great. After proceed with the conversion project you would see some warning and and possible conversion error. Those are easily handballed.
It took only 1 hour or so to convert a project with all kind of migration. I removed the old vb.net project and then add back the c# project. fixed the project references. That’s it.

Thursday, September 13, 2012

Visual Studio 2012 Web publisher Wizard messed up!

Hi guys want to share a tittle experience while testing some new feature of visual studio 2012, its one of the oldest feature, but found a bug today. Bellow I shared the screen shot.

image

Ah… looks kind of smashed right?, How did it get there, few simple steps actually,

Step1: Right Click on Web project and select “Publish” to lunch the publish wizard.

Step 2: Click on Click on the drop down and will find a new option called “<New…>”

Step 3: Now click on the <New…> this will bring out a window like bellow

image

Step 4: Now don’t put anything on the text box of the “New Profile” dialog , rather click on “Cancel”

Step 5: Now you would see the connection , settings, and preview tab got enabled.

Step 6: Click on Preview tab, you would see that the window is kind of messed up.

Hope it would be fix soon.

Until next time , cheers!

Monday, September 10, 2012

WOW, We can now debug browser specific right from our VS

Hi Guys, I kind of got excited and sharing it with the community, if you have different browser installed in your machine you can now debug web application as browser specific.

Bellow I have shared a screen shot with with browser selection menu while debugging a web application. 

image

Me like it!. Cheers!

How to resolve “_doPostBack is undefined” JavaScript error in IE10?

Today I am going to share a small piece of information, most probably all of you know it, still sharing it as it caused significant amount of time killing on our development team.

The problem started when we noticed no submit button is working in ie 10 in one of our development project. further more our client identified this bug as showstopper which raised the importance level of the issue to critical for the project. Hence we decided to take a look at this issue quickly.

After few minutes of testing we didn’t noticed any problem that can cause this issue, Later we enabled “Display a notification about every Script Error” in IE 10 and then we quickly find out the issue.

image

Then immediately the problem got visible to all of us. there is what the script error looks like.

image

What is the problem?

According to Scott Hanselman

“There is a bug in the browser definition files that shipped with .NET 2.0 and .NET 4, namely that they contain definitions for a certain range of browser versions. But the versions for some browsers (like IE 10) aren't within those ranges any more. Therefore, ASP.NET sees them as unknown browsers and defaults to a down-level definition, which has certain inconveniences, like that it does not support features like JavaScript. “

What is The solution?

There are two fixes for this problem

  1. Machine-wide fixes
  2. Site-only fixes

Machine-wide fixes

Please visit Hanselman blog for more information on this,

According to Mr. Hanselman

“We're releasing a hotfix that will fix these, which you'll be able to get to via some KB articles. These KBs with fixes are live and are the best way to update your system. The fixes solve the browser-detection issue forever for all sites on a machine. These will be rolled up into future versions of the framework and will eventually also be on Windows Update.

What the fixes do is update the ie.browser and firefox.browser files in \Windows\Microsoft.NET\Framework\<version>\Config\Browsers with new and future-proofed versions of these browser definitions. Nothing else is affected.”

 

Site only fixes

Go to http://nuget.org/packages/App_BrowsersUpdate, and copy the command and use nuget console to install right browser files.

The command is given bellow.

PM> Install-Package App_BrowsersUpdate

Make sure you have nuget package manager and then run the command from

image

As a Result two browser file will be added in App_Browsers folder.

image

Now if you deploy your application again you will see that every thing is working file. Cheers!

Reference

http://www.hanselman.com/blog/BugAndFixASPNETFailsToDetectIE10CausingDoPostBackIsUndefinedJavaScriptErrorOrMaintainFF5ScrollbarPosition.aspx

Thursday, September 6, 2012

How to resolve asp.net resend confirm with a wired technique?

Introduction

Today I want to share a small piece of information that I learned about Resend confirmation, window of browsers. Frist I thought that this is a default feature of all browser and can not be bypassed, but I was very wrong about it. Yes there is a real and technically sound way to over come this problem.

image

the problem

First lets discuss what actually happens. Lets say we have a button in our asp.net page and an event handler for that button. Upon clicking on the button we change the text of the textbox. pretty straight forward requirement. and the code is relatively simple.

Code for the button handler is given bellow:

using System;

namespace AspDotNetFormReSend
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1Click(object sender, EventArgs e)
{
TextBox1.Text = "Button Clicked!";
}
}
}

Clicking on the button works perfect. And get same result each time I click on it. But what happens when I try to refresh the browser with F5 Key or Reload Context menu or browser button. It simply throws a confirmation window asking user to confirm to resend the browser information once again.


How to resolve Resend?


Pretty Simple solution indeed, after googling for a few minutes found couple of suggestion and all of them saying pretty much same thing, “do a Response.Redirect to same page once again”, this will resolve the issue. Well I put the idea into text and the result is amazing.


Modified code for button and page load is given bellow:


using System;

namespace AspDotNetFormReSend
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var httpCookie = Request.Cookies["Action"];
if (httpCookie != null)
{
string action = httpCookie.Value;
if (string.IsNullOrEmpty(action) == false && action.Equals("Button_Click"))
{
TextBox1.Text = "Button Clicked!";
//Response.Cookies["Action"].Expires = DateTime.Now.AddSeconds(-100);
}
}
}
}

protected void Button1Click(object sender, EventArgs e)
{
Response.Cookies["Action"].Value = "Button_Click";
Response.Cookies["Action"].Expires = DateTime.Now.AddSeconds(100);
Response.Redirect("Default.aspx");
}
}
}


What we did here is to set what button action we want to perform in cookie and then do a Redirect in the same page. On Page_Load event I captured button action from cookie and then performed the desired changes in the textbox. Note that if you want to perform any action just once you should remove the cookie right after you used it.


Advantages


This works pretty great even if you are having a stateless web site, means no session state and view state. One more advantage is that if you navigate from page to page, since you set the action on cookie upon return to the same page you would get same data you kept few minutes or hours ago without pressing action button. Use the cookie smartly for more innovative interactions.


There are some down side also, but lets keep them hidden for now, and enjoy the moment thinking we have resolved the Resend issue.


Conclusion


In this short article we have seen how to resolve browser resend issue using cookie and Redirect to same page technique, please leave a comment for suggestion and criticism.


Looking forward to share more about this in future.