Hi erveryone,
I have a little issue:
I've created a method to update my database by importing a CSV file
The method to update my database is the follow one :
And the button who call il is the next one:
Everythings works perfectly but the DataGridview ("DBDataGrid") doesn't update. He updates only when we close the application (form) and restart it...
i've looked everywhere et tried a lot of things on the web but it still don't refresh...
Can someone explain me why there is this issue and how to solve it?
Thx in advance...
By the way i'm a beginner...
I have a little issue:
I've created a method to update my database by importing a CSV file
The method to update my database is the follow one :
VB.NET:
Public Sub ImportCSV2SQL(fileName As String, sqlTable As DataTable, column2Import() As Integer)
' Pr?paration des variables SQL
Dim sqlConxtion As SqlConnection = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\jeanraad\Desktop\" & _
"Holcim Quality Solutions\VB.Net\HQS_Dev\HQS_Dev\HQSdb.mdf;Integrated Security=True" & _
";Connect Timeout=30")
Dim sqlAction As SqlBulkCopy = New SqlBulkCopy(sqlConxtion.ConnectionString)
Dim sqlCommand As New SqlCommand("DELETE FROM dbo." & sqlTable.TableName, sqlConxtion)
'Dim sqlAdapter As New SqlDataAdapter("Select * From dbo." & sqlTable.TableName, sqlConxtion)
' Pr?paration des variables StreamReader et CsvReader
Dim stream As StreamReader = New StreamReader(fileName) : Dim streamCount As StreamReader = New StreamReader(fileName)
Dim csvCopy As CsvReader = New CsvReader(stream, True, ";"c)
' D?compte du nombre de lignes dans le fichier *.CSV
Dim nbrLines As Integer = 0
While streamCount.Peek <> -1
nbrLines += 1
Dim line As String = streamCount.ReadLine()
End While
' Comparaison avec la table existante dans SQL et le nombre de lignes est plus cons?quente
' dans le *.CSV alors remplacement de la table sinon demande ? l'utilisateur s'il veut la
' remplacer(1) ou annuler l'action (2)
If sqlTable.Rows.Count <= nbrLines - 1 And sqlTable.Rows.Count <> 0 Then
Dim msgResult = MsgBox("The number of rows of the CCList that you are trying to import is higher than those of those of the already existing CCList. Do you want to replace the old table or abort the importation?")
If msgResult = 1 Then
Exit Sub
End If
End If
' Tentative de se conecter au serveur SQL
Try
sqlCommand.Connection.Open()
Catch myerror As SqlException
MsgBox("Error connection SQL")
sqlCommand.Connection.Close()
Exit Sub
End Try
' Suppression de la table afin de pouvoir y introduire de nouvelles donn?es
sqlCommand.ExecuteNonQuery()
sqlCommand.Connection.Close()
' Mappings des colonnes entre le fichier *.CSV et la table de destination
For i = 0 To column2Import.Length - 1
sqlAction.ColumnMappings.Add(column2Import(i), i)
Next
' Transf?re des donn?es des donn?es du fichier *.CSV ? la table de destination
sqlAction.DestinationTableName = sqlTable.TableName
sqlAction.WriteToServer(csvCopy)
End Sub
And the button who call il is the next one:
VB.NET:
Private Sub ButtonImportCC_Click(sender As Object, e As EventArgs) Handles ButtonImportCC.Click
' Param?trage de l'OpenFileDialog afin que l'utilisateur puisse choisir la CC List ? importer
dialog2openFile.Title = "Select a CC List to Import"
dialog2openFile.Filter = "CC List (*.csv)|*.csv"
dialog2openFile.InitialDirectory = Application.StartupPath & "\Tools\"
dialog2openFile.RestoreDirectory = True
' Ouverture de l'OpenFileDialog
' Si l'utilisateur click sur OK alors la bdd SQL CCList est mise ? jour
If dialog2openFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
ImportCSV2SQL(dialog2openFile.FileName, Me.HQSdbDataSet.CCList, {0, 2})
End If
Me.CCListTableAdapter.Dispose()
Me.CCListTableAdapter.Fill(Me.HQSdbDataSet.CCList)
Me.DBDataGrid.DataSource = Me.CCListBindingSource
Me.DBDataGrid.Dock = System.Windows.Forms.DockStyle.Fill
Everythings works perfectly but the DataGridview ("DBDataGrid") doesn't update. He updates only when we close the application (form) and restart it...
i've looked everywhere et tried a lot of things on the web but it still don't refresh...
Can someone explain me why there is this issue and how to solve it?
Thx in advance...
By the way i'm a beginner...