Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Need help to get numeric output in MS Access database. That comboBOx items are characters.

What I have tried:

C#
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Mr") { comboBox1.SelectedValue = "1"; }
            if (comboBox1.Text == "Mrs") { comboBox1.SelectedValue = "2"; }
        } 

private void button1_Click(object sender, EventArgs e)
        {
            
           
            try
            {
                OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\dropdown.accdb; Persist Security Info = True");
                OleDbCommand cmd = con.CreateCommand();
                con.Open();
                cmd.CommandText = "INSERT INTO dropdown([AS_TITLE],[Name]) VALUES ('" + comboBox1.SelectedValue + "','" + textBox1.Text + "')";
                
                cmd.Connection = con;
                cmd.ExecuteNonQuery();

                
                
            MessageBox.Show("Successfully Uploaded");
                con.Close();
                
            }


            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
Posted
Updated 4-Jul-17 11:35am
v2
Comments
CHill60 4-Jul-17 17:07pm    
What is wrong with what you have?
Member 13292939 5-Jul-17 3:02am    
I need to know how can we convert character value of combo box into customize number in datatype....
Karthik_Mahalingam 5-Jul-17 1:05am    
show the databind code for combobox
Member 13292939 5-Jul-17 3:36am    
If I selected MR in a combobox then I want its value like 1 in database.
or if there is a full name in a combo box then i want just a first name from a combo box into database.

Awaiting
Regards

1 solution

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
 
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