1

I want to insert data to my access database by combobox that have some item in it but some time when I want insert some thing there is not exist in my combobox how can write it down manually on my combobox and insert it to my db

enter image description here

OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into [data] ( [Description] ) values ('" + comboBox10Text + "' )";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show("Data Inserted Successfully");
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445

2 Answers2

0

You can add another item to your combobox something like other. Then check the selected item, if it is other then enable a textbox and read the input value from it. Otherwise, read the input value from the combobox.

Mahdi
  • 3,199
  • 2
  • 25
  • 35
0

You are actually almost there, just a dot missing. comboBox10.Text will give you the entered text:

cmd.CommandText = "insert into [data] ( [Description] ) values ('" + comboBox10.Text + "' )";

I would suggest to check for emptiness and use parameterized sql commands to avoid SQL Injection

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • ah ok, if you don't mind empty values. Still, there is a dot missing in `comboBox10Text` and you really should consider to parameterize your query – Mong Zhu Nov 08 '16 at 16:57