← Conversa Geral

ALGUEM QUE PERCEBA DE VB PARA ME AJUDAR!

Lida 835 vezes

Offline

cmtavares 
Membro
Mensagens 5 Gostos 0
Troféus totais: 12
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Level 2 Level 1 First Post Karma Fifth year Anniversary Fourth year Anniversary Third year Anniversary

Boas pessoal, gostava de saber qual e o motivo pelo qual nao consigo realizar o comando de update para a base de dados ! já criei a ligação da datagridview para a form mas mesmo assim continua sem dar para fazer o update!

CODIGO;

Código: [Seleccione]
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms


Public Class EDITCLIENTES

    Private dataGridView1 As New DataGridView()
    Private bindingSource1 As New BindingSource()
    Private dataAdapter As New SqlDataAdapter()

    Private Sub GetData(ByVal selectCommand As String)

        Try

            Dim connectionString As String = "Password=irpasimpo;Persist Security Info=True;User ID=SA;Initial Catalog=OEI;Data Source=CARLOS-M\SQL"

            Me.dataAdapter = New SqlDataAdapter(selectCommand, connectionString)

            Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)

            Dim table As New DataTable()
            table.Locale = System.Globalization.CultureInfo.InvariantCulture
            Me.dataAdapter.Fill(table)

        Catch ex As SqlException
            MessageBox.Show("To run this example, replace the value of the " + _
                "connectionString variable with a connection string that is " + _
                "valid for your system.")
        End Try
    End Sub

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.Run(New EDITCLIENTES())
    End Sub

    Private Sub EDITCLIENTES_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load

        Me.dataGridView1.DataSource = Me.bindingSource1
        GetData("select * Clientes")

        Label8.Visible = False
        Label9.Visible = False
        Label10.Visible = False

    End Sub

    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click

        'If dataGridView1.SelectedRows.Count > 0 Then
        '    Dim selectedIndex As Integer = dataGridView1.SelectedRows(0).Index

        '    Dim rowID As Integer = Integer.Parse(dataGridView1(0, selectedIndex).Value.ToString())

        '    Try

        '        Dim connectionString As String = "Password=irpasimpo;Persist Security Info=True;User ID=SA;Initial Catalog=OEI;Data Source=CARLOS-M\SQL"
        '        Dim connection As New SqlConnection(connectionString)
        '        connection.Open()
        '        Dim MySQL As String = "UPDATE FROM Clientes (Nome, Morada,Nacionalidade,Email,Descrição) values (@Nome, @Morada,@Nacionalidade,@Email,@Descrição)"
        '        Dim Cmd As New SqlCommand(MySQL, connection)
        '        Cmd.Parameters.AddWithValue("@Nome", txtNome.Text)
        '        'Cmd.Parameters.Add(New SqlParameter("@Morada", txtMorada.Text))
        '        'Cmd.Parameters.Add(New SqlParameter("@Nacionalidade", txtNacionalidade.Text))
        '        'Cmd.Parameters.Add(New SqlParameter("@Email", txtEmail.Text))
        '        'Cmd.Parameters.Add(New SqlParameter("@Descrição", txtDescricao.Text))
        '        Dim rows As Integer = Cmd.ExecuteNonQuery()

        '        Cmd.ExecuteNonQuery()
        '        connection.Close()


        '    Catch ex As SqlException
        '    End Try

        'End If
        Dim connectionString As String = "Password=irpasimpo;Persist Security Info=True;User ID=SA;Initial Catalog=OEI;Data Source=CARLOS-M\SQL"
        Dim connection As New SqlConnection(connectionString)


        If dataGridView1.SelectedRows.Count > 0 Then
            Dim intCLIENTESID As Integer = dataGridView1.SelectedRows(0).Cells("ID_CLIENTE").Value
            Dim selectedIndex As Integer = dataGridView1.SelectedRows(0).Index
            Dim rowID As Integer = Integer.Parse(dataGridView1(0, selectedIndex).Value.ToString())



            If Not connection.State = ConnectionState.Open Then
                connection.Open()
            End If

            Dim da As New SqlClient.SqlCommand("SELECT * FROM Clientes " & _
                                                 " WHERE ID_CLIENTE=" & intCLIENTESID, connection)
            Dim dt As New DataTable

            txtNome.Text = dt.Rows(0).Item("@Nome")
            txtMorada.Text = dt.Rows(0).Item("@Morada")
            txtNacionalidade.Text = dt.Rows(0).Item("@Nacionalidade")

            da.ExecuteNonQuery()

            connection.Close()
        End If

    End Sub
Offline

rtbfreitas 
Equipa
Mensagens 1497 Gostos 9
Feedback +24

Troféus totais: 30
Trófeus: (Ver todos)
Windows User Apple User Super Combination Combination Topic Starter 10 Poll Votes Poll Voter Poll Starter Level 5 Level 4

Pergunta antes no p@p (portugal-a-programar.pt)