Jump to content
Gil Pedrosa

Limpar datatable

Recommended Posts

Gil Pedrosa

Ora viva!

Tenho o seguinte código e agradecia imenso a vossa ajuda.

Como consigo limpar a minha data table cada vez que clico no botão? o nome dela é ntabela2

   

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim constring As String
        constring = "Data Source=" & Form1.ComboBox1.Text & ";Initial Catalog=" & Label1.Text & ";Integrated Security=true"

        Using conexao As New SqlClient.SqlConnection(constring)
            Dim sqlcomm1 As New SqlCommand("SELECT NIF FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm2 As New SqlCommand("SELECT ID_Cliente FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm3 As New SqlCommand("SELECT Nome FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm4 As New SqlCommand("SELECT Morada FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm5 As New SqlCommand("SELECT Cod_Postal FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm6 As New SqlCommand("SELECT Localidade FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm7 As New SqlCommand("SELECT Contacto1 FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm8 As New SqlCommand("SELECT Contacto2 FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm9 As New SqlCommand("SELECT E_Mail FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm10 As New SqlCommand("SELECT Obs FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)

            

            conexao.Open()
            Try


                Dim resultado1 As String = sqlcomm1.ExecuteScalar.ToString
                NIFTextBox.Text = resultado1
                Dim resultado2 As String = sqlcomm2.ExecuteScalar.ToString
                ID_ClienteTextBox.Text = resultado2
                Dim resultado3 As String = sqlcomm3.ExecuteScalar.ToString
                NomeTextBox.Text = resultado3
                Dim resultado4 As String = sqlcomm4.ExecuteScalar.ToString
                MoradaTextBox.Text = resultado4
                Dim resultado5 As String = sqlcomm5.ExecuteScalar.ToString
                Cod_PostalTextBox.Text = resultado5
                Dim resultado6 As String = sqlcomm6.ExecuteScalar.ToString
                LocalidadeTextBox.Text = resultado6
                Dim resultado7 As String = sqlcomm7.ExecuteScalar.ToString
                Contacto1TextBox.Text = resultado7
                Dim resultado8 As String = sqlcomm8.ExecuteScalar.ToString
                Contacto2TextBox.Text = resultado8
                Dim resultado9 As String = sqlcomm9.ExecuteScalar.ToString
                E_MailTextBox.Text = resultado9
                Dim resultado10 As String = sqlcomm10.ExecuteScalar.ToString
                ObsTextBox.Text = resultado10

            Catch ex As Exception
                MsgBox("O Cliente " & TextBox1.Text & " não existe.", MsgBoxStyle.Information, "Erro de pesquisa!")
            End Try
        End Using

        Using conexao1 As New SqlClient.SqlConnection(constring)
            Dim sqlcomm11 As New System.Data.SqlClient.SqlDataAdapter("SELECT Extra2 FROM Reserv Where Extra5='" & TextBox1.Text & "'", conexao1)

            conexao1.Open()

            sqlcomm11.Fill(nTabela2)
            conexao1.Close()


            For Each [u]tLinhas2[/u] As DataRow In [u]nTabela2[/u].Rows
                ListBox2.Items.Add(tLinhas2("Extra2").ToString())

            Next

            Try
                ListBox2.SelectedIndex = 0
                If ListBox2.SelectedIndex = 0 Then
                    Extra1TextBox.Text = ListBox2.SelectedItem.ToString
                End If

                ListBox2.SelectedIndex = 1
                If ListBox2.SelectedIndex = 1 Then
                    Extra2TextBox.Text = ListBox2.SelectedItem.ToString
                End If

                ListBox2.SelectedIndex = 2
                If ListBox2.SelectedIndex = 2 Then
                    Extra4TextBox.Text = ListBox2.SelectedItem.ToString
                End If

                ListBox2.SelectedIndex = 3
                If ListBox2.SelectedIndex = 3 Then
                    Extra5TextBox.Text = ListBox2.SelectedItem.ToString
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Using
    End Sub

Obrigado!

Share this post


Link to post
Share on other sites
Andrepereira9

Boas

Isso não é nenhuma duvida de Visual Basic for Applications (VBA).

Isso é VB.Net

Vou pedir ao Moderador para mover para a secção de VB.Net

Em relação à tua dúvida, experimenta

ntabela2.Clear()


A informática chegou para resolver problemas que antes não existiam

Quem ri por último é porque está conectado a 52 Kbs.

Share this post


Link to post
Share on other sites
bioshock

:nono:

 Dim sqlcomm1 As New SqlCommand("SELECT NIF FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm2 As New SqlCommand("SELECT ID_Cliente FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm3 As New SqlCommand("SELECT Nome FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm4 As New SqlCommand("SELECT Morada FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm5 As New SqlCommand("SELECT Cod_Postal FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm6 As New SqlCommand("SELECT Localidade FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm7 As New SqlCommand("SELECT Contacto1 FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm8 As New SqlCommand("SELECT Contacto2 FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm9 As New SqlCommand("SELECT E_Mail FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)
            Dim sqlcomm10 As New SqlCommand("SELECT Obs FROM Clientes Where Nome='" & TextBox1.Text & "'", conexao)

;)

 Dim query As String = "SELECT * FROM Clientes WHERE Nome = @Nome"

Dim command As New SqlCommand(query, connection)
command.Parameters.Add("@Nome", SqlDbType.Varchar).Value = Textbox1.Text

Dim DSet As New DataSet()
Dim DAdapter As New SqlDataAdapter(command, connection)
DAdapter.Fill(DSet, "Clientes")

NIFTextbox.Text = DSet.Tables(0).Rows(0).Item(0).ToString
IDClienteTextbox.Text = DSet.Tables(0).Rows(0).Item(1).ToString
NomeTextbox.Text = DSet.Tables(0).Rows(0).Item(2).ToString
'(...)

Share this post


Link to post
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

×
×
  • 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.