0

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 :)

3
Contributors
9
Replies
10
Views
5 Years
Discussion Span
Last Post by Mitja Bonca
0

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);

Edited by Mitja Bonca: added some explanation

0

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

0

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!!!

Edited by Mitja Bonca

0

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

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.