Luis Marques Posted April 8, 2009 at 12:09 PM Report #255861 Posted April 8, 2009 at 12:09 PM Boas, tou com um problema no update que é o seguinte. Tenho vários itens por exemplo o item 1, item 2, item 3, e ao actulizar apenas vai gravar o item 2 , item 3... Ou seja o item com o valor mais baixo nunca grava. Deixo aqui o codigo que tenho para fazer o update Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'alterar na tabela encomendas Dim intEncomenda As Integer = ToNumber(Id_encomendaTextBox.Text) Dim intCliente As Integer = ToNumber(Id_clienteTextBox.Text) Dim dtData As String = ToDbdate(DataDateTimePicker.Value) Dim intdescricao As String = ToStringA(TextBox2.Text) Dim strResp As String = ToStringA(Responsavel_pdTextBox.Text) Dim dblTotal As Object = ToNumber(TotalTextBox.Text) Dim strSql As String = "UPDATE encomenda SET id_cliente=" & intCliente & ",data=" & dtData & ",descricao=" & intdescricao & ",responsavel_pd=" & strResp & ",total=" & dblTotal & " where [id_encomenda]=" & intEncomenda & "" OleDBExecute(strSql) 'alterar na tabela item encomendas For i = 0 To DataGridView1.Rows.Count - 2 ' para correr todas as linhas Dim intEncomendas As Integer = ToNumber(nz(Id_encomendaTextBox.Text, 0)) Dim intid_item As Integer = ToNumber(nz(DataGridView1.Rows(i).Cells(0).Value, 0)) Dim intidproduto As Integer = ToNumber(nz(DataGridView1.Rows(i).Cells(1).Value, 0)) Dim intquantidade As Integer = ToNumber(nz(DataGridView1.Rows(i).Cells(2).Value, 0)) Dim intpreco As Object = ToNumber(nz(DataGridView1.Rows(i).Cells(3).Value, 0)) Dim intid_imagem As Integer = ToNumber(nz(DataGridView1.Rows(i).Cells(4).Value, 0)) strSql = "UPDATE itemencomenda SET id_encomenda=" & intEncomendas & ",id_item=" & intid_item & ",id_produto=" & intidproduto & ",quantidade=" & intquantidade & ",preco=" & intpreco & " ,id_imagem=" & intid_imagem & " where [id_encomenda]=" & intEncomenda & "" OleDBExecute(strSql) Next End Sub Public Function OleDBExecute(ByVal strSql As String) As Integer Using connection As New OleDbConnection(pap.My.Settings.ligacaoBD) Dim cmd As New OleDbCommand(strSql, connection) cmd.CommandType = CommandType.Text connection.Open() Try Dim i As Integer = cmd.ExecuteNonQuery() Return i Catch ex As OleDb.OleDbException MsgBox(ex.Message) End Try End Using End Function LM
ricardoantunes Posted April 8, 2009 at 01:04 PM Report #255869 Posted April 8, 2009 at 01:04 PM porque é que estas a fazer: For i = 0 To DataGridView1.Rows.Count - 2 não pode ser: For i = 0 To DataGridView1.Rows.Count - 1 ?
Luis Marques Posted April 8, 2009 at 05:39 PM Author Report #255929 Posted April 8, 2009 at 05:39 PM com 1 dá um errito, com 2 funciona bem. LM
esquima Posted April 8, 2009 at 07:28 PM Report #255947 Posted April 8, 2009 at 07:28 PM tenta assim For Each row As DataGridViewRow In Me.DataGridView1.Rows Dim intEncomendas As Integer = ToNumber(nz(Id_encomendaTextBox.Text, 0)) Dim intid_item As Integer = ToNumber(nz( row.Cells(0).Value , 0)) Dim intidproduto As Integer = ToNumber(nz( row.Cells(1).Value , 0)) Dim intquantidade As Integer = ToNumber(nz( row.Cells(2).Value , 0)) Dim intpreco As Object = ToNumber(nz( row.Cells(3).Value , 0)) Dim intid_imagem As Integer = ToNumber(nz( row.Cells(4).Value , 0)) strSql = "UPDATE itemencomenda SET id_encomenda=" & intEncomendas & ",id_item=" & intid_item & ",id_produto=" & intidproduto & ",quantidade=" & intquantidade & ",preco=" & intpreco & " ,id_imagem=" & intid_imagem & " where [id_encomenda]=" & intEncomenda & "" OleDBExecute(strSql) Next
Luis Marques Posted April 8, 2009 at 07:37 PM Author Report #255949 Posted April 8, 2009 at 07:37 PM Já exprimentei, mas continua a dar barraca!! LM
esquima Posted April 8, 2009 at 07:40 PM Report #255950 Posted April 8, 2009 at 07:40 PM fez o update em todos menos no valor mais baixo?
Luis Marques Posted April 8, 2009 at 07:44 PM Author Report #255952 Posted April 8, 2009 at 07:44 PM Sim grava, o valor mais baixo do item é que nao grava. Ou seja só grava o item 7,8 em vez de gravar o item 6,7,8. LM
esquima Posted April 8, 2009 at 07:50 PM Report #255953 Posted April 8, 2009 at 07:50 PM ja viste em debug qtas vezes e que ele faz o for e os resultados das variaveis k vai para o update
Luis Marques Posted April 8, 2009 at 07:59 PM Author Report #255955 Posted April 8, 2009 at 07:59 PM Fiz agora o debug e faz o for 3 vezes, ou seja para os 3 itens k tenho. Não percebo porque está a dar mal, já que passa todos os itens. LM
esquima Posted April 8, 2009 at 08:02 PM Report #255956 Posted April 8, 2009 at 08:02 PM o intEncomenda vai sempre com valor das 3vezes..confirma ja agora connection.close() depois do return i
Luis Marques Posted April 8, 2009 at 08:06 PM Author Report #255957 Posted April 8, 2009 at 08:06 PM esquecime de dizer que com este codigo que deste nao passa os valores todos dos items, com aquele que eu tinha é que passam os valores todos LM
esquima Posted April 8, 2009 at 08:11 PM Report #255959 Posted April 8, 2009 at 08:11 PM For i = 0 To DataGridView1.Rows.Count - 2--com isto o teu for corre 3x,eu axo k nao
Luis Marques Posted April 8, 2009 at 08:11 PM Author Report #255960 Posted April 8, 2009 at 08:11 PM upss... tenho de corrigir o que disse, ja estava a fazer confusao. O TEU CODIGO É QUE ESTÁ A PASSAR TODOS OS ITEMS, o meu é que nao!xD LM
Luis Marques Posted April 8, 2009 at 08:14 PM Author Report #255961 Posted April 8, 2009 at 08:14 PM Isso corre os items todos, so que depois o item mais baixo não passar, ou seja logo o primeiro item LM
esquima Posted April 8, 2009 at 08:18 PM Report #255963 Posted April 8, 2009 at 08:18 PM Dim i As Integer = cmd.ExecuteNonQuery() Return i ve o que devolve este i das 3vezes que vai la
Luis Marques Posted April 8, 2009 at 08:30 PM Author Report #255965 Posted April 8, 2009 at 08:30 PM devolve sempre 0 LM
jpaulino Posted April 8, 2009 at 08:38 PM Report #255967 Posted April 8, 2009 at 08:38 PM Ontem já te mostrei como fazer um ciclo numa datagridview. Qual é o problema? For Each row As DataGridViewRow In Me.DataGridView1.Rows If Not row.IsNewRow Then Debug.WriteLine(row.Cells(0).Value) End If Next
José Lopes Posted April 8, 2009 at 10:33 PM Report #255994 Posted April 8, 2009 at 10:33 PM o problema é mesmo uma estrutura de dados... que está mal feita... por melhor que esteja o código... Quando te pedirem peixe.... ensina-os a Pescar!!Hum..lálálálá!!
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