locked
Fill DataColumn RRS feed

  • Question

  • Hi all,

     

    In my app., I have a DB, with several tables. When the application is launched, several checkings of the data in the tables are done. For example, I have a Table_Images table. I have to check if the file exists. My app also displays the whole table using a datagrid.

    In order for the user to know if there is some "corrupted" rows in the table, I create a DataColumn tha t I want to fill with several error messages depending on which value is wrong. Here is a sample code :

     

    Public Function Verifie_integrite_BD_Images() As Boolean

    Dim ReaderRequeteSQL As SqlCeDataReader

    Dim dataAdapterImages As SqlCeDataAdapter = New SqlCeDataAdapter("SELECT * FROM Table_Images", ConnexionSQL)

    Dim dataSetImages As DataSet = New DataSet

    Dim dataTableImages As DataTable

    ' On ajoute une colonne pour l'eventuelle non integritee

    Dim colonneIntegrite As DataColumn = New DataColumn

    Try

    ' Affichage dans le DataGrid

    Try

    colonneIntegrite.AllowDBNull = False

    colonneIntegrite.Caption = "Intégrité"

    colonneIntegrite.ColumnName = "Intégrité"

    colonneIntegrite.DataType = System.Type.GetType("System.String")

    colonneIntegrite.DefaultValue = "Intègre"

    dataAdapterImages.Fill(dataSetImages, "Images")

    dataTableImages = dataSetImages.Tables(0)

    dataTableImages.Columns.Add(colonneIntegrite)

    DataGridImages.DataSource = dataTableImages

    dataAdapterImages.InsertCommand = "Insert into"

    Catch

    MessageBox.Show("Erreur dans la construction de la form Base de données")

    End Try

    ' On selectionne tout ...

    ' 0 : Repertoire

    ' 1 : Nom de l'image

    ' 2 : Nb lignes

    ' 3 : Nb colonnes

    ' 4 : Latitude origine

    ' 5 : Longitude origine

    ' 6 : Latitude fin

    ' 7 : Longitude fin

    Dim RequeteSQL As New SqlCeCommand("SELECT * FROM Table_Images", form_BD.ConnexionSQL)

    ReaderRequeteSQL = RequeteSQL.ExecuteReader()

    Dim OK As Boolean = True

    While ReaderRequeteSQL.Read() = True

    ' Existence du fichier

    If Not System.IO.File.Exists(ReaderRequeteSQL.Item(0).ToString & ReaderRequeteSQL.Item(1).ToString) Then

    OK = False

    End If

    If ReaderRequeteSQL.Item(2) < ReaderRequeteSQL.Item(3) Or ReaderRequeteSQL.Item(4) > ReaderRequeteSQL.Item(5) Then

    OK = False

    End If

    End While

    ReaderRequeteSQL.Close()

    If OK = True Then

    MessageBox.Show("La BD est intègre")

    Else

    MessageBox.Show("La BD est corrompue")

    End If

    Catch err As Exception

    MessageBox.Show("Erreur de lecture dans la BD" & vbNewLine & err.ToString())

    End Try

    End Function

     

     

    The DataGrid displays properly the new column, with, as set by default, the value "Intègre". Wht I do not know, is how I can change this value when I check the validity of the data. Any idea ?

     

    Regards

    Saturday, July 21, 2007 5:54 PM

Answers

  • You could use '=' operator to assign a new value like this:

     

    dataTableImages.Rows(RowNumberHere)(ColumnNumberHere) = SomeValueHere

     

    Also you don't need DataReader as you already have all the data in memory which you can access directly from DataTable as shown above.  So get rid of your second query and loop through rows in the DataTable instead.

     

       Note you can't save this new column into the database unless you add this column to the database schema first.

     

    Saturday, July 21, 2007 8:18 PM