Monday, July 28, 2014

Connect to remote SQL Server over the Internet


I installed SQL Server 2008 R2 edition on a Virtual Machine (VM). I was supposed to access this SQL Server from my laptop over the internet.

I created the .udl file on my laptop and tried to access this remote SQL Server in the VM. It was not connecting. Tried many things but still unable to connect to the remote SQL Server on the VM.

(A quick note on the UDL file. Create a simple text document and change the extension to .udl. Double-click the UDL file. This will provide tools to establish a connection to any database based on the drivers available on the laptop. We can establish the connection to a remote server. If you open the .udl file in a notepad you can see the complete connection string. This connection string can be used in any application to communicate with the database.)

After checking few internet pages here is what I found. There are two items to check and fix.

Item 1:

In the SQL Server Configuration Manager, check the “Protocols”. The “TCP/IP” protocol should be enabled and make sure the IP address uses the port 1433. This is the default SQL port.

Item 2:

Check the Windows Firewall Settings. Add an “Inbound Rule” to allow communication through the port 1433.

After fixing these two items, I was able to communicate to my remote SQL Serve from my laptop over the Internet.

Hope this tip helps you.

Sunday, June 22, 2014

Creating Dynamic Radio Button Controls in Silverlight MVVM model

There was a need to create dynamic "Radio Button" controls in one of my projects. This was a Silverlight MVVM project.

Since the "View" is isolated from "ViewModel" and connected only through binding, it was not clear on how to do this task.

After few research I found the answer.

This is the code that needs to go on the XAML side. Note the "ItemsControl" element. The itemssource will take in a List of RadioButtonClass.

<Grid DataContext="{StaticResource ViewModel}">
        <StackPanel>
            <ItemsControl ItemsSource="{Binding RadioButtonClassList}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <RadioButton GroupName="OneGroup" IsChecked="{Binding IsSelected}" Content="{Binding RadioButtonName}"></RadioButton>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>
 </Grid>


Note the binding properties (IsSelected and RadioButtonName) in the "RadioButton" control.

The "RadioButtonClass" can be defined something like this.

public class RadioButtonClass : INotifyPropertyChanged
{
   public string RadioButtonName;
   public bool IsSelected;
}

In the "ViewModel" class if you add a property which is a List like this List<RadioButtonClass> and bind it to the View as shown in the above XAML snippet (<Grid DataContext="{StaticResource ViewModel}">) then the RadioButtons will show up. The number or radio buttons is equal to the number of items in the List.

Hope this tip helps.

Happy Coding !


 

Monday, May 19, 2014

ESRI Legend Control - Show and Hide Sliders

In the ESRI Legend Control, "DataTemplate" can be used to display slider "usercontrol" to control the opacity of the Layer.
 
Here is the XAML content where the slider "usercontrol" is used.
 
      
<esri:Legend Grid.Row="1" x:Name="legendControl" VerticalAlignment="Top" LayerItemsMode="Tree"
ShowOnlyVisibleLayers="False" Margin="5,5,5,5" ><esri:Legend.MapLayerTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding Label}"
IsChecked="{Binding IsEnabled, Mode=TwoWay}"
IsEnabled="{Binding IsInScaleRange}" >
</CheckBox>
<Slider Maximum="1" Value="{Binding Layer.Opacity, Mode=TwoWay}" Width="50" Loaded="Slider_Loaded" Tag="{Binding Layer}" />
</StackPanel>
</DataTemplate>
</esri:Legend.MapLayerTemplate>
<esri:Legend.LayerTemplate>
<DataTemplate>
<CheckBox Content="{Binding Label}"
IsChecked="{Binding IsEnabled, Mode=TwoWay}"
IsEnabled="{Binding IsInScaleRange}" >
</CheckBox>
</DataTemplate>
</esri:Legend.LayerTemplate>
</esri:Legend>


But what happens, if the user should see Slider usercontrol only for certain Layers and for certain Layers it should NOT be shown ?

There are many ways to do this. But here is one simple method.

In the Slider "usercontrol" add the following event and tag in the XAML content.

Loaded="Slider_Loaded" Tag="{Binding Layer}"

In the code behind simply use some logic like this.

private void Slider_Loaded(object sender, RoutedEventArgs e)
{Slider slider = sender as Slider;
Layer layer = slider.Tag as Layer;
if (layer.ID == "SampleData") slider.Visibility = System.Windows.Visibility.Collapsed;
}

If the "Layer.ID" matches with that "specific layer name", then the slider will not be displayed.

Hope this helps !

Have fun coding :)

Cheers
Adam





 

Saturday, April 19, 2014

Get color from HTML color string

Here is a quick code snippet to get color from HTML color string.

private Color GetColorFromString(string inStringColorCode)
{

Color togoColor = Colors.Transparent;
try
{

byte alpha = Convert.ToByte(Convert.ToInt32(inStringColorCode.Substring(0, 2), 16));

byte red = Convert.ToByte(Convert.ToInt32(inStringColorCode.Substring(2, 2), 16));

byte green = Convert.ToByte(Convert.ToInt32(inStringColorCode.Substring(4, 2), 16));

byte blue = Convert.ToByte(Convert.ToInt32(inStringColorCode.Substring(6, 2), 16));

togoColor = Color.FromArgb(alpha, red, green, blue);

}

catch (Exception ex)
{

MessageBox.Show("Error when converting color ARGB code " + inStringColorCode + ". It must be of length 8 characters." + ex.ToString(), "Error", MessageBoxButton.OK);
}
return togoColor;
}

Happy Coding !

Sunday, March 23, 2014

Using Events and Delegates in Visual C#.NET


Here are the steps to create events and delegates in .NET.
 
STEP 1:

In the Child Control which raises the event, you should have the following:

public delegate void ZoomtoMapEventHandler(object sender, ClassQueryResultsSet CQRe);

public event ZoomtoMapEventHandler ZoomtoMapEvent;

where the ClassQueryResultsSet is as follows :

public class ClassQueryResultsSet : EventArgs
                {
                                public DataSet QueryResultsDataSet ;
                                public string QueryResultsTableName ;
                                public string KeyFieldName ;
                                public ClassQueryResultsSet()
                                {
                                                //
                                                // TODO: Add constructor logic here
                                                //
                                                QueryResultsDataSet = null ;
                                                QueryResultsTableName = null ;
                                                KeyFieldName = "" ;
                                }
                }

Also when invoking the event check for null and do it as shown below:

                                                                if (ZoomtoMapEvent != null)
                                                                {
                                                                                ZoomtoMapEvent(this, Res) ;
                                                                }
                                                                else
                                                                {
                                                                                MessageBox.Show("ZoomtoMapEvent is Null !") ;
                                                                }

STEP 2

In the MAIN PARENT Program, the event is declared like below

x.userControlQuerySingleTable1.ZoomtoMapEvent += new WindowsControlLibrarySingleTable.UserControlQuerySingleTable.ZoomtoMapEventHandler(x.ShowMaptoUser)  ;

 
Also the function into which the control should come in should have the same signature as shown below:

public void ShowMaptoUser(object o, ClassQueryResultsSet e)
{
                MessageBox.Show("Calling ok" + e.QueryResultsTableName) ;
}

Using this events and delegates will come in handy when passing information from one class to another.

Happy coding.

Cheers
Adam

Monday, February 24, 2014

Simple Slideshow using timer, javascript and JQuery


Here is the code to create a simple slideshow using timer, JavaScript and JQuery.

The html file contents should be like this which has all the images to show up.

<div class="galleryImages">
<img src='Images/Slides/0.png' alt="" />
<img src='Images/Slides/1.png' alt="" />
<img src='Images/Slides/2.png' alt="" />
<img src='Images/Slides/3.png' alt="" />
</div>
 
The 'galleryImages' class is defined like this.

.galleryImages {
position: relative;
height: 350px;
width:700px;
overflow: hidden;
border:solid;
border-color:black;
border-width:thin;
-moz-border-radius: 15px;
-webkit-border-radius: 15px;
-khtml-border-radius: 15px;
border-radius: 15px;
margin:10px;
}
 
.galleryImages img {
position: absolute;
top: 0;
left: 0;
}


The JavaScript code should be like this.

var slideShowTimer;
var imageId = 0;
var galleryImages;
var transitionSpeed = 500;

function PerformInitialization() {
SetupTimer();galleryImages = $('.galleryImages img');
}
 
function SetupTimer() {
slideShowTimer = setInterval(ChangePhoto, 3000);
}
 
function ChangePhoto() {
galleryImages.eq(imageId).stop(true, true).animate({ opacity: 0 }, transitionSpeed);
imageId++;if (imageId >= galleryImages.length) {
imageId = 0;
}
galleryImages.eq(imageId).stop(true, false).animate({ opacity: 1 }, transitionSpeed);
}

Make sure to call PerformInitialization() function during "onload" of the body of the html file.

Also make sure to add these "JQuery" references.

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.min.js"></script>
   
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/flick/jquery-ui.css" rel="stylesheet" type="text/css" />

Have fun. Happy coding.

Cheers
Adam

All Blogs so far ...