Question Datarepeater Databind Issues

lynxbct

Member
Joined
Aug 30, 2011
Messages
5
Programming Experience
5-10
Hi Folks,

This is my first post here so forgive me if I am not following any correct protocols. I am attempting to generate a DataSet and bind it to a datarepeater object. I had been doing this at Design Time and was getting an error message when filling the dataset when the form loaded. So I am basically createing the dataset in my code now when the form loads and binding it to the datarepeater. However, when I attempt to bind the dataset I get a NullReferenceException - even through I know that there are 60 rows of data in the dataset table.

My code at the moment is as follows:

 
        ' Initialise the command object
        desCommand = New OdbcCommand()
        desCommand.Connection = desConnection
        ' Define DataAdapter and DataTable objects
        Dim da As New OdbcDataAdapter
        ' Define SQL Command to retreive data for specified lot
        With desCommand
            .CommandText = "SELECT [Lot Number], [Stent Number], [Mechanical Damage], [BMS Weight], [Visual Inspection 1], [Coated Weight 1], [Drug Mass 1], " & _
                           "[Visual Inspection 2], [Coated Weight 2], [Drug Mass 2], [Visual Inspection 3], [Coated Weight 3], [Drug Mass 3], [Parylene Fixture ID], " & _
                           "[Parylene Visual Inspection], [Post Parylene Weight], [Parylene Weight] " & _
                           "FROM [Lot Data] " & _
                           "WHERE ([Lot Number] = '" & SelectLotForm.lotnumber & "') " & _
                           "ORDER BY [Stent Number]"
            .Connection = desConnection
        End With
        ' Fill the dataset with data
        MessageBox.Show(desCommand.CommandText)
        da.SelectCommand = desCommand
        da.Fill(ds)
        MessageBox.Show("Num Tables: " & ds.Tables.Count)
        MessageBox.Show("Num Rows: " & ds.Tables(0).Rows.Count)
        ' Did the query return any data
        If ds.Tables(0).Rows.Count = 0 Then
            ' Define datarow variable
            Dim newDesRow As DataRow
            ' Add a row for each stent in the lot
            For i As Integer = 1 To SelectLotForm.lotqty
                ' Create a new data table row
                newDesRow = ds.Tables(0).NewRow()
                ' Add Lot Number and stent number
                newDesRow("Lot Number") = SelectLotForm.lotnumber
                newDesRow("Stent Number") = i
                newDesRow("Mechanical Damage") = False
                ' Add data row to the table
                ds.Tables(0).Rows.Add(newDesRow)
                ' Insert record into the database
                desCommand.CommandText = "INSERT INTO [Lot Data] ([Lot Number], [Stent Number], [Mechanical Damage], [Visual Inspection 1], [Visual Inspection 2], [Visual Inspection 3], [Parylene Fixture ID], [Parylene Visual Inspection]) VALUES('" & SelectLotForm.lotnumber & "', " & i & ", false, '', '', '', '', '')"
                desCommand.ExecuteNonQuery()
            Next
        End If
        ' Bind the DataRepeater object to the datatable
        DataRepeater1.DataSource = ds.Tables(0)



As you can see i have several debug messages in there. I get the error on the last line when setting the datarepeater data source. I try and get data for a specified lot - if it doesn't return any values I create some values in the datatable and add them to the DB. On first load when there is no data this works perfectly. If I then close the form and reload it, it will display fine. However, if I change any of the data (database is updated) and then go back in again, I get the error above.


It is baffling me and I would appreciate any advice you can give me.:dispirited:
 
Found out what the source of the problem was. I had a checkbox and a number of comboboxes in my datarepeater. I had event handlers for all these fields. For example, if the checkbox was ticked I wanted to hide all other fields on that row of the datarepeater. What was happening was that these event handlers were being fired when the datarepeater was databound. As a result, the function was trying to hide fields which had not been created yet.

I have just prevented the functions carrying out the tasks if they are called when the data is binding. Problem Solved!!
 

Latest posts

Back
Top