Saturday, December 25, 2010

Code behind file not found

"If your code behind files are not compiling to dll then the problem could be codefile attribute, replace it with codebehind"
 
Hi All,

A few days back while working on a DNN module which is actually an extension to DNN core module Survey I faced a problem.

As this module's code comes in WSP version I copied C Sharp version of its code in DNN module template type visual studio project and then compiled it so as to get a DLL of its code. It got built after a few arrangements(like commenting a few lines :)

Then I copied the DLL to bin folder and tried to create module as per the procedure. It worked good all the way around till module creation. Then I added this newly created module to a page and BANG!!, here comes the error.

Error said,
type survey.ascx.cs not found

Note: survey.ascx is the view control of my module.

From the error message and placing code behind files in module folder I figured out that my module was able to get code in files like provider model classes and controller and info classes. It was just not getting the code-behind files of the ASCX controls used although it was able to pick codebehind code if if I place the code behind files in module's folder itself.

Thus problem became clear that something was stopping code behind files from compiling. So I compared my survey.ascx file with an old module's viewcontrol.ascx file and in the first line I got the culprit.

It was "codefile" attribute in ascx file's code directive line. I replaced it with codebehind and the problem was resolved.

So the moral of story is
if your code behind files are not compiling to dll then the problem could be codefile attribute, replace it with codebehind and enjoy!!!!!

Friday, December 24, 2010

Drop failed for database. Currently in use. Error 3702

Hi All,

Yesterday while working on DNN I needed to restore my website to a previous state. So I thought to drop my current working database and restore a backup copy.

I use MSSQL SERVER 2005. While dropping my working database I got error as below

Drop failed for database {database name}

cannot drop database because it is currently in use. Error 3702



To resolve this problem I googled a bit and then found below as a solution. This worked as a magic to my problem.

USE Master;
GO

ALTER DATABASE {database name} SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO

DROP DATABASE {database name};
GO



Hope it helps you guys also.

Friday, November 12, 2010

The name 'Session' does not exist in the current context

Today while working on an ASP.NET web application I faced this problem accessing session variable in a C# file. After thinking for a while I got the problem. Problem was that I was trying to access Session variable from a class file in the library project, and this file was neither web form, user control or a class derived from System.Web.UI.

Session variables like Session["ProjectID"] can only be accessed from Web Forms, custom controls and from class inherited from System.Web.UI

After googling a bit I found that to access value of session variable in this case we can use line like below:

HttpContext.Current.Session["ProjectID"];


Similar sort of problem I also faced while trying to get root directory of a windows application. This time while development I tried code line "System.Windows.Forms.Application.StartupPath" in visual studio quickWatch and found it to be giving correct answer(Application root directory path) but as soon as I thought to use it in my project, it refused to build saying "namespace Windows can not be found, please check for missing assembly reference". Problem in this case was similar to above i.e, this line can't be accessed from a non windows form file.

Monday, October 25, 2010

Automatically advance tab position

Below function is to automatically advance tab position from current text control to another text control as soon as soon as current textbox gets filled.

In below function

fromId : id of current text box
fromLen : length of current textBox
toId : id of target textBox

function autoAdvance(fromId, fromLen, toId, evt)
{
var textBox = document.getElementById(fromId);
var charCode = (evt.which) ? evt.which : event.keyCode;
len = textBox.value.length;
if (len == fromLen && ((charCode >= 48 && charCode <= 57) || (charCode >= 96 && charCode <= 105)))
{
document.getElementById(toId).focus();
}
}

Call this function on onKeyUp event of text box.

Thursday, October 21, 2010

Open popup and refresh parent page on close popup, in ASP.NET with help of javascript

To open pop-up

//ButtonControl is id of button on whose click pop-up needs to be opened. Place below lines on pageLoad, rowDataBound or some other place as needed.

string popupPagePath = "http://www.ravigupta.in";
ButtonControl.Attributes.Add("onclick", "window.open('" + popupPagePath + "','','width=400, height=600'); return false;");


To close child window(pop-up) and then refresh the parent window.

Javascript code for closing child and refreshing parent

window.close();
if (window.opener && !window.opener.closed) {
window.opener.location.reload();
}

For using in ASP.NET, write below on event handler of control that initiates close of popup.

ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "CloseWindow", "window.close(); if (window.opener && !window.opener.closed) { window.opener.location.reload(); }", true);


This worked for me. Hope it helps you too.

About Me

My photo
Delhi, India
Fun, music, travel and nature loving, always smiling, computer addict!!