Pages

Sunday, May 16, 2010

Useful Regular Expression

For Alpha numeric :-

^[a-zA-Z0-9_]+$

Examples:-

ROOT, ROOT_One

For Numeric with Comma:-

^\$?(\d{1,3},?(\d{3},?)*\d{3}(.\d{0,3})?|\d{1,3}(.\d{2})?)$

Examples:-

1,111,11  

Numeric with 3 Digits(Compulsory with 0’s) :-

^[0-9]{3}$

Examples:-

000-999 integers where your has to enter three digits

001,003,343,999….

 

You can check any regular expression in the following URL it is a web based regular expression checker. Very useful.

http://www.rubular.com/

Thursday, April 8, 2010

Setting Foucs on a Textbox when the Silverlight page loads

 

I found this on silverlight.net, and was able to get it to work for me by adding a call to System.Windows.Browser.HtmlPage.Plugin.Focus() prior to calling RegularTextBox.Focus():

 

private void UserControl_Loaded(object sender, RoutedEventArgs e)
   {       
      System.Windows.Browser.HtmlPage.Plugin.Focus();
      RegularTextBox.Focus();
   }

Sunday, April 4, 2010

How to create a Dynamic LINQ Query Programmatically

 

Source :- http://blog.bvsoftware.com/post/2008/02/27/How-to-create-a-Dynamic-LINQ-Query-Programmatically.aspx


Creating a LINQ query in challenging because we don't know ahead of time which fields the user will complete. They are all optional and without anything selected we want to pull back all tickets. Normally, you'd write a query like this:

var ticket = (from t in db.cerberus_Tickets

where t.id == id

select t).Single();

In the example above we know that the t.id parameter will always be given. So how do you create a query in code when you don't know what fields to include in the WHERE clause ahead of time?

The first key is understanding the LINQ queries are not executed until they are used to enumerate through a collection. This part is key because it means we can create a query and change it in code as long as we don't try to look at the results first.

What we're going to do is create an IQueryable collection that contains all of our Ticket objects and we'll dynamically add our WHERE clause information.  Then we'll create a normal LINQ query that selects all of the matches from our IQueryable collection and handles paging. Because we don't actually enumerate the IQueryable collection that contains all our tickets, it won't actually pull back all of the tickets (which would take forever!). Instead, it will be "merged" with our normally LINQ query at run time when we enumerate over it.

1) Create our LINQ to SQL context objects

List<cerberus_Ticket> result = new List<cerberus_Ticket>();

cerberusDataContext db = new cerberusDataContext(connectionString);

2) Create an empty IQueryable collection containing all tickets. Note that this query doesn't actually select everything from the database yet. If it did this would take forever and effectively be filtering the database table in memory. That would not be a good design!

IQueryable<cerberus_Ticket> matches = db.cerberus_Tickets;

3) Add our WHERE clause information with custom logic to decide if the clauses should be added or not

 if (this.AgentIdField.Text.Trim().Length > 0)

{

     matches = matches.Where(a => a.AgentId == criteria.AgentId);

}

if (this.TicketIdField.Text.Trim().Length > 0)

{

     matches = matches.Where(a => a.TicketId.Contains(criteria.TicketId));

}

4) Create a second LINQ query that selects from the first one to sort and page the results.

// calculate start row based on page parameters passed in

int startRow = (pageNumber - 1) * pageSize;

var output = (from p in matches

orderby p.DateCreated descending

select p).Skip(startRow).Take(pageSize).ToList();

Again, I can't emphasize enough how cool it is that LINQ doesn't query the database until we call the ToList() at the end of the second statement. This delay in execution is the magic that lets us create dynamic queries on the fly.

Wednesday, March 31, 2010

?? Operator (C# Reference)

The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

Remarks


A nullable type can contain a value, or it can be undefined. The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error. If you use a cast, and the nullable value type is currently undefined, an InvalidOperationException exception will be thrown.

For more information, see Nullable Types (C# Programming Guide).

The result of a ?? operator is not considered to be a constant even if both its arguments are constants.

class NullCoalesce
{
static int? GetNullableInt()
{
return null;
}

static string GetStringValue()
{
return null;
}

static void Main()
{
// ?? operator example.
int? x = null;

// y = x, unless x is null, in which case y = -1.
int y = x ?? -1;

// Assign i to return value of method, unless
// return value is null, in which case assign
// default value of int to i.
int i = GetNullableInt() ?? default(int);

string s = GetStringValue();
// ?? also works with reference types.
// Display contents of s, unless s is null,
// in which case display "Unspecified".
Console.WriteLine(s ?? "Unspecified");
}
}

Tuesday, February 23, 2010

Silverlight Value Converters

 

From :- http://timheuer.com/blog/archive/2008/07/30/format-data-in-silverlight-databinding-valueconverter.aspx

 

Here i just want to update only the code portion of it

<ListBox x:Name="FeedList">
ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock FontFamily="Arial" FontWeight="Bold" Foreground="Red" Text="{Binding Title.Text}" />
<TextBlock FontFamily="Arial" Text="{Binding PublishDate}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>


 



Assume this XAML binds to SyndicationFeed.Items data coming from an RSS feed.  If we simply bind it the result would be this:





 public class Formatter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}





 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter != null)
{
string formatterString = parameter.ToString();

if (!string.IsNullOrEmpty(formatterString))
{
return string.Format(culture, formatterString, value);
}
}

return value.ToString();
}


xmlns:converter="clr-namespace:TypeConverter_CS"


Finally the code look like this



 



1: <UserControl x:Class="TypeConverter_CS.Page"


   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 


   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"


   4:     xmlns:converter="clr-namespace:TypeConverter_CS"


   5:     Width="400" Height="300">


   6:     <UserControl.Resources>


   7:         <

converter:Formatter x:Key="FormatConverter" />


   8:     </UserControl.Resources>


   9:     <Grid x:Name="LayoutRoot" Background="White">


  10:         <ListBox x:Name="FeedList">


  11:             <ListBox.ItemTemplate>


  12:                 <DataTemplate>


  13:                     <StackPanel Orientation="Vertical">


  14:                         <TextBlock FontFamily="Arial" FontWeight="Bold" Foreground="Red" Text="{Binding Title.Text}" />


  15:                         <TextBlock FontFamily="Arial" Text="{Binding PublishDate, Converter={StaticResource FormatConverter}, ConverterParameter=\{0:M\}}" />


  16:                     </StackPanel>


  17:                 </DataTemplate>


  18:             </ListBox.ItemTemplate>


  19:         </ListBox>


  20:     </Grid>


  21: </UserControl>


Output