Jump to content

Botão inserir insere dois registos no sql


maddox
 Share

Recommended Posts

Boa noite, estou com um grande problema e muito urgente também.

Criei um formulário no asp.net para inserir reservas de um hotel rural. Mas ao clicar no link de inserir, o código insere dois registos, uma linha com os dados preenchidos e outra vazia.

o código é o seguinte:

Imports System.Data.SqlClient
Imports System.Data

Partial Class casadelamas_reservas
    Inherits System.Web.UI.Page

    Protected Sub Insere_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ok.Click
        Dim conexao As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim ligacao As New SqlConnection(conexao)
        Dim cmdsql As String = "INSERT INTO reservas(nome) VALUES('" & frmnome.Text & "')"
        Dim cmd As New SqlCommand(cmdsql, ligacao)
        Try
            ligacao.Open()
            cmd.ExecuteReader()
            frmnome.Text = ""
        Catch ex As Exception
            'tratamento de erros
        Finally
            ligacao.Close()
            msg.Text = "Seus dados foram recebidos !"
        End Try
    End Sub
End Class

Espero que me possam ajudar o quanto antes.

Obrigado.

Link to comment
Share on other sites

Tenta assim:

Qualquer coisa apita. 

Codigo:

Dim conexao As New SqlConnection()
Dim comando As New SqlCommand("", conexao)

            conexao.ConnectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
            With comando
                Try
                    .Parameters.Clear()
                    .Parameters.AddWithValue("@ nome", frmnome.Text)
       
                    .Connection.Open()
                    .CommandText = "INSERT INTO reservas (Nome)" _
                    & "VALUES (@Nome) "

                    .CommandType = CommandType.Text
                    Try
                        .ExecuteNonQuery()

                    Catch ex As Exception

                        MsgBox(ex.Message)

                    End Try
                    .Connection.Close()
                    .Dispose()
                    comando.Dispose()

                Catch ex As Exception

                End Try
               
    
Link to comment
Share on other sites

Public Sub inserir_art(ByVal InstArt As casadelamas_reservas)

        Dim c As New ClassCon

        c.abrir()

        Dim cmdinserir As SqlCommand

        cmdinserir = New SqlCommand("INSERT INTO reservas(nome) VALUES('" & frmnome.Text & "')", c.conn)

        Try

            cmdinserir.ExecuteNonQuery()

        Catch ex As Exception

            'MsgBox(ex.Message, MsgBoxStyle.Information, "casadelamas_reservas")

        End Try

        c.Fechar()

    End Sub

cria uma class e mete este metodo! 🙂

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.