user4646310
user4646310

Reputation:

Can't add values from combobox to database

I add values in property in combobox at Collection. Like this.

Add values in combobox

I want when select 1 items and click button save. I will save into database.

Button Save is:

CameraDTO obj = new CameraDTO();
obj.DefaultCam = Convert.ToInt16(cbxDefaultCam.Items.ToString());
CameraBUS.CameraInsert(obj);

In CameraBUS.CameraInsert is:

    public void Camera_Insert(CameraDTO data)
    {
        using (var cmd = new SqlCommand("sp_Camera_Insert", GetConnection()))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@DefaultCam", data.DefaultCam));
            cmd.ExecuteNonQuery();
        }
    }

Following is the error am getting while executing.

enter image description here

Upvotes: 0

Views: 49

Answers (1)

sujith karivelil
sujith karivelil

Reputation: 29006

cbxDefaultCam.Items returns a collection of ComboBoxItem. it cannot be converted to string using .ToString() or even Convert.To..() instead for this you can use any of the following based on your use.

  • cbxDefaultCam.SelectedItem.Text <-- which gives the bounded text field
  • cbxDefaultCam.SelectedValue <-- which gives the bounded value field

Upvotes: 1

Related Questions