Shahid Riaz Bhatti

if(my.work == “Interesting” || my.availableTime > my.workHours) { this.blog.Post();}

How to retrieve data from Cookie

May 26
by Shahid Riaz Bhatti 26. May 2009 12:03
 

Retrieve data from Cookies

 
 

In this small article I’ll explain that how to retrieve user’s data from the cookies.

In .Net 2005 we have several API’s related to Users and Roles. For example if you want to know that a User ABC belongs to a role “XYZ”, then you can proceed as follow:

User.IsInRole(“XYZ”);

The above will return you Boolean variable.

But for a small web site on which I was working, I got a point where I was interested to know the Role of the current logged in user. I was using forms authentication and was not using the User and Role API’s which are provided in .Net 2005.

The Solution is very simple. The overall summary is given below:

1.       Retrieve the cookie which you stored, during the login process (i.e. on Login page)

2.       Now decrypt that authentication ticket

3.       And now retrieve the user’s data from the ticket.

The code is given below:

string cookieName = FormsAuthentication.FormsCookieName;

1.       HttpCookie authCookie = Context.Request.Cookies[cookieName];

2.       FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

3.       Response.Write(authTicket.UserData);

If you have assigned any role to the user, then by using authTicket.UserData you can get those roles..

Cheers,

Filewatcher

May 25
by Shahid Riaz Bhatti 25. May 2009 07:34
 

Working with FileSystemWatcher Class

  

Hey, if you are here to find the answer of this question "how to make sure in Created event of the Filewatcher that the file which is just created is completely copied?" then you are at the right place..

FileSystemWatcher class listens to the file system change notification and raises events when a directory, or file in a directory, changes.. In this article I’ll discuss

fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)  event of FileSystemWatcher class. Working with this event is easy. You just need to capture this event and then write your code in it. BUT there is a problem in this event which is that the Created event is fired immediately as soon as the path of the file is created in the observing directory. This can be clearly explained with the help of a following scenario: I want to observe a Directory say C:\\Test. I wrote a window service which uses the FileSystemWatcher class to observe that directory. I want to pass any new file which is created in this directory to another method which processes this file. Suppose my file’s size is large (e.g. 500mb). In my window service the Created event of the FileSystemWatcher is triggered as soon as the file’s path is created in the directory, but file is not copied completely in the directory. How to solve this issue? .Net does not provide any method or event which can tell me that the file is written completely.  I tried to solve this problem and though I achieved my task, but this is not a good solution.  In the Created event I made a thread. So for each new file a new thread will be created, so we can serve each new file without any delay. In that particular thread, I tried to open the file. If file is opened successfully, then it means that file is copied completely and is available for processing. If not, then try to open the file again. Repeat this process until the file is available. Here is the code: 

void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)

{   

       Thread _MyThread = new Thread(new ParameterizedThreadStart(this.PerformActualTask));    

       object[] Parameters = new object[] {e. FullPath };    

       _MyThread.Start(Parameters);

 

private void PerformActualTask(object parameters)

  

 //The parameter for this thread body   

 object[] Parameters = (object[])parameters;  

//Full path of the newly created file  

string _SourceFile = (string)Parameters[0];  

int WaitBeforeGiveUp = 300;  

while (true  

     

 try     

        

  //Try to open the file        

  FileStream fs = File.Open(_SourceFile, FileMode.Open);       

  {         

   //We are here because file is successfully opened         

   fs.Close();      

   //Now perform operation on that file      

   }    

 }    

 catch (Exception ex)   

       

  //If exception occured then decriment the wait before give up and ask the thread to sleep       

  if (--WaitBeforeGiveUp > 0)       

        

    // file is not available yet      

  }     

 else     

         

   //if waiting time expired.         

   // SHOW GIVE UP MESSAGE     

 }      //cause the thread to sleep     

 Thread.Sleep(1000);   

} 

}

  

In the code I made some changes which are that I tried to open a file for a particular period of time. If file opened in that period then kool, else I stored a msg in the event viewer that I have given up.     

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C# | General | Tips and Tricks

How to Remove command bar button from Add-Ins

May 04
by Shahid Riaz Bhatti 4. May 2009 10:05
 

Remove command bar from Add-ins in MS Word

 

Some time we need to develop some add-ins to fulfill our requirements in different software (e.g. MS Word). Few days back I was developing an Add-ins for MS word. The requirement was to add a button in a command bar of the MS-Word. I successfully added button in the command bar and also wrote code against the event handler of that button. But unfortunately whenever I ran the MS word, my code started to add the same button in command bar again and again. To overcome this problem I wrote code which forcefully removed that custom button from the command bar on the closing of the ms word.

The code which was removing that button on the closing of the word was in the Application_DocumentBeforeClose event of Add-in. But that code didn’t give me the expected output.

There was nothing wrong in the code, because I debugged the code and in the shutdown event I made sure that the command button exists in the command bar. If it exists then delete the button. But again that code didn’t work. And interesting thing was that the code segment which was checking that the command button exists in the command bar always gave me true and in the true section I was deleting that button, but I don’t know that why it didn’t work. I was deleting the command button by using the ID of the button something like this:

1.     Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)

2.     this.Application.CommandBars.ActiveMenuBar.FindControl(Office.

3.     MsoControlType.msoControlPopup, System.Type.Missing, IDOfControl, true, true );

4.     if (foundMenu != null)

5.     {

6.       foundMenu.Delete(false);

7.     }

The above code didn’t work L 

In line 3, I used the IDOfControl to find the command bar button, but it didn’t find that control. Though I tried to use the Id.delete() method, but it also didn’t work for me. Then I changed the Line number 3 and instead of using the ID of the control to find the command bar button, I used the tag of that control and this change in code successfully find the control and this change also make the Line 6 to give the expected output.  So if you are also facing this kind of situation then try to use the tag of the button instead of its ID. I am sure it will work. Well I was developing the add-in for office 2007. This code will probably work in other version of MS word too. The reason that this code work is given below: 

Add-in Express identifies all its controls (commandbar controls) through the use of the ControlTag property (the Tag property of the CommandBarControl interface). The value of this property is generated automatically and you don’t need to change it. For your own needs, use the Tag property instead.       

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C# | C# | Tips and Tricks | Tips and Tricks

iframe source on the fly

May 01
by Shahid Riaz Bhatti 1. May 2009 06:38

This article will explain that how to load pages dynamically in IFrame from the code behind.

Actually few days back I encountered a situation where I was interested to set the src of IFrame dynamically. Some of the solutoin which I found on the internet were some thing like this:

 Declare a Generic control:
protected System.Web.UI.HtmlControls.HtmlGenericControl IFrame1;

Then, you need to do findcontrol to identify the control on the page and
typecast it:

HtmlControl IFrame1 = (HtmlControl)this.FindControl("IFrame1");

Now you can access the src property:

IFrame1.Attributes["src"] = http://www.live.com ;

I tried this solution but it did not work. Cry

SimilarlyI found many other solution saying exactly the same thing which I mentioned above. I posted on different forums and the answer was that this solution might not work if you are using master pages. And they gave me another suggestion that instead of passing IFrame1 (i.e. the ID of your Iframe) just pass the client ID of the IFrame to Findcontrol. But that solution also did not work for me.

Then I came across a POST which stated that turn your HTML snippet into server side code and then use the above code i.e. (HtmlControl IFrame1 = (HtmlControl)this.FindControl("IFrame1");)

I did not try this suggestion, instead I only turned my IFrame into a server side control by adding a runat="server" attribute into my IFrame and then like any other server control I accessed my IFrame from the code behindLaughing

I hope that this will help all who are facing same kind of problem.

Here is the summary:

  • Turn your IFrame into a server side control by adding a runat="server" attribute, like this:
<IFrame id="myDynamicFrame" scrolling="auto" runat="server"/>
  • Now you can access this control from your code behind file and you can set its src property on the fly, like this:
myDynamicFrame.Attributes["src"] = "http://www.shahidriaz.com";
 

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.Net | C# | Tips and Tricks

RecentComments

Comment RSS

Most comments

supplynflshop supplynflshop
51 comments
tiffany-bracelets tiffany-bracelets
39 comments
AVI to iPad AVI to iPad
36 comments