Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to this so here goes. I have a table that has latitude and longitude degree coordinates and I'm trying to do an SQL UPDATE in the third column and convert the latitude and longitude coordinates into MGRS in the MGRS column.

I have a table called tblMGRSAdded and in it, it looks like this.
Latitude  Longitude  MGRS

37.51       -85.24

 37.19       -93.55

44.07       -93.79


I'm able to do the conversion in an OleDbDataReader and here's the code for that.

C#
using System.Data.OleDb;
using System.Data;
using GeoUtility.GeoSystem;
 
OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Project\local.mdb");
thisConnection.Open(); 
OleDbCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT * from tblMGRSAdded";
OleDbDataReader thisReader = thisCommand.ExecuteReader();
       while (thisReader.Read())
            {
                Geographic geoF = new Geographic(Convert.ToDouble(thisReader["longitude"]), Convert.ToDouble(thisReader["latitude"]));
                MGRS mgrs = (MGRS)geoF;
                string mgrsStr = mgrs.Zone + mgrs.Band + " " + mgrs.Grid + " " +
                       mgrs.EastString.Substring(0, 4) + " " + mgrs.NorthString.Substring(0, 4);
                Console.WriteLine("\t{0}\t{1}\t{2}", thisReader["latitude"], thisReader["longitude"], mgrsStr);
            }
thisReader.Close();
thisConnection.Close();
Console.ReadKey();


I get a result that looks like this.
Latitude  Longitude  MGRS

37.51      -85.24        16S FG 5555 5290

37.19      -93.55        15S VB 5118 1609

44.07      -93.79        15T VJ 3673 7995


I realize from tonnes of reading that you can't use an update statement from an OleDbDateReader. That's fine but then how do I do the update then for the conversion.

I'm using something called GeoUtility.dll from http://geoutility.codeplex.com/. It's a great little tool and things would be much better if I can only figure this out.

I'm not going to attempt to put down some of the code examples that I've tried but I do know this. I have to open the connection and I have to somehow use a SQL UPDATE statement but from my example how would my UPDATE statement look using mgrsStr like up above to make the conversion and update the column MGRS in the table??

Any help would be much appreciated. I've been working on this none stop and right now I can't believe something so small has now put my job in jeopardy.

Thanks

MR
Posted
Updated 7-Oct-11 18:22pm
v2

1 solution

I was able to update without any issues

I am not sure whether the latitude and the Longitude values you are passing is correct (37.51,-85.24??) . getting a system exception on your values saying its NOT VALID.

I tried with latitude=8.12345, Longitude=50.56789 and worked very well and the database is also updated, Please see the code below i have used

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb");
                thisConnection.Open();
                OleDbCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = "SELECT * from tblMGRSAdded";
                OleDbDataReader thisReader = thisCommand.ExecuteReader();
                while (thisReader.Read())
                {
                    Geographic geoF = new Geographic(Convert.ToDouble(thisReader["longitude"]), Convert.ToDouble(thisReader["latitude"]));
                    MGRS mgrs = (MGRS)geoF;
                    

                    string mgrsStr = mgrs.Zone + mgrs.Band + " " + mgrs.Grid + " " +
                           mgrs.EastString.Substring(0, 4) + " " + mgrs.NorthString.Substring(0, 4);

                    OleDbCommand thisUpdateCommand = thisConnection.CreateCommand();
                    thisUpdateCommand.CommandText = "update tblMGRSAdded set MGRS='" + mgrsStr + "' where ID=" + thisReader["Id"].ToString();
                    thisUpdateCommand.ExecuteNonQuery();  
                    MessageBox.Show(mgrsStr);                     
                }
                thisReader.Close();
                thisConnection.Close();

                MessageBox.Show("OK"); 
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);  
            }
        }



Console version of the solution - UPDATE 2
static void Main(string[] args)
      {
          try
          {
              OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb");
              thisConnection.Open();
              OleDbCommand thisCommand = thisConnection.CreateCommand();
              thisCommand.CommandText = "SELECT * from tblMGRSAdded";
              OleDbDataReader thisReader = thisCommand.ExecuteReader();
              while (thisReader.Read())
              {
                  Geographic geoF = new Geographic(Convert.ToDouble(thisReader["longitude"]), Convert.ToDouble(thisReader["latitude"]));
                  MGRS mgrs = (MGRS)geoF;
                  string mgrsStr = mgrs.Zone + mgrs.Band + " " + mgrs.Grid + " " +
                         mgrs.EastString.Substring(0, 4) + " " + mgrs.NorthString.Substring(0, 4);
                  Console.WriteLine("\t{0}\t{1}\t{2}", thisReader["latitude"], thisReader["longitude"], mgrsStr);

                  //UPDATE ROUTINE
                  using (OleDbCommand thisUpdateCommand = thisConnection.CreateCommand())
                  {

                      thisUpdateCommand.CommandText = "update tblMGRSAdded set MGRS='" + mgrsStr + "' where ID=" + thisReader["Id"].ToString();
                      thisUpdateCommand.ExecuteNonQuery();
                  }

                 // Console.WriteLine("Database updated for this record!!....Press any key to continue");
                 // Console.ReadKey();
              }
              thisReader.Close();
              thisConnection.Close();
              Console.WriteLine("COMPLETED!!!!!....Press any key to exit");
              Console.ReadKey();
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
              Console.WriteLine("ERROR!!!!!....Press any key to exit");
              Console.ReadKey();
          }
      }
 
Share this answer
 
v3
Comments
mzrax 8-Oct-11 13:25pm    
Hello Bala

I think you're close. I've tried a couple of things and your code shows all the conversions in the code that says MessageBox.Show(mgrsStr). They all show up there one at a time. It's where I was except I did it a little different. The update is still not working.

Here's my table in Access tblMGRSAdded. Latitude and Longitude are numbers field size double and the MGRS column is text.

Latitude Longitude MGRS
34.15908051 39.00210953
30.13175774 26.67066956
13.70171642 78.88327789
-25.840868 126.9972839
-21.63534737 126.1775589
45.65699768 -66.35175323
48.68639755 -85.02711487
51.3950386 -73.58667755
46.2272377 -74.29947662
42.03953934 -89.89198303

I haven't been able to run your code to update the MGRS column.

Any help would be much appreciated.

Thanks
Bala Selvanayagam 8-Oct-11 13:39pm    
Hello mzrax

I just checked with the following your data and its definitely working for me.

Latitude Longitude MGRS
34.15908051 39.00210953
30.13175774 26.67066956

Few Questions....
1.What version of MS Access you are using ?
2.Do you have an auto number field in your database ?
3.What is the .NET framework version you are running the application
4.Are you getting any error messages ? if so, what is the error messages

Also, please see my solution above for the console version under the heading Console version of the solution - UPDATE 2 ... can you try it out and let me know the error message
mzrax 8-Oct-11 14:12pm    
That's awesome Bala. You can't imagine how deeply thank you I am. I found something out. The stuff I've been working with is has 13 decimal points. When I remove some numbers to limit the decimal point to 9 it works. How would I limit the number of decimal points to only 9. Is there something that could look for the decimal point and then could 9 decimal points?

I can't say it enough. Thanks.
Bala Selvanayagam 8-Oct-11 14:20pm    
Nice to hear, its finally resolved

am not sure where do you want to restrict the decimal places and assume you want to round the decimal places from 15 to 9 in the c# application ?

double a = 1.123456789012345;
MessageBox.Show(Math.Round(a, 9).ToString());

If you are happy and the issue is resolved then please accept my answer and vote for me
mzrax 8-Oct-11 16:03pm    
Bala

Thanks Again. Sure I'll vote for you. I figured out the problem. I have other code that takes the decimal degrees in another table and copies them into the tblMGRSAdded where I've added the column MGRS.

What I did was in the SQL Insert Code from tblA to tblMGRSAdded I put the round(Latitude,9) as Latitude and round(longitude,9) as Longitude there so it only carried over the point with 9 decimal points. After that your code runs and then does the conversion in a slick way.

I still don't know why the conversion works with all 13 decimal points in the console window when you run it seperate but when you when you run it with the table it only recognizes 9 decimal places.

Thanks Again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900