Archive for category C#
How to manipulate a Windows Forms control from another thread and get return value via Invoke() method
Posted by Imran Akram in C# on June 27, 2011
I was working on a Windows Forms project and I had this requirement to basically do some manipulation on a TreeListView on the parent form (MainForm in this case) for which we already had a developed a method and also to get the return value back from it. To make life even easier, we needed to do this from a BackGroundWorker thread in this case. Now, the problem with Windows Forms controls is that they can’t be manipulated apart from the main thread, so I had to look for a work around of some sort. So here’s a little modified snippet of the code for your understanding:-
if (m.treeListView.InvokeRequired)
{
ra= (ReportData?)m.treeListView.Invoke(new Func<ReportData?>(
() => m.CreateDataforReport(“Rpt type”, false)));
//m is the main form’s reference!
}
So what I’ve done is that I passed the main form’s reference to the child form in the constructor and made the TreeListView and the CreateDataforReport public. Notice that I called CreateDataforReport method inside the Func<ReportData?> and not directly. This is what enables you to get the appropriate return value, be it string, integer or any complex type like I have it here.
The type ‘System.IO.Packaging.Package’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘WindowsBase’
Posted by Imran Akram in C#, VB .NET, XML on June 1, 2011
While I was working with the Open Office XML SDK 2.0, after I added the reference to the DocumentFormat.OpenXml assembly and tried to build the application I ran into this error message.
The type ‘System.IO.Packaging.Package’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′
Turns out, all you need to do to fix it is to just add another reference to WindowsBase assembly that is found in the .NET Tab.
Could not execute CVTRES.EXE
Posted by Imran Akram in C#, VB .NET, Visual Studio 2005 on June 7, 2009
Today I started woking on a windows application at home and I was very annoyed by the error I was getting whenever I tried to debug/build the application that said: “Could not execute CVTRES.EXE“
Tried lots of things like giving full rights to “everyone” on the debug folder and some other silly things, untill I found out that there was something in ZoneAlarm that was causing the problem. I had to uninstall and re-install ZoneAlarm. Turning it off was not sufficient. I never understood what was causing the problem at that time and to be honest I’m not too concerned with that either. May be there was some setting that needed a reset or something. But anyway like they say: “All is well that ends well”!
Hope some of you might find this useful someday.
How to use an OR in a query in EntitySpaces?
Posted by Imran Akram in ASP .NET, C#, Web Development on March 25, 2009
It’s simple, yet not very obvious. All you have to do is to change the normal behavior of the commas you use in the Where function to act as an OR. You’d do it like this:-
ObjES.Query.es.DefaultConjunction = esConjunction.Or;
ObjES.Query.Where(<<Go ahead with using the commas that usually defaults to an AND conjunction>>);
oh and I almost forgot to mention my colleague Bilal mentioned this to me. So special thanks to him as well.
How to remove ReadOnly Attribute from a file?
Posted by Imran Akram in ASP .NET, C#, Web Development on January 15, 2009
Hello friends,
Here’s a nice little piece of code you can use to change file attributes of a particular file, its particularly useful when you’re trying to delete a read only file.
string savedCardFilePath = “My absolute file path”;
if (File.Exists(savedCardFilePath))
{
/*
* Remove readonly attribute off of the file if it exists before execution
*
**/
if(File.GetAttributes(savedCardFilePath) == FileAttributes.ReadOnly)
File.SetAttributes(savedCardFilePath, FileAttributes.Normal);
File.Delete(savedCardFilePath);
}
UPDATE May 14, 2012:
There have been some problems reported and better solutions given on this article so I updated the code to:
FileAttributes attributes = File.GetAttributes(file);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
File.SetAttributes(file, attributes ^ FileAttributes.ReadOnly);
Thanks for the feedback and sorry about the mistake
How to use OrderBy() for multiple columns in EntitySpaces?
Posted by Imran Akram in ASP .NET, C#, VB .NET, Web Development on December 31, 2008
You can pass multiple columns in like this:-
collection.Query.OrderBy(collection.Query.ProductID.Descending, collection.Query.CategoryID.Ascending)
How to manipulate Session in Http Handlers
Posted by Imran Akram in ASP .NET, C#, VB .NET, Web Development on December 26, 2008
There is a common issue that you can’t manipulate session objects in http handlers, the files with ASHX extension
Well normally you can read the contents in the session via context.Session but you can add/remove/modify that in there. So how do you do this:
The answer is simple: Just implement the IRequiresSessionState interface. It doesnt have any method to implement so you dont have to worry about any details, this just notifies the runtime that Session can be manipulated in this context. So your class declaration would look something like this:-
public class SetLabels : IHttpHandler, IRequiresSessionState
{
…
How to get the file name from a full file path?
Posted by Imran Akram in ASP .NET, C#, VB .NET on November 15, 2008
If you have a full file path like D:\My Documents\myimg.jpg and you want to get just the file name myimg.jpg, you can get it with the help of the Path class in this fashion:-
In C#
string filename = Path.GetFileName(“D:\My Documents\myimg.jpg“);
In VB .NET
Dim fileName as String = Path.GetFIleName(“D:\My Documents\myimg.jpg”)
How to generate Random numbers in .NET
Posted by Imran Akram in C#, VB .NET on September 26, 2008
Actually these are not exactly ‘true’ random numbers. These are actually pseudo-random numbers, which means that they give you an illusion that they’re being generated randomly.
It’s very simple: There’s a class called System.Random. and the rest is as simple as this:-
Random r = new Random(0);
int num = r.Next(0,999999);
in VB .NET
dim r as Random = new Random(0)
dim num as Int = r.Next(0,999999)
