I am doing a sample program where it involves inserting data from textboxes and a Combox to SQL Server databse ...

private void btnAdd_Click(object sender, EventArgs e)
        {

            da.InsertCommand = new SqlCommand("INSERT INTO Member VALUES (@MemberID, @Name, @Surname, @IDNo, @Address, @Town, @TelNo, @MobNo, @Email)", c);

            da.InsertCommand.Parameters.Add("@MemberID", SqlDbType.VarChar).Value = textBox50.Text;
            da.InsertCommand.Parameters.Add("@Name", SqlDbType.VarChar).Value = textBox49.Text;
            da.InsertCommand.Parameters.Add("@Surname", SqlDbType.VarChar).Value = textBox48.Text;
            da.InsertCommand.Parameters.Add("@IDNo", SqlDbType.VarChar).Value = textBox47.Text;
            da.InsertCommand.Parameters.Add("@Address", SqlDbType.VarChar).Value = textBox46.Text;
            da.InsertCommand.Parameters.Add("@Town", SqlDbType.VarChar).Value = comboBox1.SelectedItem;
            da.InsertCommand.Parameters.Add("@TelNo", SqlDbType.VarChar).Value = textBox51.Text;
            da.InsertCommand.Parameters.Add("@MobNo", SqlDbType.VarChar).Value = textBox52.Text;
            da.InsertCommand.Parameters.Add("@Email", SqlDbType.VarChar).Value = textBox53.Text;

            c.Open();
            da.InsertCommand.ExecuteNonQuery();             
            MessageBox.Show("Done");     

            c.Close();


        }

The problem is regarding the comboBox ! It is giving me this error when I am adding the data to the SQL Server :

Failed to convert parameter value from a DataRowView to a String.

in the server, the Town is declared as varchar(90) .

please help ! thanks :)

Recommended Answers

All 9 Replies

Add "ToString()" method on the end:

comboBox1.SelectedItem.ToString();

To add: SelectedItem for it self is an object type, but I need to have a string.
You could even Convert to string like:

Convert.ToString(comboBox1.SelectedItem);

thanks !

Hi, why do you wanna use selectedValue for Town field? There is actually no point, unless if you wanna get an id
of town. Else, use SelectedItem.
If you wanna get a value from comboBox - this means you defined display and value member and set datasouce for combobox.

To get a value you do:

DataRowView view = comboBox1.SelectedItem as DataRowView;
da.InsertCommand.Parameters.Add("@Town", SqlDbType.VarChar).Value = int.Parse(view["columnName"].ToString());
//this is if you want to get an integer - an id column defined as value member.

If you wanna get only display member only do:
// .. Value = view["displayMemberColumnName"].ToString();

Hope it helps.
bye

ups, sorry, I forgot your ids are string (varchars), not integers.
Try to change this code to:

DataRowView view = comboBox1.SelectedItem as DataRowView;
da.InsertCommand.Parameters.Add("@Town", SqlDbType.VarChar).Value = view["ID_Town"].ToString();

NOTE: And make sure "ID_Town" is the corrent name of the value member of comboBox!!!

and if my ids are int ?

I have my ID's are Incremented by 1 automatically (int) and I want to add them from my combobox to a new table in SQL server.

thanks

Do you increment your id's manually or is it done by sql

and if my ids are int ?

Then do:

DataRowView view = comboBox1.SelectedItem as DataRowView;
da.InsertCommand.Parameters.Add("@Town", SqlDbType.Int).Value = int.Parse(view["ID_Town"].ToString());

thats it.

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.