Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hye, this is my code to change the checkbox value to String and send to Database. But, i cannot save to my database.

thank you,

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Checkbox_Database
{
    public partial class Form1 : Form
    {

        //OPEN CONNECTION
        OleDbConnection con = new OleDbConnection(@"");

        String gen;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'cHECKBOX_DATABASEDataSet.Table1' table. You can move, or remove it, as needed.
            this.table1TableAdapter.Fill(this.cHECKBOX_DATABASEDataSet.Table1);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Checkbox

            if (checkBox1.Checked == true)
            {
                gen = "Male";
            }
            else if (checkBox2.Checked == true)
            {
                gen = "female";
            }

            try
            {
                con.Open();
                OleDbCommand cmd = con.CreateCommand();
                cmd.CommandText = "insert into Table1 values('" + textBox1.Text + "','"+ gen +"')";
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Record insert Succesfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }


        }
    }
}
Posted
Updated 27-Jul-17 23:03pm
Comments
Karthik_Mahalingam 28-Jul-17 4:54am    
what is the error message?

1 solution

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

There is also the likelihood that your INSERT is not working beause you don;t specify the fields:
C#
cmd.CommandText = "insert into Table1 values('" + textBox1.Text + "','"+ gen +"')";
When you insert like this, SQL starts inserting values from the first column onward - which is dangerous, because if the columns get reordered for any reason, your code inserts into the wrong rows and either crashes, or worse fills your db with a mixture of valid and invalid data. Plus, if you have a row ID column which is an IDENTITY column say, they are generally the first column, and you can't set a value to them - SQL will complain if you try.
Always specify the fields, always use parameterised queries:
C#
cmd.CommandText = "INSERT INTO Table1 (myColumn1, myColumn2)  VALUES(@V1, @V2)"
cmd.Parameters.AddWithValue("@V1", textBox1.Text);
cmd.Parameters.AddWithValue("@V2". gen);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900