Friday, December 30, 2011

Code for Zoom to GIS Layer

Team,

Here is the code to Zoom to a specific GIS Layer.

This code scans through all features and zooms to the extent of all.

Particularly this line of code "envelopeCls.Union(featureExtent);" collects the envelope extents of each record.

Have fun coding!

Cheers
Anand




private bool ZoomtoLayer(string m_zoomtoLayerName)
{
bool togoBool = false;
for (int layerIndex = 0; layerIndex < ArcMap.Document.ActiveView.FocusMap.LayerCount; layerIndex++)
{
ILayer2 eachLayer = ArcMap.Document.ActiveView.FocusMap.get_Layer(layerIndex) as ILayer2;
if (eachLayer is IFeatureLayer)
{
IFeatureLayer eachFeatureLayer = eachLayer as IFeatureLayer;
if (eachFeatureLayer.Name.Trim().ToUpper() == m_zoomtoLayerName.Trim().ToUpper())
{
IFeatureLayerDefinition featureLayerDef = eachFeatureLayer as IFeatureLayerDefinition;
QueryFilterClass queryFilter = new QueryFilterClass();
queryFilter.WhereClause = featureLayerDef.DefinitionExpression;
ESRI.ArcGIS.Geodatabase.IFeatureCursor featureCursor = eachFeatureLayer.FeatureClass.Search(queryFilter, true);
ESRI.ArcGIS.Geometry.IEnvelope envelopeCls = new EnvelopeClass();
envelopeCls.SetEmpty();
ESRI.ArcGIS.Geodatabase.IFeature feature;
while ((feature = featureCursor.NextFeature()) != null)
{
ESRI.ArcGIS.Geometry.IGeometry geometry = feature.Shape;
ESRI.ArcGIS.Geometry.IEnvelope featureExtent = geometry.Envelope;
envelopeCls.Union(featureExtent);
}
envelopeCls.Expand(m_zoomFactor, m_zoomFactor, true);
ArcMap.Document.ActiveView.Extent = envelopeCls.Envelope;
ArcMap.Document.ActiveView.Refresh();
return true;
} // if (eachFeatureLayer.Name.Trim().ToUpper() == gisLayerName.Trim().ToUpper())
} // if (eachLayer is IFeatureLayer)
} // for
return togoBool;
}

Wednesday, November 30, 2011

Escaping XML

I found out about a neat function which can escape the XML content.

InnerXml = System.Security.SecurityElement.Escape(stringtoescape);
               
A simple inbuilt class like this can save you some time in trying to write your own function.


Tooltip in Windows Form Listbox Items

There was a request to provide tooltip for the items in the Listbox control.

The key idea is, selecting the listbox Index from the system point.

Here is the code.

Happy coding !

    private void listBoxControl_MouseMove(object sender, MouseEventArgs e)
    {
        if (this.listBoxControl.SelectedIndex == -1)
        {
            TurnOffTooltip();
            return;
        }
        if (sender is ListBox)
        {
            int selectedIndex = this.listBoxControl.SelectedIndex;
            ListBox listBox = (ListBox)sender;
            System.Drawing.Point point = new System.Drawing.Point(e.X, e.Y);
            int hoverIndex = listBox.IndexFromPoint(point);
            if (hoverIndex >= 0 && hoverIndex < listBox.Items.Count && hoverIndex == selectedIndex)
            {
                TurnOnTooltip();
            }
            else
            {
                TurnOffTooltip();
            }
        }
    }
 
    private void listBoxControl_MouseLeave(object sender, EventArgs e)
    {
        TurnOffTooltip();
    }
 
    private void TurnOffTooltip()
    {
        this.toolTip.SetToolTip(this.listBoxControl, "");
        this.toolTip.Hide(this.listBoxControl);
        return;
    }
 
    private void TurnOnTooltip()
    {
        SomeClass someClassObject = (SomeClass)this.listBoxControl.SelectedItem;
        DisplayTooltip(someClassObject);
    }
 
    private void DisplayTooltip(SomeClass someClassObject)
    {
        string toDisplay = "Overlay Item : " + someClassObject.m_ILAOverlayItem.Name;
        toDisplay = toDisplay + "\n" + "Impact Analysis Config : " + someClassObject.m_ILAimpactLayerConfig.ILConfigName;
        toDisplay = toDisplay + "\n" + "GIS Layer : " + someClassObject.m_ILAimpactLayerConfig.GetGISLayerName();
        toDisplay = toDisplay + "\n" + "Overlay Item : " + someClassObject.m_ILAOverlayItem.Name;
        toDisplay = toDisplay + "\n" + "Inside Status Code : " + someClassObject.m_ILAInsideStatusCode[0].ToString();
        toDisplay = toDisplay + "\n" + "Is Published : " + someClassObject.m_IsPublished.ToString();
        this.toolTip.SetToolTip(this.listBoxControl, toDisplay);
        this.toolTip.Show(toDisplay, this.listBoxControl);
    }
 
    private void listBoxControl_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.listBoxControl.SelectedItem != null)
        {
            SomeClass someClassObject = (SomeClass)this.listBoxControl.SelectedItem;
            TurnOnTooltip();
        }
    }




ArcIMS Error 1722 during installation


When installing ArcIMS, I got the following error.


After some research I found the following solution

1. Goto Control Panel-> Services
2. Stop All ArcIMS /ArcGIS Services.
3. Stop Tomcat
4. Restart IIS.

Now install the ArcIMS Application /Service pack and the Error will not Repeat again.

Hope this saves some time for you.

Sunday, October 16, 2011

ASP.NET Master Page and Other Pages allignment Issues

I was creating a website with ASP.NET master page and other content pages.

For some reason when I was navigating from one page to another there was a shift in the footer. I had a horizontal line above the footer and this was jumping few pixels up and down when navigating through the pages.

After some research, I found out that this was happening because some of the pages had the paragraph (<p> </p>) tags and this was pushing the content a little bit.

I resolved this by adding a proper margin and padding to the div body in the master page itself.

Hope this tip can help you !

Sunday, September 25, 2011

Class Deserializing issue with a class containing ArrayList as member variable


In one of my program, I had a class declared with a Member Variable whose datatype is ArrayList.

I serialized and saved the class contents in the text file.

When I tried to deserialize, I got back only the first value in the ArrayList, not all the values. Even though the file had more than 1 value, always I was getting back only one value into my class member variable.

After several debugging I thought of adding a constructor to the class and also inside that class constructor I initialized the ArrayList member variable to new ArrayList. After doing this then the deserialization happened properly. Then I got all my values back into my ArrayList member variable.

Whenever deserialization occurs the class constructor is called and proper initialization happens and then the ArrayList is being filled.

Hope this tip saves time for someone !


Wednesday, August 24, 2011

Creating hotspots on image

Team,

I was asked to create a simple web page with a static image and hotspots (hyperlinks which will navigate to other pages) on some of the image features.

Did some digging work and was able to write this code.

Here is the working link.

The link above contains two visible hotspots and one invisible hotspot.

Key points are :

1. The main DIV tag must contain the background image with style position relative.

2. The inner hyperlinks must contain style position absolute and you can place those hyperlinks wherever you want on the image.

I have added the source code below.

Happy coding !

Cheers
Anand

================

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
    <title>Hotspot on Image</title>
</head>
<body style="text-align: center;">
    <div id="divMain" style="background-image: url('sampleImage.png'); width: 528px;
        height: 396px; position: relative;">
        <a id="birdyahoo" href="
http://www.yahoo.com" target="_blank" style="border: medium solid Red;
            width: 46px; height: 75px; position: absolute; left: 186px; top: 81px;" title="Click here to visit Yahoo Website">
        </a><a id="birdGoogle" href="
http://www.google.com" target="_blank" style="border: medium solid Red;
            width: 92px; height: 64px; position: absolute; left: 184px; top: 255px;" title="Click here to visit Google Website">
        </a><a id="birdMicrosoft" href="
http://www.microsoft.com" target="_blank" style="width: 69px; height: 38px; position: absolute; left: 347px; top: 162px;" title="Click here to visit Microsoft Website">
        </a>
    </div>
</body>
</html>


================

Sunday, July 24, 2011

Send E-Mail using ASP.NET

Team, I got involved in a project where we need to send e-mail from ASP.NET web application.

Tried many techniques and got all these errors .


"Mailbox unavailable. The server response was: DY-001 (SNT0-MC4-F22) Unfortunately, messages from 76.175.219.250 weren't sent. Please contact your Internet service provider. You can tell them that Hotmail does not relay dynamically-assigned IP ranges. You can also refer your provider to http://mail.live.com/mail/troubleshooting.aspx#errors."
 "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first"

"Mailbox unavailable. The server response was: 5.7.3 Requested action aborted; user not authenticated"
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."

After few research here is the final working code.

public static bool SendMessage()
{
            bool togoValue = false;
            try
            {
                MailMessage mm = new MailMessage();
                mm.Sender = new MailAddress("fromEmailID@hotmail.com");
                mm.From = new MailAddress("fromEmailID@hotmail.com ");
                mm.To.Add(new MailAddress( "toEmailID@hotmail.com "));
                mm.Subject = "This is test Subject";
                mm.Body = "This is test Body";
                SmtpClient client = new SmtpClient("smtp.live.com");
                client.Credentials = new NetworkCredential("fromEmailID@hotmail.com", "password");
                client.Port = 25;
                client.EnableSsl = true;
                client.Send(mm);
                togoValue = true;
            }
            catch (Exception ex)
            {
                throw new Exception("Error when sending mail." + ex.ToString());
            }
            return togoValue;
 }

Replace your values in the right location and give it a try.

I tried both GMail server (smtp.gmail.com) and Hotmail Server (smtp.live.com) and both are working fine.

Have fun coding !

Tuesday, June 7, 2011

ListBox Control DisplayMember Property Issue

My goal was to add a ListBox Control and add Class Objects Items to it. The Display Values should be one of the class member variables.

I have declared a string variable  (m_Name) which is public (It's basically a "Field" of the class) and then I also assigned the ListBoxControl.DisplayMember = "m_Name" ;

This was not working and the ListBox Control was always showing the Class.ToString() value and not my expected class member value.

After few Googling,  I found out that it should be a "Property" of the class not the "Field".

Once I created a "Property" and assigned it to the ListBoxControl.DisplayMember then it started to display the correct value.

Hope this saves some time.

Happy Coding !

Monday, May 23, 2011

Wrap Text in Silverlight Datagrid Columns

The old code shows how the text column was bound to a Key-Value pair. But the problem was if the value is larger, then it will exceed the 190 pixels width.

To resolve the problem, I have to use a "CellTemplate" and "DataTemplate" as shown in the new code. The "TextWrapping" element is the key.

Happy Coding.



OLD CODE:
<data:DataGrid.Columns>
<data:DataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold" Width="120" />
<data:DataGridTextColumn Binding="{Binding Path=Value}" Width="190" />
</data:DataGrid.Columns>

NEW CODE:
<data:DataGrid.Columns>
<data:DataGridTemplateColumn>
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Key}" FontWeight="Bold" Width="118" TextWrapping="Wrap" Margin="2,2,2,2" ></TextBlock>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTemplateColumn>
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value}" FontWeight="Normal" Width="188" TextWrapping="Wrap" Margin="2,2,2,2" ></TextBlock>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>

Friday, April 22, 2011

Passing SQL String as Query String in Web Applications

I was working on a web page where I had to pass a SQL Query string to another page as query parameter.

When I finished the coding and tested, it was working for some queries and failing for certain. After a close examination I found out it was failing for "LIKE" queries. The query had characters like '%'

For example if the query is like "SELECT FNAME LIKE '%ANA%'" then this will fail. Because the receiving page was unable to pass the query string properly.

After some research I found out there is a javascript function encodeURI() which will encode the query string.

There is also another function decodeURI() which does opposite of encodeURI().

By using these functions I was able to complete the coding.



Visual Studio 2010 Compilation Error

While working with Visual Studio 2010 in Silverlight coding I got this error message.

"Unable to attach to application. The version of clr.dll in the target does not match the one mscordacwks.dll was built for.Do you want to continue anyway ?"

I tried cleaning up the solution and rebuilding still did not fix anything.

Finally after some research I found out that this error happens when the Windows Updates is on progress.

I waited till Windows updates are complete and then restarted the laptop.

Then recompiled and it worked.

Hope this helps.

Here is the snapshot of the error image.

Monday, March 21, 2011

Embed Video in Silverlight

Friends,
Here is the code snippet to embed a Video File in Silverlight.

I tried it with .wmv file and it worked fine.

Hope it helps someone !

Cheers
Anand


XAML File Content

        <Border x:Name="BorderVideo" BorderThickness="5" BorderBrush="Blue"  CornerRadius="25" Canvas.ZIndex="1" Width="670" Height="550"  >
            <StackPanel Orientation="Vertical">
                <MediaElement x:Name="ChurchSong"
                      Width="640"
                      Height="480"
                      AutoPlay="False"
                      Source="MVI_0957_WMV V9.wmv" Canvas.ZIndex="0" >
                </MediaElement>
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
                    <Button x:Name="buttonStopandRewind" Content="Stop and Rewind" Width="100" Margin="10,10,10,10" Click="buttonStopandRewind_Click" />
                    <Button x:Name="buttonPause" Content="Pause" Width="100" Margin="10,10,10,10" Click="buttonPause_Click" />
                    <Button x:Name="buttonPlay" Content="Play" Width="100" Margin="10,10,10,10" Click="buttonPlay_Click" />
                    <Button x:Name="buttonSkip" Content="Skip 5 Secs" Width="100" Margin="10,10,10,10" Click="buttonSkip_Click" />
                </StackPanel>
            </StackPanel>
        </Border>

.cs File Content
      private void buttonStopandRewind_Click(object sender, RoutedEventArgs e)
        {
            this.ChurchSong.Stop();
        }

        private void buttonPause_Click(object sender, RoutedEventArgs e)
        {
            this.ChurchSong.Pause();
        }

        private void buttonPlay_Click(object sender, RoutedEventArgs e)
        {
            this.ChurchSong.Play();
        }

        private void buttonSkip_Click(object sender, RoutedEventArgs e)
        {
            this.ChurchSong.Position = this.ChurchSong.Position.Add(new TimeSpan(0, 0, 5));
        }

All Blogs so far ...