Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
Hi,

I have developed a c sharp application which will execute an sql Query and get corresponding Coordinates(latitude, longitude) of a location. Once the program executes the sql query it will get the Location Coordinates.

Now after getting the coordinates from 1st form, It will go to second form and there in that form a webbrowser control is embedded. So it should open a google Map with the location from coordinates.

But unfortunately, when i pass the values from first form to 2nd form. The values are passing to 2nd form successfully.

But the webpage is not displaying at all. Only white screen is coming.

But when i call the 2nd form directly from main Program then it is trying to open the Webpage and returning the error that Google Maps is not supported in the browser. So i need a new browser like chrome or firefox embedded in the form to implement.

Main Program:-
C#
namespace LocationFinder
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new MainForm());  //When this main form is commented out , its executing 2nd form and displaying an error.
            Application.Run(new LocationMap()); //When this 2nd form is commented out , its executing 1st form, then calling 2nd one from 1st and displaying a blank White Page.
        }
    }
}


Form1:-

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;

namespace LocationFinder
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        public void MainForm_Load(object sender, EventArgs e)
        {   
            string old_value = "";

                while (true)
                {
                    String connetionString = null;
                    SqlConnection conn;
                    SqlCommand cmd;
                    String sql = null;
                    SqlDataReader reader;
                    connetionString = "server=(local);database=modelDb;user id=sa;pwd=123456";
                    sql = "DECLARE @var varchar(1000) = (SELECT TOP 1 Text FROM Alarms WHERE AlarmDefinitionId=139 ORDER BY EventTime DESC) DECLARE @start_position int, @end_position int SELECT @start_position = PATINDEX('% at%', @var) SELECT @end_position = PATINDEX('%kilometers%', @var) DECLARE @VALUE VARCHAR(10) = (Select SUBSTRING(@var, @start_position+5,5)) Select Top 1 @VALUE,RouteTable.Latitude,Routetable.Longitude,Alarms.ApplicationTime,RouteTable.StationName,RouteTable.SectionName FROM Alarms INNER JOIN Routetable ON Routetable.Location BETWEEN FLOOR(@VALUE)-1 AND CEILING(@VALUE)+1 WHERE AlarmDefinitionId=139 ORDER BY EventTime DESC";

                    conn = new SqlConnection(connetionString);
                    try
                    {
                        conn.Open();
                        cmd = new SqlCommand(sql, conn);
                        reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {

                            if (old_value.ToString() != reader.GetValue(0).ToString())
                            {
                                MessageBox.Show("Location:-" + "              " + reader.GetValue(0) + Environment.NewLine + "Latitude:-" + "                          " + reader.GetValue(1) + Environment.NewLine + "Longitude:-" + "                       " + reader.GetValue(2) + Environment.NewLine + "Leak Occured Time:-" + "       " + reader.GetValue(3) + Environment.NewLine + "Station Name:-" + "                 " + reader.GetValue(4) + Environment.NewLine + "Section Name:-" + "                " + reader.GetValue(5));
                                old_value = reader.GetValue(0).ToString();
                                string lat = reader.GetValue(1).ToString();
                                string lon = reader.GetValue(2).ToString();
                                LocationMap form1 = new LocationMap(lat, lon);
                                form1.Show();
                            }
                            else
                            {

                            }
                            
                       }
                        reader.Close();
                        cmd.Dispose();
                        conn.Close();
                    }
                    catch (Exception ex)
                    {
                      MessageBox.Show(ex.ToString());
                    }
                    System.Threading.Thread.Sleep(5 * 1000);
                }
            }
        
     private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About_LocationFinder formhelp = new About_LocationFinder();
            formhelp.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


Form2:-

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LocationFinder
{
    public partial class LocationMap : Form
    {
        public LocationMap(string lati, string longi)
        {
            InitializeComponent();
            textBox2.Text = longi.ToString();
            textBox1.Text = lati.ToString();
            
        }
        
        private void LocationMap_Load(object sender, EventArgs e)
        {
            string Latitude = textBox1.Text;
            string Longitude = textBox2.Text;
            try
            {
                string address = "https://www.google.co.in/maps/place/";

                if (Latitude != string.Empty && Longitude != string.Empty)
                {
                    string address = address + Latitude + "," + Longitude;
                    webBrowser1.Navigate(address);
                }
                else
                {
                   webBrowser1.Navigate(address);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }  
        }
    }
}


The Error When i call 2nd form directly from the main program is in the below link:-

http://s29.postimg.org/xw4661hav/Error.jpg[^]
Posted
Updated 5-May-15 20:14pm
v3
Comments
Sergey Alexandrovich Kryukov 6-May-15 2:33am    
Are you using some stone-age browser? Why would you worry about this "error" at all?
—SA
Krishna Chaitanya Bezawada 6-May-15 3:26am    
I have used web browser control in windows forms, then it is returning that error. I need to display that webpage, its not displaying that webpage.

1 solution

Forget the Windows Forms web browser. It's based on Internet Explorer's engine. The main problem however is that it reports itself as Internet Explorer 7 to the web pages, which came out in 2006, so it's pretty much unsupported by every major website.

Alternatively you can use the Awesomium HTML UI Engine, which is based on webkit. http://www.awesomium.com/
 
Share this answer
 

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