none
Moving data into a table.

    Question

  • I am hesitant to even start a thread and hope that this will just be nothing more than a VB related issue and not something that becomes some kind of irrelevant issue.  So here goes.

    I have a form that have controls that are associated with fields from a number of tables in a Dataset. There are several ways that I have used to put control values into a table and they all work quite well.

    For bound fields I might do something like this:

                        'Update Item Number
                        Me.LkpMBItemNumTableAdapter.Fill(Me._MasterBase2_0ValMasterDataSet.lkpMBItemNum)
                        _MasterBase2_0ValMasterDataSet.lkpMBItemNum(0).intMBItemNumber = glbintValNum

    The reason I use the Fill() query is that this is a single element (row) table and will never have but one element. 

    Now it is my understanding this would place the value directly into the field/row of the table and then after an EndEdit and Update it would be saved and permanent.  Even if this is not the way it is done, it works.

    Additionally, for the above to work I have to make sure that in the form TableAdapterManager must reference the LkpMBItemNumTableAdapter in the TableAdapterManager properties.

    So here is my dilemma.  I used the methodology below to place a value into the table noted below:

                            Me.LnkMBChangeRequestTableAdapter.FillByCRNum(Me._MasterBase2_0ValMasterDataSet.lnkMBChangeRequest, glbintCRNum)
                            _MasterBase2_0ValMasterDataSet.lnkMBChangeRequest(0).strChgProject = glbstrProjType

    In this case the query filters to a single element (row) of the table and the second line is supposed to place the global value into the specified field in that row.  The problem is the following error is thrown. 

    _MasterBase2_0ValMasterDataSet.lnkMBChangeRequest(0) = '_MasterBase2_0ValMasterDataSet.lnkMBChangeRequest(0)' threw an exception of type 'System.IndexOutOfRangeException'

    Now the only reason I can think of for this error is that I have not yet saved the data and this row does not yet actually exist.  While that does not really make sense to me, it seems plausible.  I have no control on the form that is bound to this field but would like to be able to input the value into the table without have a control that I just don't need.

    So on the assumption that I have at least marginally explained the situation (as I understand it), is there anyone here who might be able to assist me without it causing some form of forum warfare?


    gwboolean

    Tuesday, May 2, 2017 3:37 AM

Answers

  • GW,

    What caught my attention in that one is that he's using a DataView.

    The DataView is how it's bound. From that link:

    "Changes to the DataView’s data will affect the DataTable. Changes to the DataTable’s data will affect all DataViews associated with it."


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by gwboolean Thursday, May 4, 2017 3:35 PM
    Thursday, May 4, 2017 1:46 AM
  • Reed

    I think I know what you are saying (although I am not entirely sure). If what you are talking about is placing a variable value into a control, I do that all of the time (see example below). 

    However, what I have is a field with no data bound control associate with with it, and I want to put a variable value directly into that field that has no bound control....



    You don't have to make a new control... the Form is a control. If you expose the field variable as a property of the Form, you can then bind to the Form and property. For example:

    Public Class Form1
    
        Private _TheField As String
        Public Property TheField As String
            Get
                Return _TheField
            End Get
            Set(value As String)
                _TheField = value
            End Set
        End Property
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataBindings.Add("TheField", dataSource, "dataMember")
        End Sub

    Here we have a property called "TheField" which has been bound to some [dataSource].

    I think the choice of how to handle this really comes down to the reason for the field value in the first place.  How it is used throughout the program and when/how its value changes are some of the deciding factors.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by gwboolean Thursday, May 4, 2017 3:31 PM
    • Unmarked as answer by gwboolean Thursday, May 4, 2017 3:32 PM
    • Marked as answer by gwboolean Thursday, May 4, 2017 3:35 PM
    Thursday, May 4, 2017 11:58 AM
    Moderator

All replies

  • OK, I played around with this a little more and now understand what is going on.

    I previously mentioned that I get an error when attempting to place the value into a field in a specific row because it might be that the row does not exist yet.  So what I did was place the query and line command as shown in the second example above after the EndEdit() and Update() and the value did indeed get placed into the table where I wanted it with no error.  While that does solve the problem, I would still like to know how one would handle saving a value that is a variable, instead of a bound control into a table.  I think what I might be asking is whether a variable can be bound?


    gwboolean


    • Edited by gwboolean Tuesday, May 2, 2017 5:01 PM
    Tuesday, May 2, 2017 3:53 AM
  • Hi gwboolean,

    Based on your description, can you please tell me that you want to save some data( you have modified) into the DataBase? If yes, please refer to:

    https://msdn.microsoft.com/en-us/library/xzb1zw3x(v=vs.100).aspx

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 3, 2017 9:29 AM
    Moderator
  • GW,

    In addition to Cherry, if you're using a BindingSource then be sure to call its .EndEdit method first. It won't see any changes otherwise.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, May 3, 2017 11:11 AM
  • Frank and Cherry, Actually, the first part I already took care of.  However, what I am wanting to know now is how, or if, one might take a variable, and assign it a binding source to a field in the table?

    Frank, I was using a binding source.  My problem was that I was trying to put data into a field after I had run the EndEdit.  Not a feasible approach.  That was an easy fix, once I figured it out.  


    gwboolean

    Wednesday, May 3, 2017 2:45 PM
  • Frank and Cherry, Actually, the first part I already took care of.  However, what I am wanting to know now is how, or if, one might take a variable, and assign it a binding source to a field in the table?

    Frank, I was using a binding source.  My problem was that I was trying to put data into a field after I had run the EndEdit.  Not a feasible approach.  That was an easy fix, once I figured it out.  


    gwboolean

    GW,

    Have a look at this on Stack Overflow:

    http://stackoverflow.com/questions/12016507/how-to-update-table-using-bindingsource

    That's a pretty good example I think. If it's not what you want though, be patient and let some of the good database people here help with it.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, May 3, 2017 2:56 PM
  • Frank, thanks.  I can see how that can work.  However, before I mark that as the answer (which it most certainly is), I would like to see what other approaches there are to solving the same problem.  So I will wait awhile before marking this answered.

    gwboolean

    Wednesday, May 3, 2017 7:56 PM
  • Frank, thanks.  I can see how that can work.  However, before I mark that as the answer (which it most certainly is), I would like to see what other approaches there are to solving the same problem.  So I will wait awhile before marking this answered.

    gwboolean


    I agree with your thinking. :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, May 3, 2017 7:59 PM
  • Frank and Cherry, Actually, the first part I already took care of.  However, what I am wanting to know now is how, or if, one might take a variable, and assign it a binding source to a field in the table?

    Frank, I was using a binding source.  My problem was that I was trying to put data into a field after I had run the EndEdit.  Not a feasible approach.  That was an easy fix, once I figured it out.  


    gwboolean

    You can bind a control property to a data source. So if you can expose your variable as a property of a control, you can bind to it.  So for example, you could add a new public property to the Form and then bind to that property value. Your mileage may vary depending on the types involved and the actual usage of the bound value.

    I think that in this case it would be more traditional to have "read" and "update" methods of your own to handle dealing with the variable value.  The "read" method can determine if the value needs to be read from the database or if a cached value can be returned instead.  The "update" method can update both the database and the cache of the value.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 3, 2017 8:36 PM
    Moderator
  • @Reed and @Cherry and others who find this thread,


    Many persons have trouble with the word "binding", in the .Net it is mostly meant as an abbreviation for "data binding".  (While there is also used late binding which is something different as we know).


    Success
    Cor

    Wednesday, May 3, 2017 8:55 PM
  • Cor,

    In this case it is databinding.

    Reed

    I think I know what you are saying (although I am not entirely sure). If what you are talking about is placing a variable value into a control, I do that all of the time (see example below). 

    However, what I have is a field with no data bound control associate with with it, and I want to put a variable value directly into that field that has no bound control.  Now I know that I could create a bound control for the field and just make that control visible property value = False and be able to do everything I want.  However, I really believe that to be a poor way to achieve what I want to do.

    Now here is what I have actually done so far:

                        Me.LnkMBChangeRequestTableAdapter.FillByCRNum(Me._MasterBase2_0ValMasterDataSet.lnkMBChangeRequest, glbintCRNum)
                        _MasterBase2_0ValMasterDataSet.lnkMBChangeRequest(0).strChgProject = glbstrProjType
                        Me.LnkMBChangeRequestBindingSource.EndEdit()
                        Me.TableAdapterManager.UpdateAll(Me._MasterBase2_0ValMasterDataSet)
     

    Which is OK, but still not what I believe to be the most efficient approach.

    I like what I saw that Frank setup, I just have not had a chance to try that yet, but I do believe that it to be a good solution to what I am after.  I was just hoping to see a few other approaches that might be as good as that one.


    gwboolean



    • Edited by gwboolean Thursday, May 4, 2017 1:31 AM
    Thursday, May 4, 2017 1:29 AM
  • GW,

    What caught my attention in that one is that he's using a DataView.

    The DataView is how it's bound. From that link:

    "Changes to the DataView’s data will affect the DataTable. Changes to the DataTable’s data will affect all DataViews associated with it."


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by gwboolean Thursday, May 4, 2017 3:35 PM
    Thursday, May 4, 2017 1:46 AM
  • Reed

    I think I know what you are saying (although I am not entirely sure). If what you are talking about is placing a variable value into a control, I do that all of the time (see example below). 

    However, what I have is a field with no data bound control associate with with it, and I want to put a variable value directly into that field that has no bound control....



    You don't have to make a new control... the Form is a control. If you expose the field variable as a property of the Form, you can then bind to the Form and property. For example:

    Public Class Form1
    
        Private _TheField As String
        Public Property TheField As String
            Get
                Return _TheField
            End Get
            Set(value As String)
                _TheField = value
            End Set
        End Property
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataBindings.Add("TheField", dataSource, "dataMember")
        End Sub

    Here we have a property called "TheField" which has been bound to some [dataSource].

    I think the choice of how to handle this really comes down to the reason for the field value in the first place.  How it is used throughout the program and when/how its value changes are some of the deciding factors.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by gwboolean Thursday, May 4, 2017 3:31 PM
    • Unmarked as answer by gwboolean Thursday, May 4, 2017 3:32 PM
    • Marked as answer by gwboolean Thursday, May 4, 2017 3:35 PM
    Thursday, May 4, 2017 11:58 AM
    Moderator
  • Frank,

    It took me awhile to work through it, but DataView is a very cool solution to a number of problems I have had and have yet to have.  A bit of an overkill for the piddly little issue here (almost embarrassing to have that problem with this gun in my holster).

    Reed,

    That is exactly what I am looking for to resolve this exact problem.

    Thanks both of you


    gwboolean


    • Edited by gwboolean Thursday, May 4, 2017 3:34 PM
    Thursday, May 4, 2017 3:31 PM
  • Frank,

    It took me awhile to work through it, but DataView is a very cool solution to a number of problems I have had and have yet to have.  A bit of an overkill for the piddly little issue here (almost embarrassing to have that problem with this gun in my holster).

    Reed,

    That is exactly what I am looking for to resolve this exact problem.

    Thanks both of you


    gwboolean


    Sorry for the delay.

    I'm glad that it helped. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, May 4, 2017 11:16 PM