Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have developed an C# Windows forms application which executes an SQL Query and display the output in the form. So i have now took that 3 output column values into 3 variables.
i need that 3 values to get into another form in the same project, so that another form will do its work, using these values.

My Main Form which holds output is:-
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 + "Time:-" + "       " + reader.GetValue(3) + Environment.NewLine + "Station Name:-" + "                 " + reader.GetValue(4) + Environment.NewLine + "Section Name:-" + "                " + reader.GetValue(5));
                                string lat = reader.GetValue(1).ToString();
                                string lon = reader.GetValue(2).ToString();
                            }
                            else
                            {

                            }
                            old_value = reader.GetValue(0).ToString();
                       }
                        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_LeakLocationFinder formhelp = new About_LeakLocationFinder();
            formhelp.Show();
        }

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


I need to use the value of reader[1], reader[2] to be used in another form. So that my 2nd form will work.
Posted
Updated 4-May-15 0:26am
v2
Comments
[no name] 4-May-15 6:35am    
Use session.
Try this : Passing Data Between Forms
Maciej Los 4-May-15 6:41am    
This question has been marked as WinForm...
Florian Braun 4-May-15 6:42am    
is your second Form already open or do you open it after getting the values from your sql?

1) if the latter is true: just change the constructor of your second form.
2) if the form is already you can write a public method in second form like: public void SetValue(string a, string b, string c) { //do sth with a,b,c}
Krishna Chaitanya Bezawada 4-May-15 6:54am    
After getting the values from SQl, and after user clicks the ok in message box, then the form 2 will open. May i know briefly how can i construct form 2 according to you?
Karthik_Mahalingam 4-May-15 6:48am    
use static variables to pass the data to other forms

when you create an empty form it looks like this:

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
}


to get three variables from constructor you can change it to:

C#
public partial class Form1 : Form
{
    string a;
    string b;
    string c;
    public Form1(string _a, string _b, string _c)
    {
        InitializeComponent();
        a=_a;
        b=_b;
        c=_c;

        //example for doing something with a,b and c:
        label1.Text=a;
        textBox1.Text=b;
        listBox1.Items.Add(c);
    }
}


showing that second form might be something like:
C#
var frm=new Form1("some","string","to show");
frm.Show();


remember whats here called Form1 is your second form so you need to use your own name.
 
Share this answer
 
v2
you need to pass variable like this :
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
       {
           About_LeakLocationFinder formhelp = new About_LeakLocationFinder(reader[1], reader[2]);
           formhelp.Show();
       }


Now, you have to go to destination form and write this code :
public About_LeakLocationFinder(string rdr1, string rdr2)
       {
           InitializeComponent();
           reader1=rdr1;
           reader2=rdr2;
       }


Hope this help.
 
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