CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: pass values

  1. #1
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    pass values

    hey everyone,

    okay, once again with the application. i think i should just make one thread for this thing, but the question today is: how do i send out a request to an SQL database from a form, return the data, pass that data to the original form, and then open a new form with the data passed to the orignal form?

    i know its tricky so i will try and explain.


    original form = form1
    new form = form2
    search form = form3

    so form1 is told to open form3
    form3 inputs data to use to search the database with
    form3 completes the search
    database returns matching records to form3
    form3 passes these values back to form1
    form1 opens a new form2 and has the data form3 passed it fill in form2


    best i can do to explain what i need, i know its gonna require some public variables and probably a new class but i need to know how to make this work.

    thanks

    rockking

  2. #2
    Join Date
    Jan 2011
    Posts
    11

    Re: pass values

    Code:
    public partial class frmOne : Form
    {
            // Constructor
    	public frmOne(){}
    	
            // Button click event but the code could go anywhere
    	private void btnSearch_Click(object sender, EventArgs e)
    	{
    		DataTable searchResults;
    		searchResults = frmThree.Search();
    		
    		frmTwo myDisplay = new frmTwo();
    		myDisplay.ShowResults(searchResults);
    	}
    }
    
    public partial class frmTwo : Form
    {
    	private DataTable m_Results;
    	
            // Constructor
    	public frmTwo(){}
    	
    	public void frmTwo_Load(object sender, EventArgs e)
    	{
    		// Display results in DataGridView
    		dgvResults.Datasource = m_Results;
    	}
    	
            // Point of entry
    	public void ShowResults(DataTable results)
    	{
    		m_Results = results;
    	}
    }
    
    public partial class frmThree : Form
    {
    	private DataTable m_Results;
    	
            // Constructor
    	public frmThree
    	{
    		m_Results = new DataTable();
    	}
    	
            // Point of entry
    	public DataTable Search()
    	{
    		this.ShowDialog();
    		return m_Results;
    	}
    	
            // Button to search
    	private void btnSearch_Click(object sender, EventArgs e)
    	{
    		DataSet ds = new DataSet();
    		SqlConnection sqlConn = new SqlConnection("Your connection string");
    		SqlDataAdapter sqlAdapter = new SqlDataAdapter("Your SQL Query", sqlConn);
    
    		try
    		{
    			sqlConn.Open();
    			sqlAdapter.Fill(ds);
    			m_Results = ds.Tables[0];
    
    			// User could stay on form if there is an error but close if results are there
                            this.Close();	
    		}
    		catch (Exception ex)
    		{
    			MessageBox.Show(ex.Message);
    		}
    		finally
    		{
    			sqlConn.Close();
    			sqlConn.Dispose();
    			sqlAdapter.Dispose();
    		}
    	}
    }

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: pass values

    Keep in mind that you cannot set UI's properties like TextBox.Text in other thread than the UI one, so if you need to do it (e.g. for displaying results computed/fetched in other thread), you have to use Control.Invoke() method.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: pass values

    so, now could i create a partial class that was spread across all three forms in use? or would that be a little to risky?

  5. #5
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: pass values

    how do i make object references? i cant get them to work correctly, here is the code i have so far:

    Code:
            string connectString = ConfigurationSettings.AppSettings["DbPath"];
            string icon = ConfigurationSettings.AppSettings["ciIcon"];
            object A;
            private string lastName;
            string firstName;
    
            public frmMBCI()
            {
                A = new object();
                A = CS_CheckInCheckOut.Search.lastName;
                ReferenceConverter a;
                a.ConvertToString(A);
                lastName = a.ConvertToString(a);
                InitializeComponent();
                this.Icon = new Icon(icon);
                textBox1.Text =  lastName.ToString();
            }

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: pass values

    Do you think the ReferenceConverter? It's purpose is desing time. Just cast the reference
    Code:
    lastName = (string)a;
    // OR
    lastName = a as strng;
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  7. #7
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: pass values

    No, this isn't possible. Parts must form together single class.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  8. #8
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: pass values

    okay so i have it working between form1 and form3

    so how do i get it so that form2 tells form1 the value for form3 to read?

    form1
    Code:
     public partial class frmMain : Form
        {
            public static string lastName;
            string firstName;
            string connectString = ConfigurationSettings.AppSettings["DbPath"];
            string mainIcon = ConfigurationSettings.AppSettings["mainIcon"];
            public frmMain()
            {
                InitializeComponent();
                this.KeyDown += new KeyEventHandler(frmMain_KeyDown);
                this.PreviewKeyDown += new PreviewKeyDownEventHandler(frmMain_PreviewKeyDown);
                textBox1.Text = "Last Name";
                textBox2.Text = "First Name";
                this.Icon = new Icon(mainIcon);
            }
    
            void frmMain_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
            {
                switch (e.KeyCode)
                {
                    case Keys.End:
                        {
                            e.IsInputKey = true;
                            break;
                        }
                }
            }
    
            void frmMain_KeyDown(object sender, KeyEventArgs e)
            {
                switch (e.KeyCode)
                {
                    case Keys.End:
                        {
                            this.Close();
                            break;
                        }
                }
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                lastName = textBox1.Text;
            }
    
            private void textBox2_TextChanged(object sender, EventArgs e)
            {
                firstName = textBox2.Text;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
               /* SqlConnection dataConnection = new SqlConnection();
                dataConnection.ConnectionString = "Data Source=DELL\\SQLEXPRESS;Initial Catalog=Learning_DB;User ID=jsitz;Password=labR2o6";
                SqlCommand dataCommand = new SqlCommand();
                try
                {
                    dataConnection.Open();
                    dataCommand.CommandText = "INSERT INTO Learning_Table([last name]) VALUES ('" + lastName + "')";
                    dataCommand.ExecuteNonQuery();
    
                }
                finally
                {
                    dataConnection.Close();
                }*/
               /* using (var conn = new SqlConnection("Data Source=DELL\\SQLEXPRESS;Initial Catalog=Learning_DB;User ID=jsitz;Password=labR2o6"))
                {
                    using (var cmd = new SqlCommand("INSERT INTO Learning_Table([Last Name], [First Name]) VALUES('"+ lastName +"', '"+ firstName +"')", conn))
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                    }
                }
                */
    
                using (var conn = new SqlConnection(connectString))
                {
                    using (var cmd = new SqlCommand("[dbo].[Learning.Delete]", conn))
                    {
                        conn.Open();
    
                        //set command type to stored procedure
                        cmd.CommandType = CommandType.StoredProcedure;
    
                        //Add input parameters
                        cmd.Parameters.AddWithValue("@LastName", lastName);
                        cmd.Parameters.AddWithValue("@FirstName", firstName);
    
                        //add output parameters
                        cmd.Parameters.Add("@NewLearningId", SqlDbType.Int);
                        cmd.Parameters[2].Direction = ParameterDirection.Output;
    
                        //execute
                        cmd.ExecuteNonQuery();
    
                        //retrieve inserted id
                        //newLearningId = (int) cmd.Parameters[2];
                    }
                }
    
    
                MessageBox.Show("Successfully added " + lastName + ", " + firstName, "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                textBox1.Text = "Last Name";
                textBox2.Text = "First Name";
            }
    
            private void checkInToolStripMenuItem2_Click(object sender, EventArgs e)
            {
                /*frmMBCI mbci = new frmMBCI();
                mbci.ShowDialog();
                */
                Search search = new Search();
                search.ShowDialog();
                lastName = CS_CheckInCheckOut.Search.lastName;
            }
    
            private void quitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    form2
    Code:
        public partial class Search : Form
        {
            string icon = ConfigurationSettings.AppSettings["searchIcon"];
            public static string lastName;
            string firstName;
            public Search()
            {
                InitializeComponent();
                this.Icon = new Icon(icon); 
            }
    
            private void txtLName_TextChanged(object sender, EventArgs e)
            {
                lastName = txtLName.Text;
                CS_CheckInCheckOut.frmMain.lastName = lastName;
            }
    
            private void btnQuery_Click(object sender, EventArgs e)
            {
                frmMBCI mbci = new frmMBCI();
                mbci.ShowDialog();
                this.Close();
            }
    
        }
    form3
    Code:
     public partial class frmMBCI : Form
        {
            string connectString = ConfigurationSettings.AppSettings["DbPath"];
            string icon = ConfigurationSettings.AppSettings["ciIcon"];
            object A;
            string lastName;
            string firstName;
    
            public frmMBCI()
            {
                InitializeComponent();
                A = new object();
                A = CS_CheckInCheckOut.frmMain.lastName;
                //ReferenceConverter a;
                //a.ConvertToString(A);
                lastName = (string)A;
                
                this.Icon = new Icon(icon);
                textBox1.Text = lastName;//(string)A;
            }
    }
    are there any special variable types that i should be using? or am i just going the wrong way about all this?

  9. #9
    Join Date
    Jul 2010
    Location
    .NET 4.0/VS2010
    Posts
    79

    Re: pass values

    so here is what i am trying. i now have the 3 forms and another class named variables

    variables has all the variables needed to be used between all the forms
    so how do i make the values of the variables in class variables change to whatever is assigned to them from and of the forms? here is how it would go

    form3 tells variable studentLastName a value
    form3 closes
    form2 opens and reads the variable studentLastName
    studentLastName now becomes the text of a text box

    much simpler and less confusing this time

    but what type of variable should i make each variable? currently they are all "public static string"
    is this the proper type to use?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured