none
Insert DataValueField RRS feed

  • Pergunta

  • Como posso fazer um insert com o valor do DataValueField = ("TIPOCOD"), sendo que o DropDownList "ddlProd" lista  DataTextField = ("TIPONOME"), conforme as linhas abaixo:

    Dim Dadapter3 As OleDbDataAdapter = New OleDbDataAdapter
            Dadapter3.SelectCommand = New OleDbCommand("SELECT PRODCOD,PRODNOME FROM tbProd ORDER BY PRODNOME asc", New OleDbConnection(My.Settings.SPConecte))
            Dadapter3.Fill(DSet, "TBprod")
            ddlProd.DataSource = DSet.Tables("TBprod")
            ddlProd.DataTextField = ("PRODNOME")
            ddlProd.DataValueField = ("PRODCOD")
            ddlProd.DataBind()
            Dim Dadapter4 As OleDbDataAdapter = New OleDbDataAdapter
            Dadapter4.SelectCommand = New OleDbCommand("SELECT TIPOCOD,TIPONOME,PRODCOD FROM  tbTipo ORDER BY TIPONOME asc", New OleDbConnection(My.Settings.SPConecte))
            Dadapter4.Fill(DSet, "TBTipo")
    
    Protected Sub ddlProd_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlProd.SelectedIndexChanged
            Dim ItemDataView As New DataView(DSet.Tables("TBTipo"))
            ItemDataView.RowFilter = "PRODCOD = " & ddlProd.SelectedItem.Value
            ddlTipo.DataSource = ItemDataView
            ddlTipo.DataTextField = ("TIPONOME")
            ddlTipo.DataValueField = ("TIPOCOD")
            ddlTipo.DataBind()
    End Sub
    
    Private Sub inserir()
            Dim conecte As New OleDbConnection
            conecte.ConnectionString = My.Settings.SPConecte
            Try
                conecte.Open()
            Catch ex As System.Data.OleDb.OleDbException
                Label1.Text = "Erro de conexao com o Banco de Dados: " & ex.Message.ToString
                Return
            End Try
            Dim selcom As New System.Data.OleDb.OleDbCommand
            selcom.Connection = conecte
            selcom.CommandText = "INSERT INTO tbCadEndereco ( produto,tipo)" & _
       " VALUES('" & ddlProd.Text & "','" & ddlTipo.Text & "')"
            Dim teste As Boolean
            Try
                teste = selcom.ExecuteNonQuery
            Catch ex As System.Data.OleDb.OleDbException
                Label16.Text = "Erro: " & ex.Message.ToString
                Return
            End Try
            conecte.Close()
            Label2.Text = "Dados gravados com  sucesso"
    End Sub

    quarta-feira, 20 de junho de 2012 14:49

Respostas

  • Eu estabeleci parametros no comandText conforme abaixo, e correu tudo bem.

    " VALUES(?,?)"
            selcom.Parameters.Clear()
            selcom.Parameters.Add("@produto", OleDbType.VarChar, 100).Value = ddlProd.SelectedValue
            selcom.Parameters.Add("@tipo", OleDbType.VarChar, 100).Value = ddlTipo.SelectedValue

    • Marcado como Resposta Wilson Boris sexta-feira, 6 de julho de 2012 22:58
    quarta-feira, 20 de junho de 2012 18:44