tags:

views:

160

answers:

1

I'm trying to paste the values from a textbox in form1 to textbox in form2. I did that, but while i upgraded my aplication it stopped to work. I allso need that couse i get an error(incorect data type in conditional statement) when i want to insert a value from a textbox (to a access database) that's not on the form that makes the insert statemen. the code: string textFromForm1;

    public Form2()
    {
        InitializeComponent();


    }

    public void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        using (Form3 obrok = new Form3()) 
        obrok.ShowDialog();

    }

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


    }

    private void button2_Click(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged_1(object sender, EventArgs e)
    {

    }
    Form1 bmr=new Form1();
    int masa;
    private void Form2_Load(object sender, EventArgs e)
    {
        textBox1.Text = bmr.masaTextBox.Text;    
    }

the code for insert statement: string conString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\Users\Simon\Desktop\save.mdb";

        OleDbConnection empConnection = new OleDbConnection(conString);


        string insertStatement = "INSERT INTO obroki_save "
                             + "([ID_uporabnika],[ID_zivila],[skupaj_kalorij]) "
                             + "VALUES (@ID_uporabnika,@ID_zivila,@skupaj_kalorij)";

        OleDbCommand insertCommand = new OleDbCommand(insertStatement, empConnection);

        insertCommand.Parameters.Add("@ID_uporabnika", OleDbType.Char).Value = users.iDTextBox.Text; 
        insertCommand.Parameters.Add("@ID_zivila", OleDbType.Char).Value = iDTextBox.Text;
        insertCommand.Parameters.Add("@skupaj_kalorij", OleDbType.Char).Value = textBox1.Text;
        empConnection.Open();

        try
        {
           int count = insertCommand.ExecuteNonQuery();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            empConnection.Close();
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
        }
A: 

Generally that is not a good thing to do. I am not even sure if that approach is thread-safe.

Instead of doing

textBox1.Text = bmr.masaTextBox.Text; 

try adding public properties to your form1, and keep your data there. Or alternatively pass the necessary data to form2's constructor. Anyways, textbox should not be a data storage where you keep a value then access from somewhere else... textbox should be used for displaying data and reading it.

m0s
i managed to solve the problem, but i'm still getting error that there is a wrong datatype in the conditional statement. Why is that?In form 3(where i want to insert data. all data is inserted from the textboxes from the form 3. why do i have that problem if i did the same stuff on other tables and it worked?)
simon