by JBrooks
16. December 2011 09:54
I found this code on the internet to create a CheckBox in a DataGrid that wouldn’t require more than one click to change. But it didn’t work for me.
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<CheckBox IsThreeState="False" IsChecked="{Binding Path=IsActive, Mode=TwoWay}"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
The reason it didn’t work is because I also had this as part of my DataGrid
<sdk:DataGrid CurrentCellChanged="dgTotals_CurrentCellChanged" …
That method had a dgTotals.BeginEdit(); in it to allow the user to begin editing the DataGrid cells without having to first click on them. The simple solution was to just skip that for my CheckBox column.
if(dgTotals.CurrentColumn != null && dgTotals.CurrentColumn.DisplayIndex != 1)
dgTotals.BeginEdit();
by JBrooks
6. December 2011 11:41
The first thing I do when troubleshooting a Silverlight startup error (like where it just has the startup progress bar sitting at 100% forever) is to make sure I can reach the RIA Service and it doesn’t return an error.
This example shows how to decode the URL back to the service:
http://localhost:1893/MWEntry5-Web-Services-ScheduleDomainService.svc
This is just the path delimited with dashes to the service as seen in your project and also with all of the periods changed to dashes and then just add “.svc” at the end.

You should see a page for the RIA Service if you put this URL in a browser.
When calling a method it is ClientBin first, followed by the name space, followed by “binary” and then your method name. Here are 2 examples:
http://localhost:1893/ClientBin/MWEntry5-Web-Services-ScheduleDomainService.svc/binary/GetSchedulableEntities
http://MyDomain/MyWebApp/ClientBin/MyWebApp-Web-AuthenticationService.svc/binary/GetUser
I just needed to put this somewhere so I can reference it in the future.
by jbrooks
10. November 2011 09:09
In Excel 34.5 will round to 35 and in .NET it will round to 34. This is because .NET defaults to using the MidpointRounding.ToEven mode – also called banker’s rounding. In .NET you have the option of overriding this by passing in the other MidpointRounding like:
Math.Round(34.5, MidpointRounding.AwayFromZero); // Now rounds to 35
A little annoying since in all of the applications that I’ve ever written the users expect rounding to match Excel’s and I’ve never needed the default. Worse, in Silverlight you don’t even have the option to change the default when using Math.Round().
So I had to write my own extension that can be used like:
double myValue = 34.5;
int myRoundedValue = (int) myValue.RoundCorrect(0); // rounds to 35
Here is the extension code:
public static class DoubleExtensions
{
public static double RoundCorrect(this double d, int decimals)
{
double multiplier = Math.Pow(10, decimals);
if (d < 0)
multiplier *= -1;
return Math.Floor((d * multiplier) + 0.5) / multiplier;
}
}
or if you want the code for a normal method it would be
public double RoundCorrect(double d, int decimals)
{
double multiplier = Math.Pow(10, decimals);
if (d < 0)
multiplier *= -1;
return Math.Floor((d * multiplier) + 0.5) / multiplier;
}
by jbrooks
9. November 2011 07:24
On one project we keep our connection string as an AppSetting (See: HERE). So that is what I use to build my RIA Services connection string to account for the differences between Dev, Test, QA and Prod. You could also do the same with a regularly stored connection sting. Here is how we did it:
public partial class MyDomainService
{
protected override MyEntities CreateObjectContext()
{
string ConnString = Common.cApp.AppSettings["DefaultConnection"];
string seConn = ConfigurationManager.ConnectionStrings["MyEntities"].ToString();
EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder(seConn);
ecsb.ProviderConnectionString = ConnString;
EntityConnection ec = new EntityConnection(ecsb.ToString());
ScheduleEntities ctx = new ScheduleEntities(ec);
return ctx;
}
}
53793326-9fee-447e-8b15-7387cbb98345|0|.0
Tags:
Silverlight
by jbrooks
2. November 2011 09:36
I introduced a Silverlight project to my ASP.Net application and every thing worked fine in development but when I went to build it with out automatic build process MSBuild would kick out the following error:
[msbuild] App.xaml.cs(2,17): error CS0234: The type or namespace name 'Services' does not exist in the namespace 'MyProject.Web' (are you missing an assembly reference?)
Had to change a registration key, the DefaultToolsVersion was set to 2.0, changed it to 4.0
Ran the install selecting repair for the Silverlight 5.0 sdk (silverlight5_sdk.exe) – it was originally installed as part of the tool kit, but found others saying the real install still needed to be done to get it to work with MSBuild.
Next go the error: error MSB4044: The "CreateRiaClientFilesTask" task was not given a value for the required parameter "ClientFrameworkPath"
Had to install EntityFramWork4.1 on my build server and then reboot.
Now on to the next problem.
by jbrooks
19. August 2011 07:46
We have the requirement that our passwords have to change every 90 days. I wanted to automate this and it sounded pretty easy to do, but it wasn’t that easy. Why? Because there are a lot of rules for passwords.
First, I have to store it in the web.config. So no XML special characters.
- quot "
- amp &
- apos '
- lt <
- gt >
Next, If used in an OLE DB or ODBC connection string, a password must not contain the following characters: [] {}() , ; ? * ! @.
Finally, strong passwords must contains characters from at least three of the following categories:
- English uppercase characters (A through Z)
- English lowercase characters (a through z)
- Base 10 digits (0 through 9)
- Nonalphabetic characters (for example: !, $, #, %)
So keeping all of these rules in mind I created a simple class. that I can just call
public class PasswordGenerator
{
private static string CHARS_LCASE = "abcdefgijkmnopqrstwxyz";
private static string CHARS_UCASE = "ABCDEFGHJKLMNPQRSTWXYZ";
private static string CHARS_NUMERIC = "23456789";
private static string CHARS_SPECIAL = "*-+_%/";
private static string CHARS_ALL = CHARS_LCASE + CHARS_UCASE + CHARS_NUMERIC + CHARS_SPECIAL;
public static string GeneratePassword(int length)
{
char[] chars = new char[length];
Random rand = new Random();
for (int i = 0; i < length; i++)
{
switch (i)
{
case 0:
chars[i] = CHARS_LCASE[rand.Next(0, CHARS_LCASE.Length)];
break;
case 1:
chars[i] = CHARS_UCASE[rand.Next(0, CHARS_UCASE.Length)];
break;
case 2:
chars[i] = CHARS_NUMERIC[rand.Next(0, CHARS_NUMERIC.Length)];
break;
case 3:
chars[i] = CHARS_SPECIAL[rand.Next(0, CHARS_SPECIAL.Length)];
break;
default:
chars[i] = CHARS_ALL[rand.Next(0, CHARS_ALL.Length)];
break;
}
}
return new string(chars);
}
}
So now I just simply call this for a new password:
PasswordGenerator.GeneratePassword(13)
93ce90f9-3673-4248-bc57-f332d6731d25|0|.0
Tags:
by JBrooks
6. May 2011 07:13
Some things in Silverlight seem harder than they should be. Binding a list of strings to a combobox is one such thing. This involves the following steps to do a simple bind.
1. Have a private list of the strings in your page code.
private List<string> FuelList = new List<string>()
{
"OIL",
"GAS",
"COAL"
};
2. In your initialization method add the list as a resource so your XAML can use it. This must be done before the InitializeComponent(); call.
public ucMyUserControl()
{
this.Resources.Add("FuelList", FuelList);
InitializeComponent();
}
3. Then you can reference the list as the source to you combo box.
<sdk:DataGrid AutoGenerateColumns="False" MinHeight="130" Name="dgMyDataGrid"
ItemsSource="{Binding Path=ScheduleRows, Mode=TwoWay}"
CurrentCellChanged="dgMyDataGrid_CurrentCellChanged">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="Fuel Type">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="2" VerticalAlignment="Center"
HorizontalAlignment="Left" Text="{Binding FUEL_TYPE}" Width="120" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Height="23" Name="cbxFuelType"
ItemsSource="{StaticResource FuelList}"
SelectedValue="{Binding Path=FUEL_TYPE, Mode=TwoWay}" Width="120">
</ComboBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
...
4. One last item, notice the datagrid’s current cell changed event is wired up to start editing without requiring the user to click first. That method is simple enough:
private void dgMyDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
dgMyDataGrid.BeginEdit();
}
That gives me my datagrid with my combo box:

Here is a sample solution that has the full code:
ComboBoxTest.zip (1,016.17 kb)
by JBrooks
8. April 2011 07:45
The Silverlight TextBox does have a Watermark property, but Microsoft says “Do not use in a Silverlight 4 application” HERE. So I looked into making one myself and it didn’t take much to do it.
I just wanted the text box below to show the text “Search” in a grayed out way.

To do this it is just setting the forground color.
<TextBox Name="tbxSearch" Width="120" Text="Search" Foreground="#5A000000"
VerticalAlignment="Center" GotFocus="tbxSearch_GotFocus" />
I also wanted to reset the text to the normal color and to clear the text box when the user gave it focus. Simple enough. If you notice above, the GotFocus event is wired up to the following method:
private void tbxSearch_GotFocus(object sender, RoutedEventArgs e)
{
if (((SolidColorBrush)tbxSearch.Foreground).Color != Colors.Black)
{
tbxSearch.Text = "";
((SolidColorBrush)tbxSearch.Foreground).Color = Colors.Black;
}
}
So that is it except that this should be made into a user control.
4db72071-1d11-4338-9ed5-63a4cde65f6c|1|4.0
Tags:
Silverlight
by jbrooks
15. March 2011 11:19
All of these failed except the last one, I was logged in as RMUser:
exec sp_password 'DC9E3622', 'A55BE7!#4', 'RMUser'
Msg 15210, Level 16, State 1, Procedure sp_password, Line 20
Only members of the sysadmin role can use the loginame option. The password was not changed.
ALTER LOGIN RMUser WITH PASSWORD ='A55BE7!#4'
Incorrect syntax near 'LOGIN'.
exec sp_password @old = 'DC9E3622', @new = 'A55BE7!#4'
Command(s) completed successfully.
ad01b1b5-6df7-45f2-90b5-5fcfcdb25509|0|.0
Tags:
by JBrooks
7. January 2011 14:24
I have 10 clients that each have a process that posts a value to my web service every hour at three minutes after the hour. I needed to know when a post doesn’t happen for a client.
The logic of the SQL is that I want to return the last row posted for each Client ID when that last row’s post time is older than 60 minutes (plus 2 minutes for variations). And I only care if it has happened in the last 4 days. This is the SQL:
declare @intervalMinutes int,
@variationMinutes int,
@activeDays int
set @intervalMinutes = 60
set @variationMinutes = 2
set @activeDays = 4
select 'For Client '+cast(clientId as varchar)+
' Last Posting is too old: '+
convert(varchar(5),p.postTime,1)+ ' '+convert(varchar(5),p.postTime,108)
as Message,
'Last status was: '+
status as Details
from Postings p
where p.postTime =
(select max(pIn.postTime)
from Postings pIn
where pIn.clientId = p.clientId)
and p.postTime between
dateadd(day,-@activeDays,getdate())
and dateadd(mi,-@intervalMinutes - @variationMinutes,getdate())
order by p.postTime
So this could return a table that looks like:

I now only get notified if there last posting is more than 62 minutes old. I previously blogged about how to have rows like these show up in an RSS Feed HERE. The good thing about an RSS Feed is that you can have it on your phone, Outlook and web browser.
3bba906b-bd8a-4b87-981e-8ffb1790e5ee|0|.0
Tags: