Sunday, October 30, 2011

Refreshing Datagridview after update, insert. Resolved

Attention. In this post there was a bug! Now fixed. All depends on Insert routine. In the "Note" table there's an autoincrement field named "counter". The right insert routine is this
Code:
Imports FirebirdSql.Data.FirebirdClient
_____________________________________________________
Public Class Edit
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim connection As FbConnection = New FbConnection("servertype=1;username=sysdba;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\Note.gdb")

        connection.Open()

        ' Start a local transaction.
        Dim fbTran As FbTransaction = connection.BeginTransaction()

        ' Enlist the command in the current transaction.
        Dim command As FbCommand = connection.CreateCommand()
        command.Transaction = fbTran

        command.CommandText = _
          "insert into Note" _
        & " (Title, Note) values ('" & Replace(Me.TextBox1.Text, "'", "''") & "','" _
         & Replace(Me.TextBox2.Text, "'", "''") & "')"
        command.ExecuteNonQuery()

       
        command.CommandText = _
                      "select max(contatore) as maxcount from note"
        command.ExecuteNonQuery()
       

        fbTran.Commit()
       


        Dim newCustomersRow As DataRow = Note.DataSet1.Tables("Note").NewRow()
        Dim dr As FbDataReader = command.ExecuteReader
        dr.Read()

        newCustomersRow("counter") = dr.Item("maxcount")


        newCustomersRow("Title") = Me.TextBox1.Text
        newCustomersRow("Note") = Me.TextBox2.Text

        Note.DataSet1.Tables("Note").Rows.Add(newCustomersRow)
       

        connection.Close()
       

        Me.Close()
                ' main form
              Note.Show()


    End Sub
End Class
To edit a row use this routine:
code:
Imports FirebirdSql.Data.FirebirdClient
____________________________________________________
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click



        Dim ObjConnection As New FbConnection()

        ObjConnection.ConnectionString = "servertype=1;username=sysdba;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\note.gdb"
        ObjConnection.Open()

        Dim ObjCommand As New FbCommand()
        ObjCommand.Connection = ObjConnection

        ObjCommand.CommandText = "update note set title='" & Replace(Me.TextBox1.Text, "'", "''") & "', Note='" & Replace(Me.TextBox2.Text, "'", "''") & "'where counter='" & Note.DataGridView1.CurrentRow.Cells("counter").Value & "'"
        ObjCommand.ExecuteNonQuery()
        Dim customerRow() = Note.DataSet1.Tables("Note").Select("counter ='" & Note.DataGridView1.CurrentRow.Cells("counter").Value & "'")
        customerRow(0)("Title") = Me.TextBox1.Text
        customerRow(0)("Note") = Me.TextBox2.Text
       

        Note.DataGridView1.Columns("title").HeaderText = GetString1("str6")
        Note.DataGridView1.Columns("Note").HeaderText = GetString1("str7")
        Note.DataGridView1.Columns("counter").Visible = False
        Me.Close()
                  ' main form
                  Note.Show()

    End Sub
End Class
Instead of strings appear Getstring1 function because the application is localized: let see this post in this blog: http://firebird-vbnet.blogspot.it/2015/12/localizing-windows-forms-with-resgenexe.html

No comments:

Post a Comment