anitstudent February 2016

Using textbox value for a class

i'm trying to use my Textbox value from another form to a class to insert items to my database. I tried creating another instance of my Form1 which is the name of the form I want to get the value from but it returns 0 items to my database when I click my submit button is there anyway to do this?

public void Insert()
    {
        Form1 mform = new Form1();
        string query = "INSERT INTO parts (item_id, description, brand, model, color, quantity) VALUES('0', '"
               + mform.Description.Text
                 + "','" + mform.Brand.Text
                + "','" + mform.Model.Text
                + "','" + mform.Color.Text
                + "','" + mform.Quantity.Text + "')";

        if (this.OpenConnection() == true)
        {
           MySqlCommand cmd = new MySqlCommand(query, connection);            
           cmd.ExecuteNonQuery();                
           this.CloseConnection();
        }
    }

Answers


David Pine February 2016

When you instantiate a new instance of the Form1() object, you are assuming that the new instance's "Description, Brand, Model, Color and Quantity" TextBox's contains text? The default value of a TextBox.Text is the default value of its string property type, null.

Ideally, you will take the values that the user has populated from the form instance and then pass them into the DB like so:

public void Insert()
{
    using (var mform = new Form1())
    {
        // Ensure that the user entered values...
        if (mform.ShowDialog() == DialogResult.Ok)
        {
            string query = "INSERT INTO parts (item_id, description, brand, model, color, quantity) VALUES('0', '"
               + mform.Description.Text
               + "','" + mform.Brand.Text
               + "','" + mform.Model.Text
               + "','" + mform.Color.Text
               + "','" + mform.Quantity.Text + "')";

            if (this.OpenConnection() == true)
            {
               var cmd = new MySqlCommand(query, connection);
               cmd.ExecuteNonQuery();
               this.CloseConnection();
            }
        }
    }
}

Additionally, you should avoid inline SQL and instead use stored procedures, or at the very least use SqlParameter's.


SiD February 2016

No, you cannot access Form1 values in a class by creating new instance. To access textbox values you need to do the following:

  1. Create public property in a class
  2. Assign property with textbox value in some appropriate event (such as TextChanged)
  3. Access property inside class to get textbox value and save it to database.

Example

Class:

public class DataAccess
{
    public string IncomingValue { get; set; }

    public string SaveToDatabase()
    {
        string valueToSave = IncomingValue;

        // Insert into database

        return "Success";
    }
}

Form:

DataAccess access = new DataAccess();

private void textBox1_TextChanged(object sender, EventArgs e)
{
    access.IncomingValue = textBox1.Text;
}

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(access.SaveToDatabase());
}

Also, I would suggest you to use Parametrized Query. This will give you more readability and saves you from SQL injection and confusion.

Post Status

Asked in February 2016
Viewed 3,356 times
Voted 11
Answered 2 times

Search




Leave an answer