[dba-VB] Data Binding values from code

Charlotte Foust cfoust at infostatsystems.com
Thu Mar 13 19:34:32 CDT 2008


I haven't waded through your code (I write VB.Net, so it's more work to
read yours), but I noticed you're setting the Text property of the
control.  Why that instead of the Value property?  I have seen instances
when it made a difference.  It's perfectly possible in my experience to
set the text of a control without triggering the form's or the control's
validation. 

Charlotte Foust

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Michael
Maddison
Sent: Thursday, March 13, 2008 3:50 PM
To: Discussion concerning Visual Basic and related programming issues.
Subject: Re: [dba-VB] Data Binding values from code

Sorry guys,

Wasn't complaining about not being answered.

 "doesn't Me.textbox.Value = "something" work? "

No!  Not for me.

textBox.Text = "ABC";
Does not maketh the DS == Dirty.
Or the BS or the CM.  Or I could have completely lost the plot ;-)

I usually use the NetTiers framework, where this sort of stuff is really
straightforward.
So I was surprised that values I was setting in code were not being sent
to the db (Access).
Google didn't help much, some mentions of adding custom attributes to
the objects so that the binding would work.  I wanted a drag n drop out
of the box solution for this quick n dirty app.

Anyhow, I tried adding a bindingNav control, it didn't help.
I find it hard to believe that I didn't stuff up somewhere, but I didn't
write much code, just dragged the objects onto the form and bound them
really.

I used the Update code straight out of MSDN.  Deletes 1st, then updates,
then addnews.

Here is the code I ended up with...

            if ( cboSlaking.SelectedValue != null )
                txtSlaking.Text = cboSlaking.SelectedValue.ToString( );

            if ( cboDisperse.SelectedValue != null )
                txtDispersability.Text =
cboDisperse.SelectedValue.ToString( );

            if ( cboVC.SelectedValue != null )
                txtVCRating.Text = cboVC.SelectedValue.ToString( );

            txtDepthQual.Text = cboDepthQual.SelectedValue.ToString( );

            if ( !IsValid( ) ) return;

            if ( _isNew )
            {
                DataRowView drv = ( DataRowView ) profilesBS.Current;
	    
	//Binding updates work arround	
	//Manually insert the row, cancel the edit for BS
                try
                {
                    soilProfilesTA.Insert( _newProfileID,
                        Convert.ToInt16( drv.Row.ItemArray.GetValue( 2 )
), //Prof
                        drv.Row.ItemArray.GetValue( 3 ).ToString( ),
//GH
                        Convert.ToInt32( drv.Row.ItemArray.GetValue( 4 )
), //Hor sub
                        drv.Row.ItemArray.GetValue( 5 ).ToString( ),
//Hor Suff
                        Convert.ToDouble( String.IsNullOrEmpty(
drv.Row.ItemArray.GetValue( 6 ).ToString( ) ) ? 0 :
drv.Row.ItemArray.GetValue( 6 ) ),//Depth S
                        Convert.ToDouble( String.IsNullOrEmpty(
drv.Row.ItemArray.GetValue( 7 ).ToString( ) ) ? 0 :
drv.Row.ItemArray.GetValue( 7 ) ),//Depth fin
                        String.IsNullOrEmpty( txtDepthQual.Text ) ? null
: txtDepthQual.Text.ToString( ),//depth range   
                        Convert.ToInt32( drv.Row.ItemArray.GetValue( 9 )
),//col L
                        drv.Row.ItemArray.GetValue( 10 ).ToString(
),//col H
                        Convert.ToInt32( drv.Row.ItemArray.GetValue( 11
) ),//col s
                        drv.Row.ItemArray.GetValue( 21 ).ToString(
),//mott %   
                        String.IsNullOrEmpty( txtMottlingComments.Text )
? null : txtMottlingComments.Text.ToString( ), //Mott text
                        drv.Row.ItemArray.GetValue( 13 ).ToString(
),//texture
                        Convert.ToInt32( drv.Row.ItemArray.GetValue( 14
) ),//ped
                        Convert.ToInt32( drv.Row.ItemArray.GetValue( 15
) ),//struct
                        String.IsNullOrEmpty( txtComments.Text ) ? null
: txtComments.Text.ToString( ),  //other
                        drv.Row.ItemArray.GetValue( 20 ).ToString(
),//stone
                        Convert.ToInt32( txtSlaking.Text ), //slak  
                        Convert.ToInt32( txtDispersability.Text ),//disp
                        Convert.ToInt32( txtVCRating.Text ) );//vc  
                }
                catch ( Exception ex )
                {
                    MessageBox.Show( ex.Message );
                    return;
                    //throw;
                }

                profilesBS.CancelEdit( );
            }
            else
            {
                profilesBS.EndEdit( );
            }

            Profiles.SoilProfilesDataTable newProfile = (
Profiles.SoilProfilesDataTable ) profiles1.SoilProfiles.GetChanges(
DataRowState.Added );
            Profiles.SoilProfilesDataTable editedProfile = (
Profiles.SoilProfilesDataTable ) profiles1.SoilProfiles.GetChanges(
DataRowState.Modified );

            try
            {
                if ( newProfile != null )
                {
                    soilProfilesTA.Update( newProfile );
                }

                if ( editedProfile != null )
                {
                    soilProfilesTA.Update( editedProfile );
                }

                profiles1.AcceptChanges( );

            }

            catch ( DBConcurrencyException ex )
            {
                string customErrorMessage;
                customErrorMessage = "Concurrency violation\n";
                customErrorMessage += ex.Row[0].ToString( );
                MessageBox.Show( customErrorMessage );
            }
            catch ( Exception ex )
            {
                MessageBox.Show( ex.Message );
            }
            finally
            {

                if ( newProfile != null )
                {
                    newProfile.Dispose( );
                }
                if ( editedProfile != null )
                {
                    editedProfile.Dispose( );
                }

                this.Close( );
            }
            if ( !_isNew )
            {
                DataRowView drv = (DataRowView)  profilesBS.Current;
                if ( drv["SiteID"] != null )
                {
                    int _siteID = (int) drv["SiteID"];  //
Convert.ToInt32( txtSiteID.Text.ToString( ) );

                    OnNewHorizonEdit( new NewHorizonEventArgs( _siteID )
);
                }
            }            

cheers

Michael M


By the way, doesn't Me.textbox.Value = "something" work?  If the dataset
is dirty, you update it.  I feel somehow that I'm missing the point of
the question.  Are you using a bindingnavigation control or what?

Charlotte Foust

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Michael
Maddison
Sent: Wednesday, March 12, 2008 11:17 PM
To: Discussion concerning Visual Basic and related programming issues.
Subject: Re: [dba-VB] Data Binding values from code

And the answer seems to be,
if you use the built in stuff, you don't.
You don't seem to be able to let the bindingsource know that the data
has changed.

I ended up doing a direct insert.

cheers

Michael M

Hi,

I've gone to databinding hell...
How do I change a value IN CODE in a textbox on a form and get it to be
written back to the db?

Using Dataset, BindingSource and TableAdaptor.

Typing in the textbox works just fine...

cheers


Michael M

_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com


_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com


_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com


_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com


_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com





More information about the dba-VB mailing list