Results 1 to 9 of 9

Thread: Updating database with datagridview changes

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    14

    Updating database with datagridview changes

    I have datagridview filled from database through datadapter. The problem is that I cant use generated Fill, Update and Delete commands since in my SELECT query there are two joined tables. I need some direction how to achieve this. I will like to update changes in CellEndEdit procedure. Should I fill datagridview with two SELECT statements without joined tables? Or is there some simplest way to do it?

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

    Re: Updating database with datagridview changes

    Joining tables shouldn't make any difference as long as you update to exactly the same combination as you originally filled the DGV with the binding should take care of assigning the new values correctly.
    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
    New Member
    Join Date
    Oct 2009
    Posts
    14

    Re: Updating database with datagridview changes

    If I'm updating database form datagridview chagnes with da.update(dt) Im getting error
    Dynamic SQL generation is not supported against multiple base table.
    So why is this troubling me so much?

    Code:
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'Baza_podatkovDataSet.narocilo' table. You can move, or remove it, as needed.
            Dim CONNECT_STRING As String = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" & System.AppDomain.CurrentDomain.BaseDirectory & "baza_podatkov.mdb"
            ' "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" & System.AppDomain.CurrentDomain.BaseDirectory & "baza_podatkov.mdb"
           
     Dim cnn As New OleDbConnection(CONNECT_STRING)
            cnn.Open()
    
         
    Dim sql As String = "SELECT item.item_number, item_name, order.quantity,order.notes FROM (order INNER JOIN item ON order.item_id = item.ID) WHERE(order.ID_customer = " & Form2.value & ")"
    
    Dim cmd As New OleDbCommand(sql, cnn)
    
    da = New OleDbDataAdapter(cmd)
          
    da.Fill(ds, "item")
    DataGridView1.DataSource = ds.Tables("item").DefaultView
    
            cnn.Close()
    
    
    End Sub

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

    Re: Updating database with datagridview changes

    Your problem is in trying to update single values. A Where clause is invalid in joined tables. As I said you need to make the identical selection and then update the whole lot from the dgv bound table..
    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!

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    14

    Re: Updating database with datagridview changes

    I see... since this is my second training app, there is so much to learn. What I've tried is:

    Code:
    Dim sql As String = "UPDATE item, order SET item.item_number = @item_number,   item.item_name = @item_name, order.quantity = @quantity, order.notes  = @notes FROM (order INNER JOIN item ON order.ID = item.ID)"
    Dim sql As String = "SELECT item.item_number, item_name, order.quantity,order.notes FROM (order INNER JOIN item ON order.item_id = item.ID) WHERE(order.ID_customer = " & Form2.value & ")"

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    14

    Re: Updating database with datagridview changes

    I see... since this is my second training app, there is so much to learn. What I've tried is:

    Code:
    Dim sql As String = "UPDATE item, order SET item.item_number = @item_number, item.item_name = @item_name, order.quantity = @quantity, order.notes  = @notes FROM (order INNER JOIN item ON order.ID = item.ID)"
    But I'm getting missing operator error...ty once more...

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

    Re: Updating database with datagridview changes

    Firstly my apologies as I misled you somewhat believing that a mass update was possible when it turns out not to be. So back to the old style but with some changes which are entirely peculiar to Access databases (or rather OleDB). Example with notes ...

    vb.net Code:
    1. Public Class Form1
    2.     Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Joiner.mdb")
    3.     Dim ds As New DataSet ' declared here so available throughout
    4.  
    5.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    6. ' all standard stuff
    7.         Dim cmd As New OleDb.OleDbCommand("SELECT Stuff.PName, [Identity].YearJoined, Stuff.[Star Rating] FROM [Identity] INNER JOIN Stuff ON [Identity].PName=Stuff.PName", conn)
    8.         Dim da As New OleDb.OleDbDataAdapter(cmd)
    9.         conn.Open()
    10.         da.Fill(ds, "Joined")
    11.         conn.Close()
    12.         dgv.DataSource = ds.Tables("Joined")
    13.     End Sub
    14.  
    15.     Private Sub dgv_CellEndEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellEndEdit
    16.  
    17. ' NB the order of sections in the SQL is different for OleDB, first is the JOIN!
    18.  
    19.         Dim cmd As New OleDb.OleDbCommand("UPDATE [Identity] INNER JOIN Stuff ON [Identity].PName=Stuff.PName SET [Identity].YearJoined= @YJoined, Stuff.[Star Rating]= @Stars WHERE [Identity].PName=@Pname", conn)
    20.  
    21. ' you must declare parameters in the order in which they are met in the SQL as OleDB effectively ignores the names
    22. ' this assumes that the dgv's datasource is the table as is. Use the DGV values directly if sortable or a DataView or filter is in use.
    23.  
    24.         cmd.Parameters.AddWithValue("@YJoined", ds.Tables("Joined").Rows(e.RowIndex).Item(1))
    25.         cmd.Parameters.AddWithValue("@Stars", ds.Tables("Joined").Rows(e.RowIndex).Item(2))
    26.         cmd.Parameters.AddWithValue("@PName", ds.Tables("Joined").Rows(e.RowIndex).Item(0))
    27.         'Dim da As New OleDb.OleDbDataAdapter(cmd)
    28.  
    29.         conn.Open()
    30.         cmd.ExecuteNonQuery()
    31.         conn.Close()
    32.  
    33.     End Sub
    34. End Class
    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!

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    14

    Re: Updating database with datagridview changes

    Ty mate for your solution...But sill I have one issue. If my Update query contains where on end of it, then my records are not updating and I'm 99% that my st_artikla is on 1'st position in my joined table. If i take where out of query then updating is going on but updating all items...this i understand since there is no "where" inside query.

    Code:
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'Baza_podatkovDataSet.narocilo' table. You can move, or remove it, as needed.
            Dim CONNECT_STRING As String = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" & System.AppDomain.CurrentDomain.BaseDirectory & "baza_podatkov.mdb"
            ' "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" & System.AppDomain.CurrentDomain.BaseDirectory & "baza_podatkov.mdb"
            Dim cnn As New OleDbConnection(CONNECT_STRING)
    
            cnn.Open()
    
              Dim sql As String = "SELECT artikel.st_artikla,artikel.naziv_artikla, narocilo.rok_izdelave FROM (narocilo INNER JOIN artikel ON narocilo.id_artkla = artikel.ID) WHERE(narocilo.ID_stranke = " & status_narocila.value & ")"
            Dim cmd As New OleDbCommand(sql, cnn)
    
            da = New OleDbDataAdapter(cmd)
    
            da.Fill(ds, "joined")
    
            DataGridView1.DataSource = ds.Tables("joined")
            cnn.Close()
        End Sub
    
        Private Sub DataGridView1_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
            Dim CONNECT_STRING As String = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" & System.AppDomain.CurrentDomain.BaseDirectory & "baza_podatkov.mdb"
            ' "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" & System.AppDomain.CurrentDomain.BaseDirectory & "baza_podatkov.mdb"
            Dim cnn As New OleDbConnection(CONNECT_STRING)
    
            cnn.Open()
    
            Dim sql As String = "UPDATE narocilo INNER JOIN artikel ON narocilo.id_artkla = artikel.ID SET artikel.st_artikla=@st_artikla, artikel.naziv_artikla=@naziv_artikla, narocilo.rok_izdelave=@rok_izdelave where artikel.st_artikla=@st_artikla "
            Dim cmd As New OleDbCommand(sql, cnn)
    
            cmd.Parameters.AddWithValue("@rok_izdelave", ds.Tables("joined").Rows(e.RowIndex).Item(2))
            cmd.Parameters.AddWithValue("@naziv_artikla", ds.Tables("joined").Rows(e.RowIndex).Item(1))
            cmd.Parameters.AddWithValue("@st_artikla", ds.Tables("joined").Rows(e.RowIndex).Item(0))
    
            cmd.ExecuteNonQuery()
            cnn.Close()
        End Sub
    End Class

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

    Re: Updating database with datagridview changes

    You didn't remember my instruction to declare the parameters in the order in which they are encountered in the sql, but more importantly you can't reuse a parameter in the same command (again this only applies to OleDB).

    UPDATE narocilo INNER JOIN artikel ON narocilo.id_artkla = artikel.ID SET artikel.st_artikla=@st_artikla, artikel.naziv_artikla=@naziv_artikla, narocilo.rok_izdelave=@rok_izdelave where artikel.st_artikla=@st_artikla

    Makes 4 parameters, not 3 so your Where is effectively = ""
    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