Monday, June 21, 2010

Microsoft Expression Studio 4 released !!

Microsoft Expression Blend is a user interface design tool developed and sold by Microsoft for creating graphical interfaces for web and desktop applications that blend the features of these two types of applications. It is an interactive, WYSIWYG front-end for designing XAML-based interfaces for Windows Presentation Foundation and Silverlight applications. It is one of the applications in the Microsoft Expression Studio suite.

Expression Blend supports the WPF text engine with advanced OpenType typography and ClearType, vector-based 2D widgets, and 3D widgets with hardware acceleration via DirectX.
  • create rich web experiences, games, desktop apps, and more
  • design apps that use the full power of Silverlight
  • take your ideas from concept to finished project work effectively with design tools, Expression Blend and Visual Studio
You will find the new version from here: http://www.microsoft.com/expression/

Sunday, June 13, 2010

How to compile C# Application for both 32 bit and 64 bit system?

I have faced this problem when one of my client try to run SMS Suit software in Windows 7 64 bit. Previously it is working perfectly in vista and windows xp 32 bit . After searching online I have found lots of solutions on it now I want to share these with you...

Solution :1

You can edit your visual studio c# project file (.csproj) and put this snippet:
1.<PlatformTarget> x86 </PlatformTarget>

inside

2.<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">


Solution :2(Command-line form)
Using CorFlags to set the 32BIT flag on the executable. E.g:
corflags.exe myapp.exe /32BIT+

This will modify your exe, so you might wanna keep a backup just in case. You can also revert the flag using /32BIT- instead of /32BIT+.


Solution :3(Simplest way)
1.Right click your Project, and select Properties.
2.In properties, select the Build tab. Under Platform target, select x86.
3.Hit Ctrl+Shift+S to save all files, right click the Solution and select "Clean" to get rid of old binaries. Any builds after that should be 32 bit .
Now it is ready for both 32 bit and 64 bit OS.



This should solve the problem ! Please inform me if there is another better way.

Wednesday, June 9, 2010

Looping Through Controls in ASP.NET

In my recent development I have badly needed to read all the controls from asp.net page. After searching I have found lots of solution. Now I want share with you one of these solution. In this solution we read all the control from asp.net page with out using master page.

**Remember to add namespace  System.Collections.Generic
 
Code: C#

private static Control FindControlIterative 
(Control root, string id)
        {
            Control ctl = root;
            LinkedList ctls = new LinkedList();

            while (ctl != null)
            {
                if (ctl.ID == id)
                    return ctl;
                foreach (Control child in ctl.Controls)
                {
                    if (child.ID == id)
                        return child;
                    if (child.HasControls())
                        ctls.AddLast(child);
                }
                ctl = ctls.First.Value;
                ctls.Remove(ctl);
            }
            return null;
        }


 Code: VB.net
  
Private Shared Function FindControlIterative
(ByVal root As Control, _
ByVal id As String) As Control

  Dim ctl As Control = root
  Dim ctls As LinkedList(Of Control) = 
                  New LinkedList(Of Control)

  Do While (ctl IsNot Nothing)
   If ctl.ID = id Then
    Return ctl
   End If
   For Each child As Control In ctl.Controls
    If child.ID = id Then
     Return child
    End If
    If child.HasControls() Then
     ctls.AddLast(child)
    End If
   Next
   ctl = ctls.First.Value
   ctls.Remove(ctl)
  Loop

  Return Nothing

 End Function

 Read Control when use Master Page:

And this is where things get a bit tricky with MasterPages. The problem is that when you use MasterPages the page hierarchy drastically changes. Where a simple this.FindControl() used to give you a control instance you now have to drill into the container hierarchy pretty deeply just to get to the content container.

Code:C#

public static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;

foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}

You can use this method this way..

this.lblError = FindControlRecursive(this.Master,"lblError") as Label;

MS Sql server: How to do multiple rows insert ?

Recently I have faced a problem when insert multiple row insert at single call. After first row insert then problem arise from  second row insert. After goggling I have found a solution : it need to clear parameter after each row insert. I want to share that example with you:

Code :C#
// Arranging Data in an Array.
const int no_of_values = 2;
int[] val1 = new int[no_of_values];
int[] val2 = new int[no_of_values];
val1[0] = val1;
val2[0] = val2;
val1[1] = val11;
val2[1] = val22;
// Do the inserts using Parameterized queries.
Connection.Open();
SqlCeCommand command = Connection.CreateCommand();
command.CommandText = "Insert INTO [Table] (col1, col2) Values (...val1, ...val2)";
for (int i = 0; i < no_of_values; i++)
{
command.Parameters.Clear(); //it needs to clear parameter after each row inserted.
command.Parameters.Add("...val1", val1[ i ]);
command.Parameters.Add("...val2", val2[ i ]);
command.ExecuteNonQuery();
}
Connection.Close();

I have fixed this problem for MS SQL Server, it is also same for oracle too.
Hope that it may helpful for developers.

Monday, June 7, 2010

String Formatting in C#

Numbers

Basic number formatting specifiers:
Specifier Type Format Output (Passed Double 1.42) Output (Passed Int -12400)
c Currency {0:c} $1.42 -$12,400
d Decimal (Whole number) {0:d} System.FormatException -12400
e Scientific {0:e} 1.420000e+000 -1.240000e+004
f Fixed point {0:f} 1.42 -12400.00
g General {0:g} 1.42 -12400
n Number with commas for thousands {0:n} 1.42 -12,400
r Round trippable {0:r} 1.42 System.FormatException
x Hexadecimal {0:x4} System.FormatException cf90

Custom number formatting:
Specifier Type Example Output (Passed Double 1500.42) Note
0 Zero placeholder {0:00.0000} 1500.4200 Pads with zeroes.
# Digit placeholder {0:(#).##} (1500).42
. Decimal point {0:0.0} 1500.4
, Thousand separator {0:0,0} 1,500 Must be between two zeroes.
,. Number scaling {0:0,.} 2 Comma adjacent to Period scales by 1000.
% Percent {0:0%} 150042% Multiplies by 100, adds % sign.
e Exponent placeholder {0:00e+0} 15e+2 Many exponent formats available.
; Group separator see below

The group separator is especially useful for formatting currency values which require that negative values be enclosed in parentheses.

This table will help you to format the string value to double value:

Value           Format String              Result
1234.567     {0:0.00}                      1234.57
1234.567     {0:00000.0000}          01234.5670
1234.567     {0:#####.##}             1234.57
1234.567     {0:#.###}                   1234.567
1234.567     {0:#.#}                       1234.6
1234.567     {0:#,#.##}                  1,234.57
1234.567     {0:$#,#.##}                $1,234.57
1234.567     {0:$ #,#.##}               $ 1,234.57
1234.567     {0:($ #,#.##)}             ($ 1,234.57)
-1234.567    {0:#,#.##}                 -1,234.57
.1234           {0:#%}                       12%
.1234           {0:Percent = #.0%}     Percent = 12.3%

Example:
double dValue=1234.567;
string sCurrency =  dValue.ToString(" {0:0.00}",dValue);
Output: 1234.57

Or,
double dValue=23.00;
String.Format("{0:$#,##0.00;($#,##0.00);Zero}",dValue );
This will output “$1,240.00″ if passed 1243.50. It will output the same format but in parentheses if the number is negative, and will output the string “Zero” if the number is zero.

For more info: http://blog.stevex.net/string-formatting-in-csharp/

Deprecated HTML elements /Bad Tag

There are several HTML elements and attributes that have now been declared deprecated by the World Wide Web Consortium (the organization that sets HTML standards).

'Deprecated' means that the elements no longer serve a purpose and have been replaced by other methods, mostly involving cascading stylesheets (CSS). Although it is recommended that web browsers continue to support them, eventually they will become obsolete.

Here lists all the deprecated elements and attributes of HTML 4..
  1.  The FONT and BASEFONT tags
  2.  The CENTER tag and ALIGN attribute
  3. The U, S and STRIKE elements
  4. The BACKGROUND and BGCOLOR attribute
  5. The BORDER attribute
  6. The TEXT, LINK, ALINK and VLINK attributes
  7. The HPSACE and VSPACE attributes
  8. The LANGUAGE attribute
  9. The CLEAR attribute
  10. The WIDTH and HEIGHT attributes
  11. The TYPE attribute for lists
  12. The START and VALUE attributes for lists
  13. The COMPACT attribute for lists
  14. The APPLET element
  15. The DIR and MENU elements
  16. The ISINDEX element
  17. The NOSHADE and SIZE attributes for HR
  18. The NOWRAP attribute
  19. The VERSION attribute
For more details....Deprecated HTML element/ Bad Tag

How to get File size in ASP.NET/C#

In asp.net forum I have got this question many times like this, "How to get File size" or "How to get File Length". For this today I want to share with you how to get the file size in asp.net. Its very simple but helpful...

Code:C#

Add namespace:
Using System.IO;


string sMyFileName = "~/photos/test.jpg";

FileInfo finfo = new FileInfo(Server.MapPath(sMyFileName ));
long FileInBytes = finfo.Length;
long FileInKB = finfo.Length / 1024;
long FileInMB = FileInKB /1024;

Response.Write("File Size: " + FileInBytes.ToString() +
" bytes (" + FileInMB.ToString() + " MB)");

Windows 7 : C# Error:'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine

Problem

Today I have faced this error when try to use MS Access database from SMS software. After goggling I have found the solution in ...
MSDN forum


Solution

The behavior you described is expected. If your application runs in 64-bit mode, all of the components it uses must also be 64-bit. There is no 64-bit Jet OLE DB Provider, so you get the message described. You would receive a similar error when trying to connect to a database using OLE DB or ODBC if there is no 64-bit version of the specified OLE DB provider or ODBC driver.
This problem only occurs in applications that run in 64-bit mode. Compiling the application so it runs only in 32-bit mode is the best current solution.

How to compile application to run in both 32 bit or 64 bit?
You can do it several ways...

Solution :1

You can edit your visual studio c# project file (.csproj) and put this snippet:
1.<PlatformTarget>x86</PlatformTarget>
inside
2.<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">


Solution :2
Using CorFlags to set the 32BIT flag on the executable. E.g:
corflags.exe myapp.exe /32BIT+

This will modify your exe, so you might wanna keep a backup just in case. You can also revert the flag using /32BIT- instead of /32BIT+.

Solution :3(Simplest way)
1.Right click your Project, and select Properties.
2.In properties, select the Build tab. Under Platform target, select x86.
3.Hit Ctrl+Shift+S to save all files, right click the Solution and select "Clean" to get rid of old binaries. Any builds after that should be 32 bit .
Now it is ready for both 32 bit and 64 bit OS.

This should solve the problem ! Please inform me if there is another better way.

Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'

Exception: Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'...