none
DataGridview in form1 not update after edit from form2 (Using statement, Try Catch statement) RRS feed

  • Question

  • Hello 

    I just changed to use using statement and I got a problem when I need to update data in DataGridview at form1.

    My step is > open form1 (data list) > select data and open selected on form2 (edit form) > Edit data on form2 and save > then refresh data on form1 but data is not change.

    this is my code 

    Form1: customer_analyzer_list  (Load data working fine)

    Public Sub LoadCustAnalyzerList()
            Try
                Using StrWer As New StreamReader(Application.StartupPath & "\AppConn.ini")
                    Using objConn As New MySqlConnection(StrWer.ReadLine)
                        dtCtmAnl.Clear()
                        objConn.Open()
                        If tbKeySearch.Text = "" Then
                            StrSql = "SELECT A.customer_analyzer_list_id,B.hos_name,A.analyzer_name,A.install_date,A.cancel_date,A.install_staff,CASE A.active WHEN TRUE THEN 1 ELSE 0 END AS CALActive,A.po_no,A.inv_no " &
                                     "FROM customer_analyzer_list A LEFT JOIN hos_list B ON A.hos_code=B.hos_code LIMIT 100;"
                        Else
                            StrSql = "SELECT A.customer_analyzer_list_id,B.hos_name,A.analyzer_name,A.install_date,A.cancel_date,A.install_staff,CASE A.active WHEN TRUE THEN 1 ELSE 0 END AS CALActive,A.po_no,A.inv_no " &
                                     "FROM customer_analyzer_list A LEFT JOIN hos_list B ON A.hos_code=B.hos_code WHERE B.hos_name LIKE '%" & tbKeySearch.Text & "%' OR A.analyzer_name LIKE '%" & tbKeySearch.Text & "%';"
                        End If
                        Using objCmd As New MySqlCommand(StrSql, objConn)
                            Dim adapter As New MySqlDataAdapter(objCmd)
                            adapter.Fill(dtCtmAnl)
    
                            If dtCtmAnl.Rows.Count > 0 Then
                                gvCALData.DataSource = dtCtmAnl
                                gvCALData.Columns(0).Visible = False
                                gvCALData.Columns(1).HeaderText = "โรงพยาบาล"
                                gvCALData.Columns(1).Width = 250
                                gvCALData.Columns(2).HeaderText = "ชื่อ Analyzer"
                                gvCALData.Columns(3).HeaderText = "วันที่ติดตั้ง"
                                gvCALData.Columns(4).HeaderText = "วันที่ยกเลิก"
                                gvCALData.Columns(5).HeaderText = "เจ้าหน้าที่ติดตั้ง"
                                gvCALData.Columns(6).HeaderText = "สถานะ"
                                gvCALData.Columns(7).HeaderText = "เลขที่ PO"
                                gvCALData.Columns(8).HeaderText = "เลขที่ Invoice"
                            Else
                                gvCALData.DataSource = Nothing
                            End If
                        End Using
                    End Using
                End Using
            Catch ex As Exception
                Throw ex
            End Try
        End Sub


    Form2: customer_analyzer_edit (Edit form)

     Private Sub btSaveAnz_Click(sender As Object, e As EventArgs) Handles btSaveAnz.Click
            Try
                If ChkHosCode(tbHosCode.Text.Trim) = 0 Then
                    MessageBox.Show("รหัสโรงพยาบาลไม่ถูกต้อง")
                ElseIf cboAnalyzer.SelectedValue = 0 Then
                    MessageBox.Show("กรุณาเลือก Analyzer")
                Else
                    If rbtStsYes.Checked = True Then
                        tmpActive = 1
                        StrSql = "UPDATE customer_analyzer_list SET hos_code='" & tbHosCode.Text & "',analyzer_id=" & Convert.ToInt32(cboAnalyzer.SelectedValue) & ",analyzer_name='" & cboAnalyzer.Text & "',install_date='" & dtpInstallDate.Value.ToString("yyyy-M-d", CultureInfo.CreateSpecificCulture("en-GB")) & "',install_staff='" & tbStaff.Text & "',active=" & tmpActive & ",po_no='" & tbPONo.Text & "',inv_no='" & tbInvNo.Text & "' WHERE customer_analyzer_list_id=" & pCALID & ";"
                    Else
                        tmpActive = 0
                        StrSql = "UPDATE customer_analyzer_list SET hos_code='" & tbHosCode.Text & "',analyzer_id=" & Convert.ToInt32(cboAnalyzer.SelectedValue) & ",analyzer_name='" & cboAnalyzer.Text & "',install_date='" & dtpInstallDate.Value.ToString("yyyy-M-d", CultureInfo.CreateSpecificCulture("en-GB")) & "',cancel_date='" & dtpCancelDate.Value.ToString("yyyy-M-d", CultureInfo.CreateSpecificCulture("en-GB")) & "',install_staff='" & tbStaff.Text & "',active=" & tmpActive & ",po_no='" & tbPONo.Text & "',inv_no='" & tbInvNo.Text & "' WHERE customer_analyzer_list_id=" & pCALID & ";"
                    End If
    
                    Using StrWer As New StreamReader(Application.StartupPath & "\AppConn.ini")
                        Using objConn As New MySqlConnection(StrWer.ReadLine)
                            objConn.Open()
                            Using trans As MySqlTransaction = objConn.BeginTransaction
                                Using objCmd As New MySqlCommand(StrSql, objConn)
                                    RowExc = objCmd.ExecuteNonQuery
                                    If RowExc > 0 Then
                                        trans.Commit()
                                        'customer_analyzer_list.btSearchCAL.PerformClick()
                                        Call customer_analyzer_list.LoadCustAnalyzerList()
                                        MessageBox.Show("แก้ไขเรียบร้อย")
                                        'Call customer_analyzer_list.btSearchCAL_Click(sender, e)
                                        Me.Close()
                                    Else
                                        trans.Rollback()
                                        MessageBox.Show("แก้ไขไม่สำเร็จ")
                                    End If
                                End Using
                            End Using
                        End Using
                    End Using
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString)
            End Try
        End Sub


    Private Sub btClose_Click(sender As Object, e As EventArgs) Handles btClose.Click
            Try
                'Call customer_analyzer_list.LoadCustAnalyzerList()
                'customer_analyzer_list.btSearchCAL.PerformClick()
                Me.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString)
            End Try
        End Sub

    Load data > open edit form > edit data : working fine but data at DataGridview in customer_analyzer_list  not change. LOL

    I tried to change position of call load data line but not work.

    *call load data and buttonsearch.performclick not working both.

     What am I missing or any example for using statement with try catch , Any suggestion?

    Thank you.

    Monday, June 4, 2018 7:07 AM

All replies

  • *call load data and buttonsearch.performclick not working both.

     What am I missing or any example for using statement with try catch , Any suggestion?

    Is this the call that you are expecting to update the DGV?
        Call customer_analyzer_list.LoadCustAnalyzerList()

    You haven't shown any code that sets the value of 'customer_analyzer_list' to the instance of the list form that contains the DGV that you want to update.    Typically, this value will be set from an argument that you pass to the constructor of the edit form.  If you are relying on the default value for this variable, that will be unreliable.  You should insert a breakpoint at the start of the method you are calling in the list form and make sure the code is reaching that point.  If it isn't then the likely problem is that you are referring to the wrong instance of the list form. 

    If you prove that you are referring to the correct instance of the list form then you need to ensure that the DGV is refreshed after the data source is changed.  See:
    https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings%28v=vs.110%29.aspx


    • Edited by Acamar Monday, June 4, 2018 8:22 AM fmt
    Monday, June 4, 2018 8:21 AM
  • The test found that my code can't refresh data in form1 from form2. I don't know why.

    but now working with run sub at form1 (add code after show edit form)

    My temp solution :

    Private Sub btDetail_Click(sender As Object, e As EventArgs) Handles btDetail.Click
            Try
                If tmpID = 0 Then
                    MessageBox.Show("กรุณาเลือกข้อมูล")
                Else
                    customer_analyzer_edit.pCALID = tmpID
                    customer_analyzer_edit.ShowDialog()
    
                    LoadCustAnalyzerList()  '<<< Add this line to call load data sub at form1
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString)
            End Try
        End Sub

    Newbie for "Using Statement".

    ขอบคุณครับ

    Monday, June 4, 2018 10:32 AM