problem with Object and SQLCommand

Hi  I have a problem in my code,  i create an object  and i try to concat it  to an sql command like a control but i get  the next error: Invalid Object Name Combobox1.SelectedItem.ToString()

here is the code

                cmd.CommandType = CommandType.Text
                Dim i As Integer
                Dim combotext As Object
                For i = 1 To 20
                    combotext = "ComboBox" & i & ".SelectedItem.Tostring()"
                    cmd.CommandText = "insert into respuestas values (GetDate(), 'Formato de    mantenimiento preventivo', '" & Categoria.Text & "', " & i & ", " & combotext & " )"
                    MessageBox.Show(cmd.CommandText.ToString)
                    cmd.Connection = SqlConnection1
                    cmd.CommandType = CommandType.Text
                    da.SelectCommand = cmd
                    da.Fill(ds)
                Next

I hope somebody can help me


Answer this question

problem with Object and SQLCommand

  • Skip_Frog

    validate that Combobox1 isn't null before you attempt to call the .ToString() method.

    actually.. what's with the "Combobox" & i & ".Selected.,..

    i don't think that works.

  • RickLH

    Even though your string "combotext" contains the method .SelectedItem.ToString(), this "code" is not called.  The complier does not look for code to execute in a value.  A string is a string, simply a line of text.  That text means nothing to the compiler as far as code goes.

    What you need to do here is create an array of ComboBoxes before you enter your For loop.  Then you can get the value of each ComboBox as your loop progresses.

    Something like:

    'This assumes that all your ComboBoxes are on the form directly and that all comboboxes on the form contain select commands; if this isn't the case, you can build the array directly

    Dim cbs As New ArrayList
    For Each c As Control in Me.Controls
    If c.GetType() is GetType(ComboBox) Then
    cbs.Add(c)
    End If

    'Now you've got a collection of all your ComboBoxes so you can reference the one you want in this loop
    cmd.CommandType = CommandType.Text 
    Dim i As Integer 
    For i = 0 To 20

    cmd.CommandText = "insert into respuestas values (GetDate(), 'Formato de mantenimiento preventivo', '" & Categoria.Text & "', " & i & ", '" & CType(cbs.Item(i-1), ComboBox).SelectedItem.ToString() & "')" 

    MessageBox.Show(cmd.CommandText.ToString) 

    cmd.Connection = SqlConnection1 
    cmd.CommandType = CommandType.Text 
    da.SelectCommand = cmd 
    da.Fill(ds) 

    Next 


    Now, bear in mind that even with that fix, you still have problems...

    You are writing an Insert statement but assigning it to the SelectCommand of your DataAdapter...  this could cause undesired results when using the DataAdapter.  For instance, the Fill command should fail to fill the DataSet since it doesn't have a Select Command.  You should be setting da.InsertCommand = cmd.

    Also, I'm guessing that GetDate() is a stored procedure in your database   If its a function in your app, then it's not going to be called because, like your "combotext" it is simply a string value.

  • problem with Object and SQLCommand