How to Insert Data Into Sql server by C# Form?


#Insert Into Sql server save data

private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                string query = "INSERT INTO tbl_CustomersInfo (ID_Customers,NAME,GANDER,PHONE,ADDRESS,GATEGORY,QUATITY,PRICE,PAYMENT,DATE) VALUES('" + txtIDCus.Text + "','" + txtName.Text + "','" + cbogander.Text + "','" + txtPhone.Text + "','" + txtAddress.Text + "','" + cboGategory.Text + "','" + txtQuatity.Text + "','" + txtPrice.Text + "','" + txtPayment.Text + "','" + dtpTime.Text + "')";
                SqlDataAdapter SDA = new SqlDataAdapter(query, con);
                SDA.SelectCommand.ExecuteNonQuery();
           
                MessageBox.Show("Save Successful!");
                string Query = "SELECT * FROM tbl_CustomersInfo";
                SqlDataAdapter sda = new SqlDataAdapter(Query, con);
                DataTable data = new DataTable();
                sda.Fill(data);
                dgvCustomerInfo.DataSource = data;
                txtIDCus.Text= "";
                txtName.Text = "";
                cbogander.Text = "";
                txtPhone.Text = "";
                txtAddress.Text = "";
                cboGategory.Text = "";
                txtQuatity.Text = "";
                txtPrice.Text = "";
                txtPayment.Text = "";
                txtIDCus.Focus();
            }
            catch {
                MessageBox.Show("ID Duplicate!​ Or Have other error!");
            }
            con.Close();
        }

write by : Chankuoch

No comments