maddox Posted November 28, 2009 at 02:39 AM Report Share #297886 Posted November 28, 2009 at 02:39 AM 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 More sharing options...
renafi Posted November 28, 2009 at 07:47 PM Report Share #297939 Posted November 28, 2009 at 07:47 PM No objecto cmd, experimenta fazer cmd.ExecuteNonQuery, em vez de cmd.ExecuteReader Oracle Certified Professional - AdministraçãoOracle Certified Professional - Pl/sqlMCPD - Microsoft Certified Professional DeveloperMCTS - Microsoft Certified Technology Specialist Link to comment Share on other sites More sharing options...
paulo silva Posted December 4, 2009 at 12:31 PM Report Share #298689 Posted December 4, 2009 at 12:31 PM 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 More sharing options...
Lisboeta Posted April 16, 2010 at 11:19 AM Report Share #321912 Posted April 16, 2010 at 11:19 AM 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now