3
Answers

C# - Submitting Windows form to SQLite database

Stan Sypek

Stan Sypek

9y
615
1
I can use the following code to submit to a database on button click:
 
private void btn_add_Click(object sender, EventArgs e)
        {
           
            try
            {
                myConnection.Open();
                SQLiteCommand myCommand = new SQLiteCommand(myConnection);
                myCommand.CommandText = "Insert Into Person (FirstName,LastName,Email,Address,HomePhone,WorkPhone,MobilePhone) " +
                                        "Values('" + txt_firstName.Text + "','" + txt_lastName.Text + "','" + txt_email.Text + "','" +
                                         txt_address.Text + "','" + txt_homePhone.Text + "','" +
                                         txt_workPhone.Text + "','" + txt_mobilePhone.Text + "')";
                myCommand.ExecuteNonQuery();
                myConnection.Close();
                readData();
                //MessageBox.Show("Your new data saved.");
                txt_address.Text = "";
                txt_email.Text = "";
                txt_firstName.Text = "";
                txt_homePhone.Text = "";
                txt_lastName.Text = "";
                txt_mobilePhone.Text = "";
                txt_workPhone.Text = "";
            }
            catch (Exception)
            {
               
                throw;
            }
        }
 
 
This works fine as long as it is just text.  I want a variation on this using text, radio buttons, and a combobox.  All values should be saved to the database on button click.  I'm using the code:
 
myConnection.Open();
SQLiteCommand myCommand = new SQLiteCommand(myConnection);
myCommand.CommandText = "Insert Into Albums (Artist,AlbumName,Year,CD,Cass,Vinyl,R2R,8Track,Genre) " +
                        "Values('" + txt_artistName.Text + "','" + txt_albumTitle.Text + "','" + txt_year.Text + "','" +
                                     radbtnCD.Select + "','" + radbtnCass.Select + "','" + radbtnVinyl.Select + "','" +
                                     radbtnRtoR.Select + "','" + radbtn8Trk.Select + "','" + combx_genre.Select + "')";
 
This gives the following error:
 
Operator '+' cannot be applied to operands of type 'string' and 'method group'.  The error goes away if I remove all the components but the text.  Can someone point me in the correct direction on how to mix the controls and submit them to the same database record? Thanks in advance.
Answers (3)