private void insertbtn_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text.ToString();
            string course;
           
            for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
            {
                course += checkedListBox1.CheckedItems[i].ToString() + " ";
            }
               
         
            string constring = "PROVIDER= MSDAORA;USER ID = SYSTEM; PASSWORD = mca";
            string query = "insert into checkapp1 values('" + name + "','" + course+ "')";
            cn = new OleDbConnection(constring);
            cn.Open();
            cm = new OleDbCommand(query, cn);
            cm.ExecuteNonQuery();
            MessageBox.Show("data inserted");
        }

problem in inserting data by this code. showing a error"unassigned value 'course' is used". please verify and correct it. :(

Recommended Answers

All 2 Replies

the error is very clear... assign a value to your course variable eg. string course = "some text"; EIDT: I assume you have no checkbox selections, but by modifying/initializing the course declaration this error should go away...

DdoubleD is right. To elaborate more on the issue take this for example:

string s;
s += "abc";

Now you're appending a value to s, but what is s? It was never assigned a value thus you have the problem. What you can do:

string s = string.Empty;
s += "abc";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.