Displaying value in a text box from an sql database in visual studio

  Uncategorised

I am creating a basic app for adding and displaying customer information using windows forms in visual studio. i have set it up so i am able to display the contents of the database in a gridview and also add to the database which you can see the code for below. what i am stuck on at the moment is updating the customers information. I want to search the database by customerID which will be entered by the user in a textbox, and display that specific customers details into their relevent textboxes which i can then edit and save.

using (SQLiteCommand cmd = conn.CreateCommand())
            {
                // adds customers details to the database
                cmd.CommandText = @"INSERT INTO customer (title, " + "firstname, " + "lastname, " + "dob, " + "nicode, " + "email, " + "password, " + "allowance) VALUES (@setTitle, @setFirstname, @setLastname, @setDOB, @setNICode, @setEmail, @setPassword, @setAllowance)";
                cmd.Parameters.AddWithValue("setTitle", cb_title.Text);
                cmd.Parameters.AddWithValue("setFirstname", txtFirst_Name.Text);
                cmd.Parameters.AddWithValue("setLastname", txtSurname.Text);
                cmd.Parameters.AddWithValue("setDOB", dtp_DOB.Text);
                cmd.Parameters.AddWithValue("setNICode", txtNI_Code.Text);
                cmd.Parameters.AddWithValue("setEmail", txtEmail.Text);
                cmd.Parameters.AddWithValue("setPassword", txtPassword.Text);
                cmd.Parameters.AddWithValue("setAllowance", txtAllowance.Text);

                int recordsChanged = cmd.ExecuteNonQuery();
                MessageBox.Show("Customer Added");
                conn.Close();

                Customers customers = new Customers();
                customers.Show();
                this.Hide();

            }

Thats what i have for adding a new customer which works fine

using (SQLiteCommand cmd = conn.CreateCommand())
            {
                // adds customers details to the database
                cmd.CommandText = @"UPDATE customer SET (title, " + "firstname, " + "lastname, " + "dob, " + "nicode, " + "email, " + "password, " + "allowance) VALUES (@setTitle, @setFirstname, @setLastname, @setDOB, @setNICode, @setEmail, @setPassword, @setAllowance)  WHERE custid = @recd";

                cmd.Parameters.AddWithValue("title", cb_title_update.Text);
                cmd.Parameters.AddWithValue("firstname", txtFirst_Name_update.Text);
                cmd.Parameters.AddWithValue("lastname", txtSurname_update.Text);
                cmd.Parameters.AddWithValue("dob", dtp_DOB_update.Text);
                cmd.Parameters.AddWithValue("nicode", txtNI_Code_update.Text);
                cmd.Parameters.AddWithValue("email", txtEmail_update.Text);
                cmd.Parameters.AddWithValue("password", txtPassword_update.Text);
                cmd.Parameters.AddWithValue("allowance", txtAllowance_update.Text);
                cmd.Parameters.AddWithValue("recd", Convert.ToInt32(txtSearch.Text));

                int recordsChanged = cmd.ExecuteNonQuery();

                MessageBox.Show("Customer Updated");
                conn.Close();

                Customers customers = new Customers();
                customers.Show();
                this.Hide();
            }

And thats the code i have so far for updating the database, but i can not figure out how to retrieve the customer data and display it into the textboxes, any help or guidance would be appreciated

Source: Visual Studio Questions

LEAVE A COMMENT