Results 1 to 4 of 4

Thread: Update (or complete) datagridview in certain cells

  1. #1

    Thread Starter
    Junior Member feroguz's Avatar
    Join Date
    Aug 2013
    Posts
    22

    Question 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:
    Name:  completar.jpg
Views: 382
Size:  54.7 KB

    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!!
    Last edited by feroguz; Sep 26th, 2013 at 05:56 PM.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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 ...

    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 ?

    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.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Junior Member feroguz's Avatar
    Join Date
    Aug 2013
    Posts
    22

    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..

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width