none
Setting to dbnull does not change rowstate RRS feed

  • Question

  • I have an app where I have to allow them to remove a date previously entered.  This of course requires the "value" to be set to dbnull.  However when I do that, the rowstate is not changed  so the update routine sees no changes to save.

    I've tried two different ways of setting the value to Null.

    bindingsource.current("DateField") = system.dbnull.value

         and

    updating the dataset directly by determining the currentrow as a Datarow and then

         currentrow("DateField") = system.dbnull.value

    I can watch it in the debugger and see the "values" change but the rowstate doesn't change to Modified.

    Does anyone have a best way to get a date column back to null with the rowstate being changed to modified.  ?

    Thanks if advance

     

    Larry K

     

     

    Wednesday, June 30, 2010 8:05 PM

Answers

  • Thanks.  It wasn't during an add it was during an edit.

    But I solved it anyway.  After the bindingsource.EndEdit, I had to test all three (there were three date fields) date textboxes for isnullorempty and if any one was then I had to enclose an update to the datarow with a begin and end edit.  Setting the bindingsource.current("field") didn't do it.  Looks like this

     

     

    If (Not CurrentRow Is Nothing) And (String.IsNullOrEmpty(Me.PLUG_DATETextBox.Text) Or _

     

                                                     String.IsNullOrEmpty(Me.PLUG_KDTextBox.Text) Or _

     

                                                     String.IsNullOrEmpty(Me.PLUG_APPRTextBox.Text)) Then

                  CurrentRow.BeginEdit()

     

                   If String.IsNullOrEmpty(Me.PLUG_DATETextBox.Text) Then

                        CurrentRow(

    "PLUG_DATE") = System.DBNull.Value

     

                   End If

     

                   If String.IsNullOrEmpty(Me.PLUG_KDTextBox.Text) Then

                       CurrentRow(

    "PLUG_KD") = System.DBNull.Value

     

                   End If

     

                   If String.IsNullOrEmpty(Me.PLUG_APPRTextBox.Text) Then

                        CurrentRow(

    "PLUG_APPR") = System.DBNull.Value

     

                   End If

                  CurrentRow.EndEdit()

     

    End If

    Thanks

     

    • Marked as answer by Larry_K31 Thursday, July 1, 2010 4:49 PM
    Thursday, July 1, 2010 4:49 PM

All replies

  • Hi Larry,

    This is a common issue with development and not a technical issue. When we create a datatable, by default the row state is Added. And if you try to change any value in the data table and even it will be in Added state.

    In other words, even if you modify any value in a newly Added row, the RowState remains same (i.e, DataRowState.Added) but not Modified. Please find the below sample code.

    DataTable dt = new DataTable();

    dt.Columns.Add(

    "A");

    dt.Columns.Add(

    "B");

    dt.Columns.Add(

    "C");

     

    for (int i = 1; i <= 5; i++)

    {

     

    DataRow drow = dt.NewRow();

    drow[0] =

    "A" + i;

    drow[1] =

    "B" + i;

    drow[2] =

    "C" + i;

    dt.Rows.Add(drow);

    }

    dg.DataSource = dt;

    dg.DataBind();

    dt.Rows[1][1] =

    DBNull.Value;

     

    if (dt.GetChanges(DataRowState.Modified) != null) // Change Modified to Added and you find it works.

    {

    System.Windows.Forms.

    MessageBox.Show("Yes");

    }

    Thursday, July 1, 2010 12:38 PM
  • Thanks.  It wasn't during an add it was during an edit.

    But I solved it anyway.  After the bindingsource.EndEdit, I had to test all three (there were three date fields) date textboxes for isnullorempty and if any one was then I had to enclose an update to the datarow with a begin and end edit.  Setting the bindingsource.current("field") didn't do it.  Looks like this

     

     

    If (Not CurrentRow Is Nothing) And (String.IsNullOrEmpty(Me.PLUG_DATETextBox.Text) Or _

     

                                                     String.IsNullOrEmpty(Me.PLUG_KDTextBox.Text) Or _

     

                                                     String.IsNullOrEmpty(Me.PLUG_APPRTextBox.Text)) Then

                  CurrentRow.BeginEdit()

     

                   If String.IsNullOrEmpty(Me.PLUG_DATETextBox.Text) Then

                        CurrentRow(

    "PLUG_DATE") = System.DBNull.Value

     

                   End If

     

                   If String.IsNullOrEmpty(Me.PLUG_KDTextBox.Text) Then

                       CurrentRow(

    "PLUG_KD") = System.DBNull.Value

     

                   End If

     

                   If String.IsNullOrEmpty(Me.PLUG_APPRTextBox.Text) Then

                        CurrentRow(

    "PLUG_APPR") = System.DBNull.Value

     

                   End If

                  CurrentRow.EndEdit()

     

    End If

    Thanks

     

    • Marked as answer by Larry_K31 Thursday, July 1, 2010 4:49 PM
    Thursday, July 1, 2010 4:49 PM