Click here to Skip to main content
15,891,423 members
Home / Discussions / C#
   

C#

 
QuestionGuaging article interest about MEF before I start writing Pin
-james6-Oct-13 13:24
professional-james6-Oct-13 13:24 
AnswerRe: Guaging article interest about MEF before I start writing Pin
BillWoodruff6-Oct-13 14:26
professionalBillWoodruff6-Oct-13 14:26 
GeneralRe: Guaging article interest about MEF before I start writing Pin
-james6-Oct-13 15:30
professional-james6-Oct-13 15:30 
AnswerRe: Guaging article interest about MEF before I start writing Pin
Brisingr Aerowing6-Oct-13 14:35
professionalBrisingr Aerowing6-Oct-13 14:35 
GeneralRe: Guaging article interest about MEF before I start writing Pin
-james6-Oct-13 15:36
professional-james6-Oct-13 15:36 
AnswerRe: Guaging article interest about MEF before I start writing Pin
Mycroft Holmes6-Oct-13 16:54
professionalMycroft Holmes6-Oct-13 16:54 
AnswerRe: Guaging article interest about MEF before I start writing Pin
Pete O'Hanlon7-Oct-13 0:17
mvePete O'Hanlon7-Oct-13 0:17 
Questiondictionary/list help needed Pin
yoni.kess6-Oct-13 4:54
yoni.kess6-Oct-13 4:54 
Hi All,
XML
I need your assitance with the following:
i have created a small application that is taking information from a DB, puting it in a datagridview and then will create a KML file.
i have add support on the datagridview from a previous post and now i am stuck with the following:

basically the sql query will give you the following:

State        Name        Long        Lat
Day        AZ1        11.11111        22.2222
Day        AZ1        11.22222        22.3333
Day        AZ1        11.33333        22.4444
Day        AZ1        11.44444        22.5555
Day        AZ2        11.11111        22.2222
Day         AZ2         22.22222       33.3333
Day         AZ2         22.33333        33.4444
Day         AZ2         22.44444        33.5555
Night        AZ1        11.11111        22.2222
Night         AZ1         11.22222        22.3333
Night         AZ1         11.33333        22.4444
Night         AZ1         11.44444        22.5555
Night        AZ5        55.11111        66.2222
Night         AZ5         55.22222        66.3333
Night         AZ5         55.33333        66.4444
Night         AZ5         55.44444        66.5555

The output of the creatkml should be a kml file ( i know how to create it, just have issues with putting the above correctly)


<Folder>
<name>Day</name>
 <Placemark>
  <name>AZ1</name>
  <styleUrl>#m_ylw-pushpin</styleUrl>
  <Polygon>
   <tessellate>1</tessellate>
   <outerBoundaryIs>
    <LinearRing>
     <coordinates>
11.11111,22.2222,0 11.22222,22.3333,01 1.33333,22.4444,0 11.44444,22.5555,0
     </coordinates>
    </LinearRing>
   </outerBoundaryIs>
  </Polygon>
 </Placemark>
 <Placemark>
  <name>AZ2</name>
  < styleUrl>#m_ylw-pushpin</styleUrl>
  < Polygon>
   < tessellate>1</tessellate>
   < outerBoundaryIs>
    < LinearRing>
     < coordinates>
11.11111,22.2222,0 22.22222,33.3333,0 22.33333,33.4444,0  22.44444,33.5555
     < /coordinates>
    < /LinearRing>
   </outerBoundaryIs>
  < /Polygon>
 </Placemark>
</Folder>
<Folder>
<name>Night</name>
...
.
.
..... basically the same as the above placemarks just with the night names




when clicking on the createKML button the application stops with the following error in the phase of :
LatLongCollectionByName.Add(matchHandle, lst);

ERROR MESSAGE "An item with the same key has already been added."

the attached code is suppose to collect all the names and the points (without the state value. this is something new i added).

i have uploaded the code and a small sample of the DB

hope i am clear

thanks alot
Jonathan

<pre lang="c#">
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;
using System.IO;
using System.Diagnostics;
using Microsoft.SqlServer.Management.Smo;
using System.Linq;

namespace WisToKML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DisconnectBtn.Enabled = false;
            KMLDestinationTB.Enabled = false;
            DBConBtn.Enabled = false;
            userTB.Text = "sa";
            PassTB.Text = "Password!";
            //FromDate.CustomFormat = "2013-06-01";
            //FromDate.Format = DateTimePickerFormat.Custom;
            //TODate.CustomFormat = "2013-06-01";
            //TODate.Format = DateTimePickerFormat.Custom;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            BrowseBtn.Enabled = false;

            DataTable dt = SmoApplication.EnumAvailableSqlServers(true);
            DBSrvTB.ValueMember = "Name";
            DBSrvTB.DataSource = dt;
 
        }

        private void DBSrvTB_SelectedIndexChanged(object sender, EventArgs e)
        {
            DBNameTB.Items.Clear();
            if (DBSrvTB.SelectedIndex != -1)
            {
                string serverName = DBSrvTB.SelectedValue.ToString();
                Server server = new Server(serverName);
                try
                {
                    foreach (Database database in server.Databases)
                    {
                        DBNameTB.Items.Add(database.Name);
                    }
                }
                catch (Exception ex)
                {
                    string exception = ex.Message;
                }
            }
        }


        private void DBConBtn_Click(object sender, EventArgs e)
        {
            if ((DBNameTB.Text == null) || (DBNameTB.Text == ""))
            {
                MessageBox.Show("Database Name is missing", "Error");
                return;
            }
            else
            {
                try
                {
                    string username = userTB.Text;
                    string password = PassTB.Text;
                    string server = DBSrvTB.Text;
                    string database = DBNameTB.Text;
                    string ConnectionString = "User ID=" + username + ";" + "Password=" + password + ";" + "Database=" + database + ";" + "server=" + server; 
                    SqlConnection connection = new SqlConnection(ConnectionString);
                    connection.Open();
                    // MessageBox.Show("DB Connected","(Connected)");
                    DBConBtn.Enabled = false;
                    DBConBtn.Text = "DB Connected";
                    DBConBtn.BackColor = Color.Green;
                    BrowseBtn.Enabled = true;
                    DisconnectBtn.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("DB Authentication Failed", "Error");
                    return;
                }
            }
        }

        private void DisconnectBtn_Click(object sender, EventArgs e)
        {
            string username = userTB.Text;
            string password = PassTB.Text;
            string server = DBSrvTB.Text;
            string database = DBNameTB.Text;
            string ConnectionString = "User ID=" + username + ";" + "Password=" + password + ";" + "Database=" + database + ";" + "server=" + server; 
            SqlConnection connection = new SqlConnection(ConnectionString);
            connection.Close();
            DBConBtn.Enabled = true;
            DBConBtn.BackColor = default(Color);
            DisconnectBtn.Enabled = false;
            BrowseBtn.Enabled = false;
        }



        #region Class LatLongPair
        class LatLongPair
        {
            public double  Lat { get;set; }
            public double  Lon { get;set; }
        }
        #endregion


        private void button3_Click(object sender, EventArgs e)
        {


            #region testing new code from web

            // this is a new code from the web

            Dictionary<string, List<LatLongPair>> LatLongCollectionByName = new Dictionary<string, List<LatLongPair>>();

            List<LatLongPair> lst = null;
            LatLongPair lPair = null;
            string matchHandle = string.Empty;

            for (int i = 0; i < LayersGrid.Rows.Count; i++)
            {
                if (LayersGrid.Rows[i].Cells[1].Value.ToString() == matchHandle)
                {
                    lPair.Lat = Convert.ToDouble(LayersGrid.Rows[i].Cells[3].Value);
                    lPair.Lon = Convert.ToDouble(LayersGrid.Rows[i].Cells[2].Value);
                    lst.Add(lPair);
                }
                else
                {
                    matchHandle = LayersGrid.Rows[i].Cells[1].Value.ToString();
                    lPair = new LatLongPair();
                    lPair.Lat = Convert.ToDouble(LayersGrid.Rows[i].Cells[3].Value);
                    lPair.Lon = Convert.ToDouble(LayersGrid.Rows[i].Cells[2].Value);

                    lst = new List<LatLongPair>();
                    lst.Add(lPair);

                        LatLongCollectionByName.Add(matchHandle, lst);

                }

                foreach (KeyValuePair<string, List<LatLongPair>> item in LatLongCollectionByName)
                {
                    Console.WriteLine("\n For name : " + item.Key + ", lat/long vaues are:\n");

                    foreach (LatLongPair lp in item.Value)
                    {
                        Console.Write("\t" + lp.Lat + ",\t" + lp.Lon + " | ");
                    }
                }







                //end of new code from web

            #endregion




                #region KML Header & Footer
                string kmlheader = "<?xml version=" + "\"" + "1.0" + "\"" + " encoding=" + "\"" + "UTF-8" + "\"" + "?>" + "\r\n";
                string kmlheader1 = "<kml xmlns=" + "\"" + "http://www.opengis.net/kml/2.2" + "\"" + " xmlns:gx=" + "\"" + "http://www.google.com/kml/ext/2.2" + "\"" + " xmlns:kml=" + "\"" + "http://www.opengis.net/kml/2.2" + "\"" + " xmlns:atom=" + "\"" + "http://www.w3.org/2005/Atom" + "\"" + ">" + "\r\n" + "<Document>" + "\r\n";
                string kmlfooter = "</Document>" + "\r\n" + "</kml>";
                #endregion

                #region KML Document Name
                string kmldocname = "\t" + "<name>" + sitenameTB.Text + "</name>" + "\r\n";
                #endregion

                #region KML Style
                string stylemap = "\t" + "<StyleMap id=" + "\"" + "msn_polygon" + "\"" + ">" + "\r\n" + "\t" + "\t" + "<pair>" + "\r\n" + "\t" + "\t" + "\t" + "<key>normal</key>" + "\r\n" + "\t" + "\t" + "\t" + "<styleUrl>#sn_polygon</styleUrl>" + "\r\n" + "\t" + "\t" + "</pair>" + "\r\n" + "\t" + "\t" + "<pair>" + "\r\n" + "\t" + "\t" + "\t" + "<key>highlight</key>" + "\r\n" + "\t" + "\t" + "\t" + "<styleUrl>#sh_polygon</styleUrl>" + "\r\n" + "\t" + "\t" + "</pair>" + "\r\n" + "\t" + "</StyleMap>" + "\r\n";
                string styleid_highlight = "\t" + "<Style id=" + "\"" + "sh_polygon" + "\"" + ">" + "\r\n" + "\t" + "\t" + "<IconStyle>" + "\r\n" + "\t" + "\t" + "\t" + "<color>ff00ff55</color>" + "\r\n" + "\t" + "\t" + "\t" + "<scale>0.709091</scale>" + "\r\n" + "\t" + "\t" + "\t" + "<Icon>" + "\r\n" + "\t" + "\t" + "\t" + "\t" + "<href>http://maps.google.com/mapfiles/kml/shapes/polygon.png</href>" + "\r\n" + "\t" + "\t" + "\t" + "</Icon>" + "\r\n" + "\t" + "\t" + "</IconStyle>" + "\r\n" + "\t" + "\t" + "<LabelStyle>" + "\r\n" + "\t" + "\t" + "\t" + "<scale>0.6</scale>" + "\r\n" + "\t" + "\t" + "</LabelStyle>" + "\r\n" + "\t" + "\t" + "<ListStyle></ListStyle>" + "\r\n" + "\t" + "</Style>" + "\r\n";
                string styleid_normal = "\t" + "<Style id=" + "\"" + "sn_polygon" + "\"" + ">" + "\r\n" + "\t" + "\t" + "<IconStyle>" + "\r\n" + "\t" + "\t" + "\t" + "<color>ff00ff55</color>" + "\r\n" + "\t" + "\t" + "\t" + "<scale>0.709091</scale>" + "\r\n" + "\t" + "\t" + "\t" + "<Icon>" + "\r\n" + "\t" + "\t" + "\t" + "\t" + "<href>http://maps.google.com/mapfiles/kml/shapes/polygon.png</href>" + "\r\n" + "\t" + "\t" + "\t" + "</Icon>" + "\r\n" + "\t" + "\t" + "</IconStyle>" + "\r\n" + "\t" + "\t" + "<LabelStyle>" + "\r\n" + "\t" + "\t" + "\t" + "<scale>0.6</scale>" + "\r\n" + "\t" + "\t" + "</LabelStyle>" + "\r\n" + "\t" + "\t" + "<ListStyle></ListStyle>" + "\r\n" + "\t" + "</Style>" + "\r\n";
                #endregion

                #region kml file writer definition
                StreamWriter KMLwriter = new System.IO.StreamWriter(KMLDestinationTB.Text + "\\" + sitenameTB.Text + ".kml", false);
                #endregion

                #region kml writer
                KMLwriter.WriteLine(kmlheader + kmlheader1 + kmldocname + stylemap + styleid_highlight + styleid_normal);
                //KMLwriter.Close();



                KMLwriter.WriteLine("\t" + "<Placemark>" + "\r\n");
                //          KMLwriter.WriteLine("\t" + "\t" + "<name>" + LayersGrid.Rows.Cells["Target ID"].Value + "</name>" + "\r\n");
                KMLwriter.WriteLine("\t" + "\t" + "<styleUrl>#msn_polygon</styleUrl>" + "\r\n");
                KMLwriter.WriteLine("\t" + "\t" + "<Polygon>" + "\r\n");
                KMLwriter.WriteLine("\t" + "\t" + "<tessellate>1</tessellate>" + "\r\n");
                KMLwriter.WriteLine("\t" + "\t" + "\t" + "<outerBoundaryIs>" + "\r\n");
                KMLwriter.WriteLine("\t" + "\t" + "\t" + "<LinearRing>" + "\r\n");
                KMLwriter.WriteLine("\t" + "\t" + "\t" + "<coordinates>" + "\r\n");
                #endregion




                #region this is old code
                //this is the old code
                /*
                                for (int i = 1; i < LayersGrid.Rows.Count; i++)
                    {

                        if (LayersGrid.Rows[i].Cells["Name"] != null)
                        {
                            if (LayersGrid.Rows[i].Cells["Name"].Value.ToString() == LayersGrid.Rows[i - 1].Cells["Name"].Value.ToString())
                            {
                                // DO XXXXXXXXXXX
                            }
                            else
                            {
                                // DO YYYYYYYYYY
                                //       MessageBox.Show(LayersGrid.Rows[i].Cells.ToString(), "different names");

                                try
                                {

                                    KMLwriter.WriteLine("\t" + "\t" + "\t" + LayersGrid.Rows[i].Cells["Longitude"].Value + "," + LayersGrid.Rows[i].Cells["Latitude"].Value + ",0" + "\r\n");
                                }

                                catch (NullReferenceException ex)
                                {
                                }
                            }
                        }
                    }
                 */
                // end this is the old code

                #endregion

                #region KML Writer

                KMLwriter.WriteLine("\t" + "\t" + "\t" + "</coordinates>" + "\r\n");
                KMLwriter.WriteLine("\t" + "\t" + "\t" + "</outerBoundaryIs>" + "\r\n");
                KMLwriter.WriteLine("\t" + "\t" + "</Polygon>" + "\r\n");
                KMLwriter.WriteLine("\t" + "</Placemark>" + "\r\n");
                KMLwriter.WriteLine(kmlfooter);
                KMLwriter.Close();

                #endregion

                #region oldcode
                //foreach (DataGridViewRow row in LayersGrid.Rows)
                //{
                //    #region writing KML for each row
                //    //if (row.Cells["Name"].Value != null)
                //    //{
                //    //    KMLwriter.WriteLine("\t" + "<Placemark>" + "\r\n");
                //    //    KMLwriter.WriteLine("\t" + "\t" + "<name>" + row.Cells["Name"].Value + "</name>" + "\r\n");
                //    //    KMLwriter.WriteLine("\t" + "\t" + "<LookAt>" + "\r\n" + "\t" + "\t" + "<longitude>" + row.Cells["Longitude"].Value + "</longitude>" + "\r\n" + "\t" + "\t" + "<latitude>" + row.Cells["Latitude"].Value + "</latitude>" + "\r\n" + "\t" + "\t" + "<altitude>0</altitude>" + "\r\n" + "\t" + "\t" + "<heading>1</heading>" + "\r\n" + "\t" + "\t" + "<tilt>0</tilt>" + "\r\n" + "\t" + "\t" + "<range>5000</range>" + "\r\n" + "\t" + "\t" + "<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>" + "\r\n" + "\t" + "</LookAt>" + "\r\n");
                //    //    KMLwriter.WriteLine("\t" + "\t" + "<styleUrl>#msn_polygon</styleUrl>");
                //    //    KMLwriter.WriteLine("\t" + "\t" + "<Point>" + "\r\n" + "\t" + "\t" + "\t" + "<coordinates>" + row.Cells["Longitude"].Value + "," + row.Cells["Latitude"].Value + ",0" + "</coordinates>" + "\r\n" + "\t" + "\t" + "</Point>" + "\r\n");
                //    //    KMLwriter.WriteLine("\t" + "</Placemark>" + "\r\n");
                //    //}
                //    #endregion


                //    //***************** testing comparinson ***********************

                //    #region bool function

                //    bool functionname;
                //{
                //     if (LayersGrid.RowCount == 0)
                //        {
                //         // return false;  // or true? Your call 
                //            MessageBox.Show("empty table");
                //        }
                //        // Get cell value in first row
                //     object oldValue = LayersGrid.Rows[0].Cells[0].Value;

                //        for (int i = 1; i < LayersGrid.RowCount; ++i)
                //            {
                //                 if (LayersGrid.Rows[i].Cells[0].Value != oldValue)
                //                        {
                //                             //  return false; // they are some differences
                //                             MessageBox.Show("there are different layers");
                //                        }
                //            }
                //                // All cells are the same as the one in first row
                //                // return true;
                //                   MessageBox.Show("only one layer");
                //} //closing bool function

                //#endregion

                //} //close for loop

                #endregion


            }
        }

        private void DBNameTB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DBNameTB.Text != null)
            {
                DBConBtn.Enabled = true;
            }
        }

        private void LyrLoadData_Click(object sender, EventArgs e)
        {
            string username = userTB.Text;
            string password = PassTB.Text;
            string server = DBSrvTB.Text;
            string database = DBNameTB.Text;
            string ConnectionString = "User ID=" + username + ";" + "Password=" + password + ";" + "Database=" + database + ";" + "server=" + server;
            string ignore = "select * from Layers";

            MessageBox.Show(ignore);

            //testing new connection

            // String reportQuery = @"  complicated query returning many rows    ";
            SqlConnection ReportConnect = new SqlConnection(ConnectionString);
            ReportConnect.Open();
            SqlCommand command = new SqlCommand(ignore, ReportConnect);
            command.CommandTimeout = 300; //5 mins
            DataSet tempDataset = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            da.Fill(ds, "Layers");
            //     connection.Open();
            LayersGrid.DataSource = ds;
            LayersGrid.DataMember = "Layers";
            //
        }


<pre lang="c#">




}
}

thanks
Jonathan
AnswerRe: dictionary/list help needed Pin
Richard MacCutchan6-Oct-13 5:09
mveRichard MacCutchan6-Oct-13 5:09 
GeneralRe: dictionary/list help needed Pin
yoni.kess6-Oct-13 5:20
yoni.kess6-Oct-13 5:20 
GeneralRe: dictionary/list help needed Pin
Richard MacCutchan6-Oct-13 5:45
mveRichard MacCutchan6-Oct-13 5:45 
GeneralRe: dictionary/list help needed Pin
yoni.kess6-Oct-13 5:51
yoni.kess6-Oct-13 5:51 
GeneralRe: dictionary/list help needed Pin
Richard MacCutchan6-Oct-13 6:22
mveRichard MacCutchan6-Oct-13 6:22 
GeneralRe: dictionary/list help needed Pin
yoni.kess6-Oct-13 10:10
yoni.kess6-Oct-13 10:10 
GeneralRe: dictionary/list help needed Pin
Richard MacCutchan6-Oct-13 20:44
mveRichard MacCutchan6-Oct-13 20:44 
AnswerRe: dictionary/list help needed Pin
BillWoodruff6-Oct-13 16:58
professionalBillWoodruff6-Oct-13 16:58 
GeneralRe: dictionary/list help needed Pin
yoni.kess6-Oct-13 21:39
yoni.kess6-Oct-13 21:39 
GeneralRe: dictionary/list help needed Pin
BillWoodruff7-Oct-13 0:39
professionalBillWoodruff7-Oct-13 0:39 
QuestionEntity Framework: Primary key violation Pin
Lutosław6-Oct-13 2:55
Lutosław6-Oct-13 2:55 
AnswerRe: Entity Framework: Primary key violation Pin
Dave Kreskowiak6-Oct-13 6:28
mveDave Kreskowiak6-Oct-13 6:28 
GeneralRe: Entity Framework: Primary key violation Pin
Lutosław6-Oct-13 9:50
Lutosław6-Oct-13 9:50 
Questionupdate ConnectionString in App.config Pin
Jassim Rahma6-Oct-13 0:03
Jassim Rahma6-Oct-13 0:03 
AnswerRe: update ConnectionString in App.config Pin
OriginalGriff6-Oct-13 0:10
mveOriginalGriff6-Oct-13 0:10 
QuestionSimple cross threading question Pin
ve3tru5-Oct-13 17:55
ve3tru5-Oct-13 17:55 
AnswerRe: Simple cross threading question Pin
Abhinav S5-Oct-13 19:00
Abhinav S5-Oct-13 19:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.