Usuário com melhor resposta
Fazer um update no dataGridView (SOCORRO GALERA!!!)

Pergunta
-
Galera seguinte,
fiz todo o codigo na mao e utilizei apenas a estrutura do dataset mas quando eu faço uma atualizaçao ele me mostra esse erro no da.update :Não há suporte para geração SQL dinâmica para UpdateCommand em SelectCommand que não retorne informações importantes de coluna.
eis o codigo:
Imports System.Data.SqlClient Public Class ConsultaVendasAtual Inherits System.Windows.Forms.Form Private cb As SqlCommandBuilder Private da As SqlDataAdapter Private conexao As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True") Private sql As String = "SELECT * FROM Vendas_Antigo" Private ds As New DataSetGeral Public Sub ConsultaVendasAtual_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'DataSetGeral.Vendas_Atual' table. You can move, or remove it, as needed. ' Me.Vendas_AtualTableAdapter.Fill(Me.DataSetGeral.Vendas_Atual) ' Create a Command Dim SqlCommand = New SqlCommand(sql, conexao) ' Create SqlDataAdapter da = New SqlDataAdapter da.SelectCommand = SqlCommand ' Create SqlCommandBuilder object cb = New SqlCommandBuilder(da) ' Fill Dataset da.Fill(ds, "Vendas_Antigo") ' Bind the data to the grid at runtime DataGridViewVendasAtual.DataSource = (ds.Vendas_Antigo) End Sub Private Sub ConsultaVendasAtual_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If ds.HasChanges() Then Dim da As SqlDataAdapter Dim command_builder As SqlCommandBuilder ' Create the DataAdapter. da = New SqlDataAdapter(sql, conexao) ' Map Table to Vendas. da.TableMappings.Add("Table", "Vendas_Antigo") ' Make the CommandBuilder generate the ' insert, update, and delete commands. command_builder = New SqlCommandBuilder(da) ' Save the changes. da.Update(ds, "Vendas_Antigo") MessageBox.Show("Atualizado") End If End Sub End Class
mesmo manualmente continua dando erro... seria alguma configuração no banco sql será?
Respostas
-
Galera,
Seguinte: fiz uma gambiarra( heheh) e funcionou, vejam o procedimento q fiz:
1ª criei um dataset tipado e utilizei o wizard para criar os metodos
2º criei um form e arrastei do datasource direto pro formulario e entao foi criado automaticamente o grid, tableadapter,dataset ,databindings e bindingNavigator
3º não funcionou o update ( mas deveria né?)
4ºmontei um select na mão utilizando um dataAdapter via codigo vb.net
5º só com o codigo de select e update e os metodos do wizard do dataset desabilitados tbm não funcionou
6º juntei tudo! (metodos do wizard habilitados e codigos de consulta e update juntos
7º Funcionou ;=)
8º Alguem poderia me dizer pq eu tive que incrementar os codigos juntos com os metodos do wizard para editar e salvar o grid?
Segue o codigo Atual:
Muito obrigado desde já pela força
Gilson DiasImports System.Data.SqlClient Public Class ConsultaVendasAtual Inherits System.Windows.Forms.Form Private cb As SqlCommandBuilder Private da As SqlDataAdapter Private conexao As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True") Private sql As String = "SELECT * FROM Vendas_Antigo" Private ds As New DataSetGeral Public Sub ConsultaVendasAtual_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'DataSetGeral.Vendas_Atual' table. You can move, or remove it, as needed. ' Me.Vendas_AtualTableAdapter.Fill(Me.DataSetGeral.Vendas_Atual) ' Create a Command Dim SqlCommand = New SqlCommand(sql, conexao) ' Create SqlDataAdapter da = New SqlDataAdapter da.SelectCommand = SqlCommand ' Create SqlCommandBuilder object cb = New SqlCommandBuilder(da) ' Fill Dataset da.Fill(ds, "Vendas_Antigo") ' Bind the data to the grid at runtime DataGridViewVendasAtual.DataSource = (ds.Vendas_Antigo) End Sub Private Sub ConsultaVendasAtual_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If ds.HasChanges() Then Dim da As SqlDataAdapter Dim command_builder As SqlCommandBuilder ' Create the DataAdapter. da = New SqlDataAdapter(sql, conexao) ' Map Table to Vendas. da.TableMappings.Add("Table", "Vendas_Antigo") ' Make the CommandBuilder generate the ' insert, update, and delete commands. command_builder = New SqlCommandBuilder(da) ' Save the changes. da.Update(ds, "Vendas_Antigo") MessageBox.Show("Atualizado") End If End Sub Private Sub Vendas_AntigoBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Vendas_AntigoBindingNavigatorSaveItem.Click Me.Validate() Me.Vendas_AntigoBindingSource.EndEdit() Me.Vendas_AntigoTableAdapter.Update(Me.DataSetGeral.Vendas_Antigo) End Sub End Class
- Marcado como Resposta tecnico.gilson quarta-feira, 16 de dezembro de 2009 20:51