none
Show Qty to Label1 RRS feed

  • Question

  • I want show Quantity in Label1.Text  from data table  tbl_mstock where Batch_No = DataGridView current row Columns(3). I've already data load on DataGridView. How to write this code. I write above this code :

    Private Sub DGV_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellValueChanged
        For i = DGV.SelectedRows.Count -1
                Str = "Select qty from tbl_mstock where batch_no = '" & DGV.Item(3, i).Value & "'"
                Cmd = New MySqlCommand(Str, Con)
                Dr = Cmd.ExecuteReader
                If Dr.Read = True Then
                    Label1.Text = Dr(0)
                End If
                connection_close()
         Next
    End Sub

    Please Help my problem.


    dopenoinam


    Friday, June 9, 2017 6:37 AM

All replies

  • Maybe you should try this:

    Private Sub DGV_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellValueChanged
    
       Dim sum = 0
    
       For i = 0 To DGV.SelectedRows.Count -1
          Str = "Select qty from tbl_mstock where batch_no = '" & DGV.Item(3, i).Value & "'"
          Cmd = New MySqlCommand(Str, Con)
          Dr = Cmd.ExecuteReader
          If Dr.Read = True Then
             sum += CInt(Dr(0))
          End If
          connection_close()
       Next
       Label1.Text = sum.ToString
    End Sub

    Though it can and must be improved using different techniques.


    • Edited by Viorel_MVP Friday, June 9, 2017 7:27 AM
    Friday, June 9, 2017 7:27 AM
  • Hi Dopen,

    I do a simple sample that you can refer to, I assume that the datagridview1 column (3) column name is batch_no .

    Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
            loaddate()
            If DataGridView1.RowCount > 0 Then
                Dim Id As Integer = DataGridView1.Rows(e.RowIndex).Cells("batch_no").Value
                fun(Id)
            End If
        End Sub
    
        Private Sub loaddate()
            Dim dt As New DataTable
            Dim strConn As String = ""
            Using conn As New MySqlConnection(strConn)
                Dim selectSql = "Select * from tbl_mstock "
                Using com As New MySqlCommand(selectSql, conn)
                    Try
                        conn.Open()
                        Dim adapter As New MySqlDataAdapter(com)
                        adapter.Fill(dt)
                        DataGridView1.DataSource = dt
                        conn.Close()
                    Catch ex As Exception
                        MessageBox.Show(ex.ToString())
                    End Try
                End Using
            End Using
        End Sub
        Private Sub fun(a As Integer)
            Dim strConn As String = ""
            Using conn As New MySqlConnection(strConn)
                Dim selectSql = "Select qty from tbl_mstock where batch_no =@batch_no "
                Using com As New MySqlCommand(selectSql, conn)
                    Try
                        conn.Open()
                        com.Parameters.AddWithValue("@batch_no", a)
                        Dim datareader As MySqlDataReader = com.ExecuteReader
                        While datareader.Read()
                            Label1.Text = datareader(0)
                            MessageBox.Show(Label1.Text)
                        End While
                        conn.Close()
                    Catch ex As Exception
                        MessageBox.Show(ex.ToString())
                    End Try
                End Using
            End Using
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Friday, June 9, 2017 7:57 AM
    Moderator
  • Dopen,

    For this we have a very nice complete sample how to do this in the most reliable and quick way on our website.. 

    http://www.vb-tips.com/ExecuteScalarText.ASPX


    Success
    Cor

    Friday, June 9, 2017 9:19 AM
  • Not function for this code

    dopenoinam

    Friday, June 9, 2017 10:29 AM
  • Hi Dopen,

    Do you try my code? In my side, it works.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 12, 2017 1:31 AM
    Moderator