Sep 10 2008

Configuring log4net

Category: Programmingavinashsing @ 9:10 am

Tried Enterprise Library Logging Application Block but i’ll have to say that log4net is easier and faster to use. So here’s how to configure it. With the log4net.dll in your bin, you will need to open your web.config file and add the following line to your configSections node:

<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net” />

The after the </configSections> or anywhere else within <configuration>,

<log4net configSource=”Config\log4net.config” />

Note: I store my log4net configuration as a separate xml file in a Config folder!

Your log4net.config file should look like this:

<!– Logging related config options below this point –>
<log4net>
<appender name=”RollingFileAppender” type=”log4net.Appender.RollingFileAppender”>
<file type=”log4net.Util.PatternString” value=”%property{logpath}”/>
<param name=”Threshold” value=”DEBUG”/>
<appendToFile value=”true”/>
<rollingStyle value=”Size”/>
<maxSizeRollBackups value=”10″/>
<maximumFileSize value=”10MB”/>
<staticLogFileName value=”true”/>
<layout type=”log4net.Layout.PatternLayout”>
<conversionPattern value=”%date [%thread] %-5level %logger - %message%newline”/>
</layout>
</appender>

<root>
<appender-ref ref=”RollingFileAppender”/>
</root>
</log4net>

Note 2 : I use only rolling file appender.

In my global.asax, i have this:

private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

static MyApplication()
{
log4net.GlobalContext.Properties["logpath"] = AppDomain.CurrentDomain.BaseDirectory + “Log\\Log.txt.not”; ;
log4net.Config.XmlConfigurator.Configure();
}

And in the classes i want to use log4net, i have:

private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

protected void Page_Load(object sender, EventArgs e)
{
log.Debug(”I am logging, yeh!”);
}


Sep 10 2008

Turning identity off for a column in SQL Server

Category: Programmingavinashsing @ 2:56 am

I’m currently moving my MySql database to SQL Server and for that reason i need to preserve the identity columns which i already have in the tables. Here’s how it’s done:

SET IDENTITY_INSERT tablename ON
SET IDENTITY_INSERT tablename OFF

So to insert the row ID=12, Name=Alfred, Age=23 in tblUsers, i would use the syntax below:

SET IDENTITY_INSERT tablename ON
INSERT INTO tblUsers (ID, Name, Age) VALUES (12, ‘Alfred’, 23)
SET IDENTITY_INSERT tablename OFF

You will need to turn the identity insert off so that SQL Server automatically generates the unique id for you afterwards.


Sep 04 2008

Calculating x and y coordinates of an element in JavaScript

Category: Programmingavinashsing @ 3:26 am

I was trying to get the Swazz calendar to work on Firefox but it would get displayed at the top on the webpage instead of showing up just under the textbox element which required the calendar values. So while debugging it i noticed that i had the the Transitional DocType on and it was messing around with it. I googled the problem and found that the solution was to ‘px’ to the value otherwise Firefox would ignore it.

getObj(’fc’).style.left=Left(ielem) + ‘px’;
getObj(’fc’).style.top=Top(ielem)+ielem.offsetHeight + ‘px’;

And this is how you get the coordinates with JavaScript:

function Left(obj)
{
var curleft = 0;
if (obj.offsetParent)
while (1) {
curleft += obj.offsetLeft;
if (!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if (obj.x)
curleft += obj.x;
return curleft;
}

function Top(obj)
{
var curtop = 0;
if (obj.offsetParent)
while (1) {
curtop += obj.offsetTop;
if (!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if (obj.y)
curtop += obj.y;
return curtop;
}


Sep 02 2008

Access to bin path denied in ASP.NET Web Application

Category: Programmingavinashsing @ 5:43 am

Just downloaded a solution from Visual Source Safe and I was getting lots of errors due to references not being found. So I went through each project and removed the old references and replaced them with the new references. However when I finally came down to the web app, it would not compile because it was unable to copy dll to the bin because access to it was denied. I checked the properties for the bin folder and noticed that it was readonly. So I unchecked the readonly property and applied it to all sub-folders and voila, the compiler is happy now and so I am :)


Sep 01 2008

Problem with SCOPE_IDENTITY() in ADO.NET

Category: Programmingavinashsing @ 5:40 am

I’m using .NET 3.5, Visual Studio Team System 2008 and SQL Server 2005 and i’m trying to return the new id that is created after an INSERT statement. When i call ExecuteScalar, it returns zero. I’ve googled the problem and it seems that you need to cast the scope_identity to int before returning it.

Here are 3 solotions for the problem:

1. SELECT CAST(SCOPE_IDENTITY() AS INT) instead of just SELECT SCOPE_IDENTITY()

2. Declare a variable to hold the scope identity as follows:

DECLARE @NewID INT

SET @NewID =  SELECT SCOPE_IDENTITY()

3. Have an output parameter in your stored procedure


Aug 27 2008

View code not appearing in Visual Studio 2008

Category: Programmingavinashsing @ 2:52 am

Another thing that I need to do is download the hot fix which addresses a couple of problems with Visual Studio 2008 Team Suite so that it can get rid of the annoying of having to go to the Solution Explorer to be able to “view code” in a Web Application rather than right clicking on an aspx page and selecting the “view code” option. The hot fix download is located below:

https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=10826


Aug 26 2008

How to find out what access users have in Windows?

Category: Programmingavinashsing @ 9:22 am

Many times you need to find out what access a particular windows user has and it’s not very easy to get that information. I wanted to get a list of folders/directories to which a particular user (local user in windows) had access to so that i could debug an application. Luckily, i found about a small command line utility tool which enables you to do just that. You will need to download the AccessCheck program and use it to get the access report.

http://technet.microsoft.com/en-gb/sysinternals/bb664922.aspx
http://windowsitpro.com/article/articleid/97672/using-accesschk-to-view-which-files-and-folders-a-user-has-access-to.html


Aug 18 2008

Integrating FCKeditor in my ASP.NET site

Category: Programmingavinashsing @ 5:42 am

So I wanted to try FCKeditor in a website i was redesigning. The first problem i foresaw was that it didn’t have an image resize option. However it seems to be the one closest to meet my requirements, therefore i’m thinking of hacking an image resize utility into the library.

As soon as i copied everything to my project, I got an HTTP Error 404 - Not found. I was like, what the hell just happened there, but then i googled the problem and find the solution. You need to specify the BasePath  (the path where your fckeditor script is located) as follows:

<FCKeditorV2:FCKeditor ID=”FCKeditor1″ runat=”server” BasePath=”~/FCKEditor/” ></FCKeditorV2:FCKeditor>

This gets rid of the error.

However if you want to take advantage of the image uploader, you will need to edit fckeditor.js as follows:

var _FileBrowserLanguage    = ‘aspx’ ;    // asp | aspx | cfm | lasso | perl | php | py
var _QuickUploadLanguage    = ‘aspx’;

This tells the script to use asp.net as the server side language.

You will also need to modify config.ascx (fckeditor/editor/filemanager/connectors/aspx/config.ascx):

In CheckAuthentication, return true!

In SetConfig(), specify where the files which are uploaded are going to go:

UserFilesPath = “~/userfiles/”;

It is also good to have all images/files/media in separate folders but by default this is not true for quick uploads. So do this for File, Image, Flash and Media:

TypeConfig["File"].QuickUploadPath = “%UserFilesPath%file/”;

TypeConfig["Image"].QuickUploadPath = “%UserFilesPath%image/”;

TypeConfig["Flash"].QuickUploadPath = “%UserFilesPath%flash/”;

TypeConfig["Media"].QuickUploadPath = “%UserFilesPath%media/”;

Remember to give ASP.NET worker process read/write access to the uploaded folder (UserFiles)!


Aug 18 2008

Setting up Visual Studio for improved productivity

Category: Programmingavinashsing @ 5:31 am

Once you install Visual Studio, there are a few things which you need to do so that you are comfortable with the environment and to make your life easier.  Here’s what i like to do:

1. Show line numbers!

2.  Insert attribute value quotes when typing

This can be enabled by
Tools -> Options -> Text Editor -> HTML -> Format
turn on - “Insert attribute value quotes when typing”

3. Change the colours of delegates, Enum etc

More on that later…

4.  If you don’t want IDs of controls to be forced to be unique

Go to Tools -> Options -> Text Editor -> HTML -> Miscellaneous
turn off - “Auto ID elements on paste in source view”


Jul 31 2008

Using Windows Control on an ASP.NET web page

Category: Programmingavinashsing @ 2:14 am

There can be a number of occasions when you would want to use a windows control on your asp.net webpage. For my one, I wanted to bypass the proxy server at work by using the WebBrowser control. The problem was that it was only available to use on Windows Forms and I needed it to work on my asp.net webpage. The idea behind it was that I would be able to browse to my web page which contained the WebBrowser control (as this page would not be banned at work because it will be hosted on a shared hosting server) and all web page requests would be provided by the hosting company rather than directly through the internet connection at work.

I created a Windows Control Library which contained the WebBrowser control and dropped the dll in the root of the website where I would call it from. I made a simple page and within the web form I added the following:

<!– Code Starts Here –>
<OBJECT id=”sert” classid=”http:GisBrowser.dll#GisBrowser.SurfControl” height=”450″ width=”800″ VIEWASTEXT>
<PARAM NAME=”Title” VALUE=”My Title”>
</OBJECT>
<!– Code Ends Here –>

The reason the dll does not go into the bin folder is because it needs to be accessible by the clients (browsers requesting the page) and anything in the bin folder is available to the server only.

Unfortunately the code did not work but this happens only with the WebBrowser control. If you choose any other control, it should work fine. I tried it with a label, treeview and a groupbox and they worked fine.

For references, please see:

http://www.beansoftware.com/ASP.NET-Tutorials/Place-Windows-Control-To-Web-Form.aspx

http://aspnet.4guysfromrolla.com/articles/052604-1.aspx 

On a side note, if you ever wanted to grab the thumbnail of a website dynamically using C#, this link will tell you how to do just that :

http://www.beansoftware.com/ASP.NET-Tutorials/Get-Web-Site-Thumbnail-Image.aspx 


Next Page »