The problem with your query is that the SelectedItem
property is not a string
, but a DataRowView
. First you must cast that property to its real type and then get the column you are interested in (in this example I use the 0, in your case the one you need)
On the other hand never generate your query using string concatenation. Use parameterized queries or your code will be exposed to SQL injection. Your code should be more or less like this:
'Convertimos los "SelectedItem" a su tipo real,DataRowView
Dim cat As DataRowView = TryCast(Categoria.SelectedItem, DataRowView)
Dim estad As DataRowView = TryCast(Estado.SelectedItem, DataRowView)
Agregar = "insert into cita (nCuenta, Categoria, Fecha, FechaC, Estado, Usuario)"
Agregar += " values(@nCuenta,@Categoria,@fecha,@fechac,@estado,@usuario)"
cmd.CommandText = Agregar
cmd.Parameters.Add("@nCuenta", SqlDbType.VarChar).Value = nCuenta.Text
cmd.Parameters.Add("@Categoria", SqlDbType.VarChar).Value = cat.Item(0).ToString()
cmd.Parameters.Add("@fecha", SqlDbType.DateTime).Value = fec
cmd.Parameters.Add("@fechac", SqlDbType.DateTime).Value = fac
cmd.Parameters.Add("@estado", SqlDbType.VarChar).Value = estad.Item(0).ToString()
cmd.Parameters.Add("@usuario", SqlDbType.VarChar).Value = Usuario.Text
cmd.ExecuteNonQuery()
There is another option. If in your ComboBox
you set the ValueMember
property, what you should do is replace SelectedItem
with SelectedValue.ToString()
.