Hello all,

I am at it again this time incorporating MySQL database into C# code using Visual Studio 2008 first off the textboxes are databinded using details view...

the issue at the moment is trying to not make bulky code and compare the value of txtDealName.text and mtxtDealNum2.text to what is in the Database

Here is my code for the button so far:

private void btnSavDealer_Click(object sender, EventArgs e)
        {
            
            //Create Database connection
            MySqlConnection conn = new MySqlConnection("server=localhost;user id=root;Password=666mysql69;persist security info=True;database=central1;");
            string query = "SELECT * FROM dealer WHERE DealNum = '" + lblDealNum2.Text + "'";
            MySqlCommand cmd1 = new MySql.Data.MySqlClient.MySqlCommand(query);
            

            if (txtDealName.Text == "")
            {
                MessageBox.Show("You must enter a name for this Dealer.");
                txtDealName.Focus();
                txtDealName.SelectAll();
                return;
            }
            else if (txtDealName.Text == cmd1.
            else
            {
                //MessageBox.Show(txtDealName.Text);
                string query = "UPDATE dealer SET DealName = '" + txtDealName.Text + "' WHERE DealNum = '" + lblDealNum2.Text + "'";
                MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(query);

                try
                {
                    conn.Open();
                    cmd.Connection = conn;
                    cmd.ExecuteNonQuery();
                }

                catch (MySqlException)
                {
                    System.Diagnostics.Debugger.Break();
                }
            }
            
            if (mtxtDealPhone2.Text == "" || mtxtDealPhone2.Text.Length == 12)
            { 
                 //MessageBox.Show(mtxtDealPhone2.Text);
                string query = "UPDATE dealer SET DealPhone2 = '" + mtxtDealPhone2.Text + "' WHERE DealNum = '" + lblDealNum2.Text + "'";
                MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(query);

                try
                {
                    cmd.Connection = conn;
                    cmd.ExecuteNonQuery();
                }

                catch (MySqlException)
                {
                    System.Diagnostics.Debugger.Break();
                }
            }
            else
            {
                MessageBox.Show("You need to enter a full phone number with area code for your Secondary number.");
                mtxtDealPhone2.Focus();
                mtxtDealPhone2.SelectAll();
                return;
            }

            this.Close();
        }

problem is I have about 20 fields to validate and if the values are the same i do not want it to write to the database that field...

For the front end Id consider just a small validation method, something like below as this can be checked on the front end. This can check if a field is empty before saving

public bool Validate()
        {
            foreach (Control control in this.Controls)
            {
                if (control is TextBox)
                {
                    if (((TextBox)control).Text == string.Empty)
                        return false;
                }
                //Handle other types if required
            }
            return true;;
        }

But for db validation Id be inclined to scrap your query strings and replace them with stored procedures(sp's). The sp could return you a status indicating if something wasnt right (ie a name already exists or something).

Hope that helps.

thanks Tower at least it gives me a direction to go will research more info on Stored Procedures

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.