Results 1 to 14 of 14

Thread: NullException on delete in WPF textbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    117

    NullException on delete in WPF textbox

    I have searched everywhere for a good solution to this problem but I can't find one. I have a range of controls in Datagridtables and textboxes on my WPF mainwindow, and when I delete any value I am unable to update the control/table to the database because of the Null exception. How do people handle this - I just need to sometimes simply delete cell data when no longer needed??

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: NullException on delete in WPF textbox

    Deleting data is not a problem. Saving changes is not a problem. Your code is the problem, but as you have chosen not to show us what you're doing, there's no way that we could possibly know what's wrong with it.

    NullReferenceExceptions are very easy to diagnose, if you actually use the debugger. When the exception is thrown, the debugger will break on the line that threw it. You can simply examine each reference on that line to see which one is Nothing. You can then work backwards to where you expected it to be set to determine why it wasn't.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    117

    Re: NullException on delete in WPF textbox

    Thanks JMc, the reason I have shown no code is because there is none. These text boxes have no specific code added, ie they remain as they did at the start of the project. However, they are bound to a datagrid and obviously I have worked a lot of code around defining textboxes etc. Nonetheless, throughout the development I have always had this null exception thrown whenever I add data through the textboxes, and have not been able to stop it. Not quite sure where to start if there is a need to look at the code???

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: NullException on delete in WPF textbox

    is the application crashing? If not then what's the problem? If so then the debugger must provide you with information about the exception and that will tell you where the exception is thrown. Assuming that there is no corruption in the project or the system, it must be something you're doing.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    117

    Re: NullException on delete in WPF textbox

    No, not crashing, the exception is simply thrown when I try to access or save data from cells that have been deleted.
    Last edited by Bear89; May 19th, 2022 at 10:01 AM.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: NullException on delete in WPF textbox

    Look at the table structure. Can your fields even hold a NULL value - Are the Nullable? Or are they marked "Not-Null"? That maybe the source of the problem. You're potentially trying to stuff a NULL into a field (or fields) that don't allow it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: NullException on delete in WPF textbox

    Quote Originally Posted by Bear89 View Post
    No, not crashing, the exception is simply thrown when I. Try to access or save data from cells that have been deleted.
    Exceptions get thrown. That's what they do. Exceptions being thrown is not a problem. It's exceptions being thrown and not caught that is a problem. If you're not catching the exception yourself and your app is not crashing then the exception is being caught elsewhere. There is no problem to solve. Goodbye.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    117

    Re: NullException on delete in WPF textbox

    Thanks tg, so here is the update code:

    Code:
    Public Sub UpdateTA()
            
            Try
                Dim PatientDataSet As Goldie.patientDataSet = CType(Me.FindResource("PatientDataSet"), Goldie.patientDataSet)
                Dim PatientDataSetTable1TableAdapter As patientDataSetTableAdapters.Table1TableAdapter = New Goldie.patientDataSetTableAdapters.Table1TableAdapter()
                PatientDataSetTable1TableAdapter.Update(PatientDataSet.Table1)  'Exception is thrown at this line
    
            Catch ex As System.Exception
                MessageBox.Show("Update failed")
                MsgBox(ex.ToString)
            End Try
        End Sub
    The ex is thrown at the 'PatientDataSetTable1TableAdapter.Update' line and reads: System.Data.OleDb.OleDbException: Field'TableFirstname'cannot be a zero length string at System.Data.DbDataAdapter.UpdatedRowStatusErrors etc.

    So it makes sense that the field is not set to accept Null, but I cant see where this is set in properties etc? Where can I set the field properties to accept Nullable?
    Last edited by Bear89; May 19th, 2022 at 10:15 AM.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: NullException on delete in WPF textbox

    Quote Originally Posted by Bear89 View Post
    Where can I set the field properties to accept Nullable?
    Read the error message. Does it say anything about NULLs? The issue here is your database, not your DataSet. You need to configure your database to accept the data that you want to save to it.

  10. #10
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: NullException on delete in WPF textbox

    Quote Originally Posted by techgnome View Post
    Look at the table structure. Can your fields even hold a NULL value - Are the Nullable? Or are they marked "Not-Null"? That maybe the source of the problem. You're potentially trying to stuff a NULL into a field (or fields) that don't allow it.

    -tg
    Quote Originally Posted by Bear89 View Post
    Thanks tg, so here is the update code:

    Code:
    Public Sub UpdateTA()
            
            Try
                Dim PatientDataSet As Goldie.patientDataSet = CType(Me.FindResource("PatientDataSet"), Goldie.patientDataSet)
                Dim PatientDataSetTable1TableAdapter As patientDataSetTableAdapters.Table1TableAdapter = New Goldie.patientDataSetTableAdapters.Table1TableAdapter()
                PatientDataSetTable1TableAdapter.Update(PatientDataSet.Table1)  'Exception is thrown at this line
    
            Catch ex As System.Exception
                MessageBox.Show("Update failed")
                MsgBox(ex.ToString)
            End Try
        End Sub
    The ex is thrown at the 'PatientDataSetTable1TableAdapter.Update' line and reads: System.Data.OleDb.OleDbException: Field'TableFirstname'cannot be a zero length string at System.Data.DbDataAdapter.UpdatedRowStatusErrors etc.

    So it makes sense that the field is not set to accept Null, but I cant see where this is set in properties etc? Where can I set the field properties to accept Nullable?
    Quote Originally Posted by jmcilhinney View Post
    Read the error message. Does it say anything about NULLs? The issue here is your database, not your DataSet. You need to configure your database to accept the data that you want to save to it.

    Indeed... I never said anything about the code... I said to look at the "table structure" ... in the database ... it sounds like there are constraints on the fields to prevent them from being zero-length .. Which means you can't update them to "" which is what you're doing when you delete the data. If the fields are not allowed to be zero-length, then it sounds like they are required fields for one reason or another.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    117

    Re: NullException on delete in WPF textbox

    Thanks pg and jmc, I have allowed null values in the access database and these are now accepted, so excellent.

    I still have a problem though with date fields, which wont allow entries in my WPF datagrid in main window. Dates entered through Access appear in the datagrid, but cant be edited. I cant see an immediate solution - the MSAccess format is as short date with no restrictions. The date field in question is a datepicker format in a child table field. Date fields in the parent table and form update without difficulty, but changes to the child form dont (whereas all non-date fields update ok).

    The xaml definitions for the datagrid are:
    Code:
                    <DataGridTemplateColumn x:Name="_DateColumn2" Header="Date" SortDirection="Descending" Width="100">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <DatePicker SelectedDate="{Binding Date, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    I dont get an error when I enter data, but any entry is not saved, with a forced update failing.

    Any thoughts?
    Last edited by Bear89; May 31st, 2022 at 12:05 AM.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: NullException on delete in WPF textbox

    Quote Originally Posted by Bear89 View Post
    I still have a problem though with date fields, which wont allow entries in my WPF datagrid in main window.
    That's not the topic of this thread. If the issue you originally asked about is resolved then you should mark this thread Resolved using the Thread Tools menu. If you have a new question on a different topic then you should start a new thread for that with all and only the information relevant tot hat issue.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    117

    Re: NullException on delete in WPF textbox

    I disagree. The difficulty here is getting wpf to accept data and this might still relate to a null issue or perhaps a related issue, very much related to the discussion so far and to the topic.

    Would appreciate help.

    To take this further, I notice that if I alter a date that is existing it will then accept the change and allow updates. However, if I try to add a date to a new row it is rejected. This to me suggests there is a default format from wpf which when implemented is unable to access the .mdb database. Does this make sense? Perhaps if I were to format the field eg to a short date format, which is the .mdb formatted property, but I'm not sure how to do this - can I add that property to the xaml?
    Last edited by Bear89; May 31st, 2022 at 03:40 AM.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    117

    Re: NullException on delete in WPF textbox

    OK, I have made some progress but would really love some help. First, it seems the datepicker can switch between normal and editable mode, and that influences whether it will accept new data. Pressing F2 makes the switch manually, and tab key then saves data, which can then update to source.

    But here is the link to previous parts of this thread. If I then try to delete this field, it will not update, with the same old null value exception thrown. MS Access does not have an option to allow null in properties of Date fields, so it cant be set as a default of the database. It seems though, that when MS Access places data in a date field, the formatting is accepted by WPF, so there must be a way of doing it.

    Also it would be good to encode the F2 editable feature but again how to do that is not obvious. Does anyone know how to do these things?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width