1 Attachment(s)
Update (or complete) datagridview in certain cells
Hi, good day!!
I have a datagridview and I want to add values (I have one combobox, 1 textbox and 2 datapicker).
But I want to add the values in the selected row, but I make mistakes.
I appreciate any help in this difficult task:
Attachment 105203
Code:
Dim estatus, notas, fecha_comp, fecha_cierre As String
Dim i As Integer
Dim conn As New MySqlConnection("Data Source=192.168.1.3;Database=mantenimiento;User ID=MGUZ;Password=Asdf1234;")
conn.Open()
Dim query As String = "UPDATE mantenimiento SET Estatus = ?estatus,Notas = ?notas,Fecha_comp = ?fecha_comp,fecha_cierre =?fecha_cierre" & "WHERE ID =" & txt_caso.Text & "')"
estatus = DataGridView1.Rows.Item(i).Cells(8).Value
notas = DataGridView1.Rows.Item(i).Cells(9).Value
fecha_comp = DataGridView1.Rows.Item(i).Cells(10).Value
fecha_cierre = DataGridView1.Rows.Item(i).Cells(11).Value
Dim cmd As MySqlCommand = New MySqlCommand(query, conn)
cmd.Parameters.AddWithValue("?estatus", estatus)
cmd.Parameters.AddWithValue("?notas", notas)
cmd.Parameters.AddWithValue("?fecha_comp", fecha_comp)
cmd.Parameters.AddWithValue("?fecha_cierre", fecha_cierre)
cmd.ExecuteNonQuery()
conn.Close()
Best Regards!! :)
Re: Update (or complete) datagridview in certain cells
Well the first and most obvious question is why you are using this method at all if you have a databound DataGridView? But as to mistakes in what you've got ...
Quote:
Dim query As String = "UPDATE mantenimiento SET Estatus = ?estatus,Notas = ?notas,Fecha_comp = ?fecha_comp,fecha_cierre =?fecha_cierre" & "WHERE ID =" & txt_caso.Text & "')"
The construction in the bolded section does not provide the necessary space between the parameter and WHERE. Although it's not essential, there should really be spaces after the commas in the rest of the statement also. I'm confused as to why having elected to use parameters :) you've not done so for txt_caso.Text :( By convention, parameters are usually prefixed with @ not ?
Quote:
estatus = DataGridView1.Rows.Item(i).Cells(8).Value
This makes no sense on any level. The intermediary variable estatus isn't necessary to begin with. It should be DataGridView1.Rows(i).Cells(8).Value but, as i = 0 at this point, this is probably not the actual value you require anyway.
Re: Update (or complete) datagridview in certain cells
Ok, sorry, I'm not an expert.
These are my first developments of this level.
When I do the Load, and I have information from my datagridview.
All I sequire is to add information (Those that I have selected in the image) in them MySQL.
I figured it would be good to add first:
Code:
Dim i As Integer
i = DataGridView1.CurrentRow.Index
With DataGridView1.Rows
. Item (i). Cells (8). Value = cmb_estatus.Text
. Item (i). Cells (9). Value = txt_notas.Text
. Item (i). Cells (10). Value = DateTimePicker1.Text
. Item (i). Cells (11). Value = DateTimePicker2.Text
End With
And then up the whole entire table with columns that were modified, but not know how to do this process ..
Could you help me sr??
Best Regards.. :)
Re: Update (or complete) datagridview in certain cells
I recommend that you have a fairly detailed read of this tutorial thread which covers pretty much everything there is to know about databases in a practical way. If you're still not sure of anything then just let us know.