In a form I am performing the UPDATE of a record in the DB. To do this:
- I select a record from a Datagreed-
- I insert the data in fields to be able to modify them. Along with this I pass the data to an arrangement.
- Then I modify the fields that I need and I click on a "RECORD" button
- I compare the fields with those in the array and so I know which ones to update and which ones I do not.
The problem is generated when comparing some fields, since not having been modified they should not be added to the query, and if it is happening.
Then I leave the code.
Here I assign the values to the text fields and store the values in the array.
Private Sub gvRegistros_DoubleClick(sender As Object, e As EventArgs) Handles gvRegistros.DoubleClick
accion = "UPDATE"
gb_registro.Enabled = True
'Valido el ID del registro a actualizar!
If Not gvRegistros.CurrentRow.Cells(0).Value.Equals("") Then
idToUpdate = gvRegistros.CurrentRow.Cells(0).Value.ToString() 'Lo asigno a una variable global.
camposToUpdate(21) = idToUpdate.ToString() ' ID
End If
lb_numreg.Text = gvRegistros.CurrentRow.Cells(1).Value.ToString()
If IsNothing(gvRegistros.CurrentRow.Cells(2).Value) Then
cb_un.Text = ""
camposToUpdate(0) = "" ' UN
Else
cb_un.Text = Convert.ToString(gvRegistros.CurrentRow.Cells(2).Value)
camposToUpdate(0) = Convert.ToString(gvRegistros.CurrentRow.Cells(2).Value)
End If
If IsNothing(gvRegistros.CurrentRow.Cells(3).Value) Then
dtp_fecha.Text = ""
camposToUpdate(1) = "" ' FECRET
Else
dtp_fecha.Text = Convert.ToDateTime(gvRegistros.CurrentRow.Cells(3).Value)
camposToUpdate(1) = Convert.ToDateTime(gvRegistros.CurrentRow.Cells(3).Value)
End If
If IsNothing(gvRegistros.CurrentRow.Cells(4).Value) Then
cb_instalacion.Text = ""
camposToUpdate(2) = "" ' INST
Else
cb_instalacion.Text = gvRegistros.CurrentRow.Cells(4).Value.ToString()
camposToUpdate(2) = gvRegistros.CurrentRow.Cells(4).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(5).Value) Then
txt_direccion.Text = ""
camposToUpdate(3) = "" ' DIRECC
Else
txt_direccion.Text = gvRegistros.CurrentRow.Cells(5).Value.ToString()
camposToUpdate(3) = gvRegistros.CurrentRow.Cells(5).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(6).Value) Then
txt_comuna.Text = ""
camposToUpdate(4) = "" ' COMUNA
Else
txt_comuna.Text = gvRegistros.CurrentRow.Cells(6).Value.ToString()
camposToUpdate(4) = gvRegistros.CurrentRow.Cells(6).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(7).Value) Then
txt_ppu.Text = ""
camposToUpdate(5) = "" ' PPU
Else
txt_ppu.Text = gvRegistros.CurrentRow.Cells(7).Value.ToString()
camposToUpdate(5) = gvRegistros.CurrentRow.Cells(7).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(8).Value) Then
txt_buses.Text = ""
camposToUpdate(6) = "" ' BUSOPE
Else
txt_buses.Text = gvRegistros.CurrentRow.Cells(8).Value.ToString()
camposToUpdate(6) = gvRegistros.CurrentRow.Cells(8).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(9).Value) Then
txt_comparacion.Text = ""
camposToUpdate(7) = "" ' COMPRO
Else
txt_comparacion.Text = gvRegistros.CurrentRow.Cells(9).Value.ToString()
camposToUpdate(7) = gvRegistros.CurrentRow.Cells(9).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(10).Value) Then
txt_marca.Text = ""
camposToUpdate(8) = "" ' MARCA
Else
txt_marca.Text = gvRegistros.CurrentRow.Cells(10).Value.ToString()
camposToUpdate(8) = gvRegistros.CurrentRow.Cells(10).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(11).Value) Then
txt_modelo.Text = ""
camposToUpdate(9) = "" ' MODELO
Else
txt_modelo.Text = gvRegistros.CurrentRow.Cells(11).Value.ToString()
camposToUpdate(9) = gvRegistros.CurrentRow.Cells(11).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(12).Value) Then
txt_ano.Text = ""
camposToUpdate(10) = "" ' ANO
Else
txt_ano.Text = gvRegistros.CurrentRow.Cells(12).Value.ToString()
camposToUpdate(10) = gvRegistros.CurrentRow.Cells(12).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(13).Value) Then
txt_tipveh.Text = ""
camposToUpdate(11) = "" ' TIPVEH
Else
txt_tipveh.Text = gvRegistros.CurrentRow.Cells(13).Value.ToString()
camposToUpdate(11) = gvRegistros.CurrentRow.Cells(13).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(14).Value) Then
txt_norma.Text = ""
camposToUpdate(12) = "" ' NORMA
Else
txt_norma.Text = gvRegistros.CurrentRow.Cells(14).Value.ToString()
camposToUpdate(12) = gvRegistros.CurrentRow.Cells(14).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(15).Value) Then
txt_filtrofab.Text = ""
camposToUpdate(13) = "" ' FABINC
Else
txt_filtrofab.Text = gvRegistros.CurrentRow.Cells(15).Value.ToString()
camposToUpdate(13) = gvRegistros.CurrentRow.Cells(15).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(16).Value) Or gvRegistros.CurrentRow.Cells(16).Value.Equals("") Or gvRegistros.CurrentRow.Cells(16).Value.Equals("N/A") Or gvRegistros.CurrentRow.Cells(17).Value.Equals("-") Or Not gvRegistros.CurrentRow.Cells(16).Value.Contains("-") Then
dtp_instfiltro.Text = DateTime.Now.ToShortDateString()
camposToUpdate(14) = DateTime.Now.ToShortDateString() ' FILINC
Else
dtp_instfiltro.Text = Convert.ToDateTime(gvRegistros.CurrentRow.Cells(16).Value)
camposToUpdate(14) = Convert.ToDateTime(gvRegistros.CurrentRow.Cells(16).Value)
End If
If IsNothing(gvRegistros.CurrentRow.Cells(17).Value) Then
txt_marcafiltro.Text = ""
camposToUpdate(15) = "" ' MARFIL
Else
txt_marcafiltro.Text = gvRegistros.CurrentRow.Cells(17).Value.ToString()
camposToUpdate(15) = gvRegistros.CurrentRow.Cells(17).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(18).Value) Then
cb_condicion.Text = ""
camposToUpdate(16) = "" ' CONDIC
Else
cb_condicion.Text = gvRegistros.CurrentRow.Cells(18).Value.ToString()
camposToUpdate(16) = gvRegistros.CurrentRow.Cells(18).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(19).Value) Then
txt_observaciones.Text = ""
camposToUpdate(17) = "" ' OBSERV
Else
txt_observaciones.Text = gvRegistros.CurrentRow.Cells(19).Value.ToString()
camposToUpdate(17) = gvRegistros.CurrentRow.Cells(19).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(20).Value) Then
txt_infoextra.Text = ""
camposToUpdate(18) = "" ' EXTINF
Else
txt_infoextra.Text = gvRegistros.CurrentRow.Cells(20).Value.ToString()
camposToUpdate(18) = gvRegistros.CurrentRow.Cells(20).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(21).Value) Then
txt_responsable.Text = ""
camposToUpdate(19) = "" ' RESPON
Else
txt_responsable.Text = gvRegistros.CurrentRow.Cells(21).Value.ToString()
camposToUpdate(19) = gvRegistros.CurrentRow.Cells(21).Value.ToString()
End If
If IsNothing(gvRegistros.CurrentRow.Cells(22).Value) Then
txt_conductor.Text = ""
camposToUpdate(20) = "" ' CONDUC
Else
txt_conductor.Text = gvRegistros.CurrentRow.Cells(22).Value.ToString()
camposToUpdate(20) = gvRegistros.CurrentRow.Cells(22).Value.ToString()
End If
GRABARToolStripMenuItem.Enabled = True
End Sub
Then on the record button, I see if I insert a new record or if I update one. If action="UPDATE", I update.
Private Sub GRABARToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GRABARToolStripMenuItem.Click
Dim query As String
FormLogin.txt_password.Text = ""
FormLogin.ShowDialog()
If accion.Equals("INSERT") Then
query = crearQueryInsert()
If query IsNot Nothing Then
SConexion.Open()
Dim action_ As New SqlCommand(query, SConexion)
action_.ExecuteNonQuery()
MsgBox("Acción finalizada exitosamente.")
SConexion.Close()
Else
MsgBox("Inserción Fallida", MsgBoxStyle.Exclamation)
Exit Sub
End If
ElseIf accion.Equals("UPDATE") Then
query = crearQueryUpdate(camposToUpdate)
Else
MsgBox("Realice un acción antes de grabar.")
Exit Sub
End If
End Sub
Then in the "createQueryUpdate" method I compare the values and create the query.
Private Function crearQueryUpdate(ByVal ParamArray arr() As String) As String
Dim query As String = ""
query = "UPDATE Registro SET "
If String.Compare(arr(0), cb_un.Text) > 0 Or String.Compare(arr(0), cb_un.Text) < 0 Then
query += "UN = '" + arr(0) + "', " ' comparando UN
End If
If String.Compare(arr(1), Convert.ToString(dtp_fecha.Value)) > 0 Or String.Compare(arr(1), Convert.ToString(dtp_fecha.Value)) < 0 Then
query += "FECRET = '" + arr(1) + "', " ' comparando FECHA
End If
If String.Compare(arr(2), cb_instalacion.Text) > 0 Or String.Compare(arr(2), cb_instalacion.Text) < 0 Then
query += "INST = '" + arr(2) + "', " ' comparando COD INSTALACION
End If
If String.Compare(arr(3), txt_direccion.Text) > 0 Or String.Compare(arr(3), txt_direccion.Text) < 0 Then
query += "DIRECC = '" + arr(3) + "', " ' comparando DIRECCION
End If
If String.Compare(arr(4), txt_comuna.Text) > 0 Or String.Compare(arr(4), txt_comuna.Text) < 0 Then
query += "COMUNA = '" + arr(4) + "', " ' comparando COMUNA
End If
If String.Compare(arr(5), txt_ppu.Text) > 0 Or String.Compare(arr(5), txt_ppu.Text) < 0 Then
query += "PPU = '" + arr(5) + "', " ' comparando PPU
End If
If String.Compare(arr(6), txt_buses.Text) > 0 Or String.Compare(arr(6), txt_buses.Text) < 0 Then
query += "BUSOPE = '" + arr(6) + "', " ' comparando BUSES OPERATIVOS
End If
If String.Compare(arr(7), txt_comparacion.Text) > 0 Or String.Compare(arr(7), txt_comparacion.Text) < 0 Then
query += "COMPRO = '" + arr(7) + "', " ' comparando COMPROBACIÓN
End If
If String.Compare(arr(8), txt_marca.Text) > 0 Or String.Compare(arr(8), txt_marca.Text) < 0 Then
query += "MARCA = '" + arr(8) + "', " ' comparando MARCA
End If
If String.Compare(arr(9), txt_modelo.Text) > 0 Or String.Compare(arr(9), txt_modelo.Text) < 0 Then
query += "MODELO = '" + arr(9) + "' " ' comparando MODELO
End If
If String.Compare(arr(10), txt_ano.Text) > 0 Or String.Compare(arr(10), txt_ano.Text) < 0 Then
query += "ANO = '" + arr(10) + "', " ' comparando AÑO
End If
If String.Compare(arr(11), txt_tipveh.Text) > 0 Or String.Compare(arr(11), txt_tipveh.Text) < 0 Then
query += "TIPVEH = '" + arr(11) + "', " ' comparando TIPO VEHICULO
End If
If String.Compare(arr(12), txt_norma.Text) > 0 Or String.Compare(arr(12), txt_norma.Text) < 0 Then
query += "NORMA = '" + arr(12) + "', " ' comparando NORMA
End If
If String.Compare(Convert.ToString(arr(13)), Convert.ToString(txt_filtrofab.Text)) > 0 Or String.Compare(Convert.ToString(arr(13)), Convert.ToString(txt_filtrofab.Text)) < 0 Then
query += "FABINC = '" + arr(13) + "', " ' comparando FILTRO FAB INC
End If
If String.Compare(arr(14), Convert.ToString(dtp_instfiltro.Value)) > 0 Or String.Compare(arr(14), Convert.ToString(dtp_instfiltro.Value)) < 0 Then
query += "FILINC = '" + arr(14) + "', " ' comparando FECHA INST. FILTRO
End If
If String.Compare(arr(15), txt_marcafiltro.Text) > 0 Or String.Compare(arr(15), txt_marcafiltro.Text) < 0 Then
query += "MARFIL = '" + arr(15) + "', " ' comparando MARCA FILTRO
End If
If String.Compare(arr(16), cb_condicion.Text) > 0 Or String.Compare(arr(16), cb_condicion.Text) < 0 Then
query += "CONDIC = '" + arr(16) + "', " ' comparando CONDICION
End If
If String.Compare(arr(17), txt_observaciones.Text) > 0 Or String.Compare(arr(17), txt_observaciones.Text) < 0 Then
query += "OBSERV = '" + arr(17) + "', " ' comparando OBSERVACIONES
End If
If String.Compare(arr(18), txt_infoextra.Text) > 0 Or String.Compare(arr(18), txt_infoextra.Text) < 0 Then
query += "EXTINF = '" + arr(18) + "', " ' comparando INFORMACIÓN EXTRA
End If
If String.Compare(arr(19), txt_responsable.Text) > 0 Or String.Compare(arr(19), txt_responsable.Text) < 0 Then
query += "RESPON = '" + arr(19) + "' " ' comparando RESPONSABLE
End If
If String.Compare(arr(20), txt_conductor.Text) > 0 Or String.Compare(arr(20), txt_conductor.Text) < 0 Then
query += "CONDUC = '" + arr(20) + "', " ' comparando CONDUCTOR
End If
'query = query.TrimEnd(", ")
If query.Length > 2 Then
query = Convert.ToString(query.Remove(query.Length - 2))
query += " WHERE ID = '" + Convert.ToString(idToUpdate) + "' "
Else
query = ""
End If
MsgBox(query)
'Return query
End Function
I hope you can help me.
Greetings.