0

This should be straight forward but it doesn't want to work for me..

I'm trying to populate a Datagridview on form load. The Sub below works fine if i call Populategrid() from a button once the grid is populate with the Reloadrecords & displayrecords.

 Public Sub populategrid()
    If Trim(Me.Text) = "CIMDETAILS" Then
        da = New SqlDataAdapter("SELECT * from Interactions where [Name] like '" & txtdetect2.Text & "%' order by [IntDate] desc", SQLCon)
        dsetAssets = New DataSet
        da.Fill(dsetAssets, "Interactions")
        dgvData.DataSource = dsetAssets.Tables("Interactions").DefaultView
    Else
        ReloadRecords()
        DisplayRecords()
    End If


Public Sub DisplayRecords()
    Try
        Dim da = New SqlDataAdapter("SELECT * from Interactions order by [IntDate] desc", SQLCon)
        dsetAssets = New DataSet
        da.Fill(dsetAssets, "Interactions")
        dgvData.DataSource = dsetAssets.Tables("Interactions").DefaultView
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
    End Try

End Sub

Sub ReloadRecords()

    Try

        FillCIM("SELECT * FROM Interactions order by [IntDate] desc", dgvData)
        dgvData.Columns(0).Visible = False
        dgvData.Columns(1).Width = 300 ''Name''
        dgvData.Columns(1).HeaderCell.Value = "Customer Name"
        dgvData.Columns(2).Width = 200 ''Int type''
        dgvData.Columns(2).HeaderCell.Value = "Interaction Type"
        dgvData.Columns(3).Width = 170  ''Int Date''
        dgvData.Columns(3).HeaderCell.Value = "Interaction Date"
        dgvData.Columns(4).Width = 400   ''Remarks''
        dgvData.Columns(4).HeaderCell.Value = "Remarks"
        dgvData.Columns(5).Visible = False


    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
    End Try

End Sub

 Private Sub CIMDETAILS_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ReloadRecords()
    DisplayRecords()
    Call populategrid()
End Sub

Public Function FillCIM(ByVal Sqlstring As String, ByVal MyDataGrid As DataGridView)

    Dim Constring = "Data Source=" & database & "\SQLEXPRESS; Initial Catalog = CIM; Integrated Security=true"
    Dim SQLCon As New SqlConnection(Constring)
    Dim SQLAdapter As New SqlDataAdapter()
    Dim myDataset As New DataSet()

    SQLCon.Open()

    Try
        SQLAdapter.SelectCommand = New SqlCommand(Sqlstring, SQLCon)
        SQLAdapter.Fill(myDataset)
        MyDataGrid.DataSource = myDataset.Tables(0)
    Catch ex As Exception
    End Try

    SQLCon.Close()
    SQLAdapter.Dispose()
    myDataset.Dispose()
    Return True
End Function
Matt
  • 53
  • 1
  • 9
  • 1
    Do not concatenate SQL string, use parameter binding, unless you want SQL Injection attack – Lukasz Szozda Oct 25 '15 at 15:30
  • @lad2025, Thanks for the advice! I've added more code to better explain my issue. Maybe you can help? I will look into parameter binding in the meantime. – Matt Oct 25 '15 at 15:43
  • I dont know vb.net, but i see that PopulateGrid checks me.Text for a specific value. Might it be so that in the load from the form this value is not set yet in me.Text ? – GuidoG Oct 26 '15 at 11:44
  • @GuidoG, thanks for your reply. The text value is on the new form once loaded, for some reason i can only run the populategrid sub from a button. Makes no sense to me at all. – Matt Oct 26 '15 at 19:11
  • @Matt The text value is on the new form once loaded. The catch is in the words "once loaded". You are calling PopulateGrid in the load event, where the text value might not be on the form yet since the form is not yet loaded. I dont know if that is actualy the problem but its worth checking out – GuidoG Oct 27 '15 at 00:29
  • 1
    What method `FillCIM` does in the `ReloadRecords` method? And what you mean by "not working"? Error exception, no records showed, wrong columns? – Fabio Oct 27 '15 at 16:04
  • Hi @Fabio. Ive added the FillCIM method. I think the problem is exactly what GuidoG said.. the txtdetect2.Text text isn't on the form until loaded so the populategrid doesn't work. The grid is populated by the Reloadrecord & DisplayRecord but it doesn't filter it down to the value of the txtdetect2. this only works if i put that sub against a button and press it once the page is loaded. – Matt Oct 30 '15 at 09:47
  • Thanks @GuidoG think you may be right about this! – Matt Oct 30 '15 at 09:47

1 Answers1

0

Pretty simple to get this working.

I've only just discovered there's an event after form load, which is form shown. Once i put the populategrid() sub in there it works fine.

Matt
  • 53
  • 1
  • 9