Sehajpal.com

No path is too difficult. No destination is too far !

Follow me on TwitterRSS Feeds

  • Home
  • About Ashish
  • Technical Arena
    • Dot Net Questions
    • WCF Questions
    • Design Patterns
    • Interested in XAML?
  • Job Openings

Bind with Enum in Silverlight

Feb 23rd

Posted by Ashish Sehajpal in Development

5 comments

Silverlight doesn’t have any kind of ObjEctDataProvider with it. So the famous example of binding the controls with an ObjectDataProvider doesn’t work with silverlight as it works with WPF :(

<UserControl.Resources>
<ObjectDataProvider MethodName=“GetValues” ObjectType=”{x:Type sys:Enum}“  x:Key=“AlignmentValues”>   <ObjectDataProvider.MethodParameters>
   
      <x:Type TypeName=“HorizontalAlignment” />
   </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

This can be done via code behind in the Silverlight.  Just add a reference to the Reflection assembly.
using System.Reflection;

Use this simple function to get IEnumerable collection out of Enum type.

        public IEnumerable<Enum> GetEnumValues(Enum enumeration)
        {
            return from gField in enumeration.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)
                   select (Enum)gField.GetValue(enumeration);
        }

Just bind the results of this function with your control and its done !

 ComboFrom.ItemsSource = GetEnumValues(new SehajService.Currency());

Bind with Enum, SilverLight

Currency amount in words in indian format using T-SQL Stored Procedure

Feb 18th

Posted by Ashish Sehajpal in Development

1 comment

I know it is not that much crucial but neither easy to get rid of financial application’s requirements as clients demand more and more facilities even they are small in size.

Here is one that kinda SQL Script that every finance domain expert looks for. That is the currency writer in WORDS instead of digits.

CREATE FUNCTION dbo.AMOUNT_IN_WORDS
(
 @Amount INT,
 @PAISE INT
)
RETURNS int

BEGIN

DECLARE
–Step 1:
–Here we define the temporary tables to hold the WORDs representation of possible digits/amounts

DECLARE @INNTBL_01 TABLE (RECNO INT IDENTITY(1, 1), MTEXT NVARCHAR(50))

DECLARE @INNTBL_02 TABLE (RECNO INT IDENTITY(1, 1), MTEXT NVARCHAR(50))

–Step 2:
–Now add up the salt and pepper onto table  i.e. insert the WORDs into it

INSERT INTO @INNTBL_01 SELECT ‘ONE’
INSERT INTO @INNTBL_01 SELECT ‘TWO’
INSERT INTO @INNTBL_01 SELECT ‘THREE’
INSERT INTO @INNTBL_01 SELECT ‘FOUR’
INSERT INTO @INNTBL_01 SELECT ‘FIVE’
INSERT INTO @INNTBL_01 SELECT ‘SIX’
INSERT INTO @INNTBL_01 SELECT ‘SEVEN’
INSERT INTO @INNTBL_01 SELECT ‘EIGHT’
INSERT INTO @INNTBL_01 SELECT ‘NINE’
INSERT INTO @INNTBL_01 SELECT ‘TEN’
INSERT INTO @INNTBL_01 SELECT ‘ELEVEN’
INSERT INTO @INNTBL_01 SELECT ‘TWELVE’
INSERT INTO @INNTBL_01 SELECT ‘THIRTEEN’
INSERT INTO @INNTBL_01 SELECT ‘FOURTEEN’
INSERT INTO @INNTBL_01 SELECT ‘FIFTEEN’
INSERT INTO @INNTBL_01 SELECT ‘SIXTEEN’
INSERT INTO @INNTBL_01 SELECT ‘SEVENTEEN’
INSERT INTO @INNTBL_01 SELECT ‘EIGHTEEN’
INSERT INTO @INNTBL_01 SELECT ‘NINETEEN’
INSERT INTO @INNTBL_01 SELECT ‘TWENTY’–Similary, insert the multiples

INSERT INTO @INNTBL_02 SELECT ‘TEN’
INSERT INTO @INNTBL_02 SELECT ‘TWENTY’
INSERT INTO @INNTBL_02 SELECT ‘THIRTY’
INSERT INTO @INNTBL_02 SELECT ‘FORTY’
INSERT INTO @INNTBL_02 SELECT ‘FIFTY’
INSERT INTO @INNTBL_02 SELECT ‘SIXTY’
INSERT INTO @INNTBL_02 SELECT ‘SEVENTY’
INSERT INTO @INNTBL_02 SELECT ‘EIGHTY’
INSERT INTO @INNTBL_02 SELECT ‘NINETY’

–Step 3:
–Check for the limit of the amount i.e. what is the place value of digits – LACs, Thousands or Hundreds

DECLARE @WORD VARCHAR(300)
SELECT @WORD = ”

DECLARE @M_AMT01  INT, @M_AMT02 INT

IF @AMOUNT < 10000000 AND @AMOUNT >= 100000 BEGIN
SET @M_AMT01 = @AMOUNT
SELECT @AMOUNT = ( @AMOUNT % 100000 )
SET @M_AMT01 = ( @M_AMT01 – @AMOUNT ) / 100000
DECLARE @WORD1 VARCHAR(300)
SET @WORD1 = ”
IF @M_AMT01 < 100 AND @M_AMT01 > 20 BEGIN
SET @M_AMT02 = @M_AMT01
SET @M_AMT01 = ( @M_AMT01 % 10)
SET @M_AMT02 = ( @M_AMT02 – @M_AMT01 ) / 10
SET @WORD1 = ( SELECT @WORD1 + MTEXT FROM @INNTBL_02 WHERE RECNO = @M_AMT02 )
END

IF @M_AMT01 <= 20 AND @M_AMT01 <> 0 BEGIN
SET @WORD1 = ( SELECT @WORD1 + MTEXT FROM @INNTBL_01 WHERE RECNO = @M_AMT01 )
END
SET @WORD = @WORD + @WORD1 + ‘ LAC ‘
END
IF @AMOUNT < 100000 AND @AMOUNT >= 1000 BEGIN
SET @M_AMT01 = @AMOUNT
SET @AMOUNT = ( @AMOUNT % 1000 )
SET @M_AMT01 = ( @M_AMT01 – @AMOUNT ) / 1000
SET @WORD1 = ”
IF @M_AMT01 < 100 AND @M_AMT01 > 20 BEGIN
SET @M_AMT02 = @M_AMT01
SET @M_AMT01 = ( @M_AMT01 % 10 )
SET @M_AMT02 = ( @M_AMT02 – @M_AMT01 ) / 10
SET @WORD1 = ( SELECT @WORD1 + MTEXT + ‘ ‘ FROM @INNTBL_02 WHERE RECNO = @M_AMT02 )
END
IF @M_AMT01 <= 20 AND @M_AMT01 <> 0 BEGIN
SET @WORD1 = ( SELECT @WORD1 + MTEXT +’ ‘ FROM @INNTBL_01 WHERE RECNO = @M_AMT01 )
END
SET @WORD = @WORD + @WORD1 + ‘ THOUSAND ‘
END
IF @AMOUNT < 1000 AND @AMOUNT > = 100 BEGIN
SET @M_AMT01 = @AMOUNT
SET @AMOUNT = ( @AMOUNT % 100 )
SET @M_AMT01 = ( @M_AMT01 – @AMOUNT ) / 100
SET @WORD = ( SELECT @WORD + ‘ ‘ +MTEXT + ‘ HUNDRED ‘ FROM @INNTBL_01 WHERE RECNO = @M_AMT01

)
END
IF @AMOUNT < 100 AND @AMOUNT > 20 BEGIN
SET @M_AMT01 = @AMOUNT
SET @AMOUNT = ( @AMOUNT % 10 )
SET @M_AMT01 = ( @M_AMT01 – @AMOUNT ) / 10
SET @WORD = ( SELECT @WORD + MTEXT + ‘ ‘ FROM @INNTBL_02 WHERE RECNO = @M_AMT01 )
END
IF @AMOUNT <= 20 AND @AMOUNT >= 1 BEGIN
SET @WORD = ( SELECT @WORD + MTEXT +’ ‘ FROM @INNTBL_01 WHERE RECNO = @AMOUNT )
END

–STEP 4:
–Calculate the paise also.
DECLARE @WORDP VARCHAR(300)
SET @WORDP = ”
IF @PAISE <> 0 BEGIN
IF @PAISE < 100 AND @PAISE > 20 BEGIN
DECLARE @PAISE_01 VARCHAR(300)
SET @PAISE_01 = @PAISE
SET @PAISE = ( @PAISE % 10 )
SET @PAISE_01 = ( @PAISE_01 – @PAISE ) / 10
SET @WORDP = ( SELECT @WORDP + MTEXT FROM @INNTBL_02 WHERE RECNO = @PAISE_01 )
END
IF @PAISE <= 20 AND @PAISE >= 1 BEGIN
SET @WORDP = ( SELECT @WORDP + MTEXT FROM @INNTBL_01 WHERE RECNO = @PAISE )
END
SET @WORD = @WORD + ‘AND ‘ + @WORDP + ‘ PAISE’
END

–STEP 5:
–Time to return answer from the function.

RETURN (REPLACE(@WORD, ‘  ‘, ‘ ‘))

–Its done dude. Try by yourself.
END

Now when you need to use this function, simply call it inline to any select statement.

CALLING THE FUNCTION

Select dbo.AMOUNT_IN_WORDS(IM.GRAND_TOTAL, 0) AS words from TBL_INVOICE_MAIN IM

Here, it will convert GRAND_TOTAL field from the table TBL_INVOICE_MAIN.

or more simply,

Select dbo.AMOUNT_IN_WORDS(654321, 0)

Isn’t it simple enough !

Amount in words, SQL

Create Word Document with Hyperlinks in it with C# code

Feb 18th

Posted by Ashish Sehajpal in Development

No comments

First of all you need to add reference to COM library in project.
Add reference to Microsoft word 11.0 word library under COM reference tab Add this in the code behind “using” section

using Microsoft.Office.Interop.Word;
private void CreateWordDocument()
{
// create annonymous object to be referenced throughout
object oMissingReference = System.Reflection.Missing.Value;
object Visible=true;
// define arbitrary starting point for defining range object.
object oStartRange = 0;
object oEndRange = 0;
// create instance of application class
ApplicationClass WordApp = new ApplicationClass();
// create document instance
Document docObject = WordApp.Documents.Add(ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference );
// select a range to add some text/image there
Range rng=docObject.Range(ref oStartRange,ref oMissingReference );
rng.Font.Name=”Arial”;
rng.InsertAfter(”Ashish Sehajpal”);
// create Hyperlink object and assign address, screen tip to it
Object address = @”http://www.sehajpal.com”;
Object screenTip = “Welcome to sehajpal.com”;
// adding Hyperlink to the text “Ashish Sehajpal“
rng.Hyperlinks.Add(rng, ref address, ref oMissingReference , ref screenTip, ref oMissingReference , ref oMissingReference );
// give file name to it so as to save the document
object filename = @”C:\createdbycsharp.doc”;
docObject.SaveAs(ref filename, ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference ,ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference , ref oMissingReference );

WordApp.Visible=true;
}

C#, Create Word Document

Unable to debug the silverlight project

Feb 18th

Posted by Ashish Sehajpal in Development

No comments

I have some strange thing happening in my application. I was able to debug anything but silverlight code !!
I did so much of RnD on net and tried various tricks, but only this thing worked : -

When you add a Silverlight project to a asp.net solution, you create 2 projects…
A. the startup project (interface starter)
B. the interface itself

1. right click on the startup project (SilverlightWeb — the one with default.aspx in it )…
2. click on ‘Property Pages’
3. open ‘Start Options’ Tab
4. enable Silverlight Debugger
5. OK

This was the solution for me

SilverLight, Unable to debug

Versioning problem in reference.cs in WCF service consumption in Silverlight 2

Feb 18th

Posted by Ashish Sehajpal in Development

2 comments

A starnge problem occured when I was trying to add reference to one of my WCF service in my silverlight project. When deeply researched I found that some of the data contracts have been references twice in the reference.cs i.e. some classes with version 3.0.0.0 and some with 2.0.0.0 version. Strange!!

A quick work around was to generate the reference via svcutil.exe and it worked well in case where the project was justa relay service and not the silverlight application. In silverlght app, it throws the error for async and completed methods. Then I used SLSvcutil.exe i.e. svcutil for silverlight which is bundled with silverlight 3 only. I used this and generated the refences.cs for me. One problem left is FAULTCONTRACTS in the code. That i removed manually and it is working now.

SilverLight, Versioning Problem

How to detect resolution of client in WPF?

Feb 18th

Posted by Ashish Sehajpal in Development

No comments

I was wondering about how to detect scrren resolution for client’s machine in wpf. I searched internet, tried googling but in vein.

Here is the snippet that can help the folks looking for similar solution.

// Detect screen height available. Remember it is the inner area of screen.
SystemParameters.FullPrimaryScreenWidth

// Detect screen width available. Remember it is the inner area of screen.
SystemParameters.FullPrimaryScreenHeight;

Detect Resolution, WPF

How to open popup or redirect to web page in SilverLight

Feb 16th

Posted by Ashish Sehajpal in Development

No comments

This is the code to open a popup window in SilverLight.

To open a website in a popup using SilverLight, we need to call Window.Navigate method. This method can be used in two different ways i.e. simply navigating to that webpage or using the window option [_blank, _self] etc.

// Navigate to the webpage itself
System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(”http://www.microsoft.com”));

//or if you want to open a popup (in a separate window)
System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(”http://www.microsoft.com”), “_blank”);

Navigation, SilverLight

How to open popup in wpf?

Feb 16th

Posted by Ashish Sehajpal in Development

1 comment

This is the code to open a popup window in wpf.

We need to create an empty window for popup and we’ll assign the object of the WPF Window to it. In this example we wre trying to open PopupForm.xaml as popup.

private void OpenPopup()
{
   var oPopupForm = new ucPopupFormPopup();

  // create POPUP window
  var oWindow = new Window();

  //setting up its starting position
  oWindow.WindowStartupLocation =WindowStartupLocation.CenterScreen;

  // we can make it available or not in taskbar
  oWindow.ShowInTaskbar =false;

  oWindow.Width = 630;
  oWindow.Height = 500;
  oWindow.ResizeMode =ResizeMode.NoResize;
  oWindow.Title =”Ashish Sehajpal – Trying WPF”;

  // Access Popup’s Controls
  oPopupForm.TextBox1.Text = “Ashish Sehajpal is here.”;

  // Assign Form to Popup window
  oWindow.Content = oPopupForm;

  // If you need to do some action on popup’s closing
  // Assignt the event handler to it
  oWindow.Closing += oWindow_Closing;
  // it’s time to show POPUP
  oWindow.ShowDialog();

}

  // eventhandler for the popup closeing event
private void oWindow_Closing(object sender, CancelEventArgs e)
{
  // do something nice here
}

How to open Popup, WPF

Calling constructor inside constructor in C#

Feb 16th

Posted by Ashish Sehajpal in Development

No comments

To call one C# constructor from another, before the body of the constructor, use either:

: base (parameters)

to call a constructor in the base class; or:

: this (parameters)

to call a constructor in this class.

The following examples illustrate how to call one constructor from another.

public class myPopUp
{
   public myPopUp(int width, int height, myPopUp parentPopUp) : this (width, height)
   {
       this.ParentPopUp = parentPopUp;
   }
}

C#, Constructors

SARS – Annual tax calculation sheet

Feb 16th

Posted by Ashish Sehajpal in Tax Calculations

No comments

A complete reference to the Sotuh African annual tax calculations for the year 2010 can be downloaded by clicking here. In it you will find the exact amount of tax payable on the income occured in RSA, rand by rand. So easy. Enjoy.

Annual Tax in SA, SARS
«12345»
  • Welcome to Sehajpal.com



  • Popular Posts

    3 idiots 9/11 About World Amount in words Annual Tax in SA Bind with Enum C# Chords Constructors Create Word Document Cricket Detect Resolution Dot Net Economical Crisis FIFA 2010 First Blog Post foeticide Forbes List Guitar Healthy Food Hindi songs How to open Popup Indians Init Parameters Java Job Openings Letter to shahrukh marriage Microsoft Kin Mobile Mumbai Navigation Pentagonization Rediff shopping Sachin Tendulkar SARS SilverLight SQL SRK Sunshine Terrorists Unable to debug Versioning Problem Weight Loss WPF Zensar

    WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.

  • My latest tweets

    Loading tweets...
    Follow me on Twitter!
  • My Blog Readers

previous next
    • Categories

      • Development (11)
      • General (23)
      • Jobs (3)
      • Tax Calculations (1)
      • Uncategorized (11)
    • Blogroll

      • Development Blog
      • Documentation
      • Plugins
      • Suggest Ideas
      • Support Forum
      • Themes
      • WordPress Planet
    • Monthly Archives

      • September 2010 (2)
      • August 2010 (5)
      • July 2010 (6)
      • June 2010 (3)
      • April 2010 (5)
      • March 2010 (5)
      • February 2010 (20)
      • January 2010 (2)
    • Recently Published

      • Web service software factory in visual studio 2010
      • Diet according to blood type
      • Dark coffee ? Hmm.. good for health
    • Unsensored Comments

      • enuppysapGuen on Web service software factory in visual studio 2010
      • enuppysapGuen on Web service software factory in visual studio 2010
      • Tweets that mention Diet according to blood type | Sehajpal.com -- Topsy.com on Diet according to blood type
      • Sumit Gupta on About Ashish
      • Ashish Sehajpal on Dot net openings at Microsoft, Hyderabad.
    • Impact Calendar

      September 2010
      M T W T F S S
      « Aug    
       12345
      6789101112
      13141516171819
      20212223242526
      27282930  
Copyright © 2010 Sehajpal.com
RSS Feeds XHTML 1.1 CSS 3.0 Top