Click here to Skip to main content
15,890,412 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. Here is the problem

Form 2
 private string data;

private void btnOK_Click(object sender, EventArgs e)
        {
            data=txtDB.Text; //set data
            this.Hide();
        }

        public string getData()
        {
            return data;
        }


Form 1
Form2 frm = new Form2();
string data = frm.getData(); //get data from Form2
 MessageBox.Show(data);



The messagebox is empty. How to make sure the data is passing
Posted
Comments
Ron Beyer 10-Oct-13 23:43pm    
Where do you show the form to let the user enter the data?
arave0521 10-Oct-13 23:59pm    
In form2.
FORM2(schemaName)

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 DynamicSimulator_v2
{
public partial class SchemaName : Form
{
private string data;

public SchemaName()
{
InitializeComponent();
}

private void btnCancel_Click(object sender, EventArgs e)
{
this.Hide();
}

private void btnOK_Click(object sender, EventArgs e)
{
data=txtDB.Text;
this.Hide();
}

public string getData()
{
return data;
}

}
}


FORM1(frmMain)


namespace DynamicSimulator_v2
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}

private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void btnNew_Click(object sender, EventArgs e)
{
string data = frm.getData(); //get data from Form2
SchemaName schemaForm = new SchemaName();
if (schemaForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show(data);
string connStr = "datasource=localhost;port=3306;username=root;password=root;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
conn.Open();
MySqlCommand cmdDatabase = new MySqlCommand("CREATE SCHEMA @schema; ", conn);
cmdDatabase.Parameters.AddWithValue("@schema",data);
MySqlDataReader myReader;
myReader = cmdDatabase.ExecuteReader();
myReader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();

}
}
}
}
ArunRajendra 10-Oct-13 23:44pm    
Are you showing form2 inside form1? if so put that code.
arave0521 10-Oct-13 23:58pm    
FORM2(schemaName)

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 DynamicSimulator_v2
{
public partial class SchemaName : Form
{
private string data;

public SchemaName()
{
InitializeComponent();
}

private void btnCancel_Click(object sender, EventArgs e)
{
this.Hide();
}

private void btnOK_Click(object sender, EventArgs e)
{
data=txtDB.Text;
this.Hide();
}

public string getData()
{
return data;
}

}
}


FORM1(frmMain)


namespace DynamicSimulator_v2
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}

private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void btnNew_Click(object sender, EventArgs e)
{
string data = frm.getData(); //get data from Form2
SchemaName schemaForm = new SchemaName();
if (schemaForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show(data);
string connStr = "datasource=localhost;port=3306;username=root;password=root;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
conn.Open();
MySqlCommand cmdDatabase = new MySqlCommand("CREATE SCHEMA @schema; ", conn);
cmdDatabase.Parameters.AddWithValue("@schema",data);
MySqlDataReader myReader;
myReader = cmdDatabase.ExecuteReader();
myReader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();

}
}
}
}
VICK 11-Oct-13 0:22am    
Have you tried session or query string???

C#
public static string str="";

       private void button1_Click(object sender, EventArgs e)
       {
           str = textBox1.Text;
           
           this.Hide();

       }
       public string getdata()
       {
           return str;
       }


form2
C#
Form1 f = new Form1();
           label1.Text =f.getdata();
 
Share this answer
 
Create a Class with Static property getData and set the value. Then you will be able to access this value any where in the application
 
Share this answer
 
Comments
arave0521 11-Oct-13 0:00am    
May I know how to set it as property. Quite new in c#.net
[no name] 11-Oct-13 0:06am    
why do you hang out with property? just use internal access specifier before variable...simple..
use
C#
internal string
datatype associated with
C#
data


copy contents from data to another string.
You are getting error because data is private.

e.g

C#
private string data=user.txt;
internal string s=data;


internal datatype can be used anywhere within same namespace.
 
Share this answer
 
Example of class with Static Property -

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class Common
    {
        public static string getData
        { get; set; }
    }
}


After creating the class you can access or set value of the property any where in your application using -

C#
Common.getData="TEST";
 
Share this answer
 
Comments
ArunRajendra 11-Oct-13 0:31am    
When you want to change the solution use "Improve solution" option. Don't post it as new solution.
Madhu Nair 11-Oct-13 0:53am    
Whose solution you want me to improve?

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