Avatar of TechLad
TechLad
Flag for United Kingdom of Great Britain and Northern Ireland asked on

VB 2010 textbox databinding

Hello,

I've just moved to VB 2010 and just finding a few things difficult to get my head round. I previously had VB6 and what I'm after if some assistance on something.

On my VB6 program I had a form with a data grid which had a ADO but it was on the second form the textboxes populated with the database. It went something like this:

Private Sub DATA()
Set Textbox1.DataSource = Form1.ADODC1
Textbox1.Datafield = "FieldName"
End sub

So far on VB 2010 I've managed to get this

fdAppCustomerList Form:
 (This has the datagrid on it)
fdAppCustomer.fdCustomerID.Text = Me.DataGridView1.CurrentRow.Cells(0).Value.ToString

However, I'm not sure if that is correct because the code I've used to save changes isn't saving.

fdAppCustomer Form:
(This form displays the previous form datagrid fields into text boxes)
fdAppCustomerList.CustomersTableAdapter.Update(fdAppCustomerList.MY_CRM_Data_Set.Customers)

Can anyone assist me ?
Visual Basic.NETMicrosoft SQL ServerProgramming.NET ProgrammingMicrosoft Development

Avatar of undefined
Last Comment
John (Yiannis) Toutountzoglou

8/22/2022 - Mon
plusone3055

John (Yiannis) Toutountzoglou

John (Yiannis) Toutountzoglou

Try Also these lines before calling the table adapter update..
Me.Validate
Me.CustomersBindingSource.EndEdit
fdAppCustomerList.CustomersTableAdapter.Update(fdAppCustomerList.MY_CRM_Data_Set.Customers)

Open in new window

Also is your TextBox Databind?
textbox1.databindings.add....

Take a look here
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.databindings.aspx
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
TechLad

ASKER
I've bound the textboxes in the properties menu. I'm not sure if that's correct way of doing it but if it is it's still not saving changes.  :(

jtoutou: the code you supplied is exactly what Ive put.
John (Yiannis) Toutountzoglou

create a new instance in your fdAppCustomer
Prublic Sub New(ByVal PassedString As String)
InitilizeComponet()
Me.fdCustomerID.Text=PassedString
End Sub

Open in new window

In your DatagridView Form
Private ObjFrm as Form
'Before You call the fdAppCustomer 

Dim MyString=Me.DataGridView1.CurrentRow.Cells(0).Value.ToString
If ObjFrm Is Nothing then ObjForm=New fdAppCustomer(MyString)
ObjFrm.ShowDialog
ObjFrm=Nothing

Open in new window

Then in your Save Button ..in fdAppCustomer
 Me.Validate()
                Me.CustomersBindingSource.EndEdit
                If Me.MY_CRM_Data_Set.HasChanges Then
                    Dim row() As DataRow = Me.MY_CRM_Data_Set.Customers.Select("", "", DataViewRowState.Added Or DataViewRowState.ModifiedCurrent)
                    Me.CustomersTableAdapter.Update(row)

Open in new window

John (Yiannis) Toutountzoglou

Note that if you already your textbox is bounded via properties
the Fill method must be present also in fdAppCustomer form
Prublic Sub New(ByVal PassedString As String)
InitializeComponent()
Me.CustomersTableAdapter.Fill(MY_CRM_Data_Set.Customers)
Me.fdCustomerID.Text=PassedString
End Sub

Open in new window

and in fdAppCustomerList
after ObjFrm=nothing call again the Me.CustomersTableAdapter.Fill(MY_CRM_Data_Set.Customers) to "refresh" the datagridview
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
John (Yiannis) Toutountzoglou

If there are multiple textBoxes in fdAppCustomer that are fill by datagridview Row then the link i post you is exactly what you are looking for....
TechLad

ASKER
Yeah code works fine all fields seem to be populating correctly. Only annoying thing is even with the revised code it just doesn't seem to save the changes made. I'm honestly Lost :(
John (Yiannis) Toutountzoglou

Is there a CustomerTableAdapter in your fdAppCustomer form?
Your help has saved me hundreds of hours of internet surfing.
fblack61
TechLad

ASKER
Yep their is.

Ive just noticed these not sure if that has anyting and their only warning

Warning      1      'Public Sub New(PassedString As String)' in designer-generated type  'MY_CRM.fdappcustomer' should call initializeComponent method.
Warning      2      Variable 'ObjFrm' is used before it has been assigned a value. A null reference
John (Yiannis) Toutountzoglou

Private ObjFrm as Form=nothing
the Initialize is already called
TechLad

ASKER
Hmm maybe you can look to see whats going on
fdAppCustomer.vb
fdAppCustomerList.vb
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
John (Yiannis) Toutountzoglou

Good Day....
fdAppCustomerList
Public Class fdAppCustomerList
    Dim ObjFrm As Form=nothing
    
    Private Sub fdAppCustomerList_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MY_CRM_Data_Set.Customers' table. You can move, or remove it, as needed.
        Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)

    End Sub
    Private Sub cmdClose_Click(sender As System.Object, e As System.EventArgs) Handles cmdClose.Click
        Me.Close()
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs)
      If e.ColumnIndex =Me.DataGridView1.Columns(0).Index then     
            If Not IsDbNull(Me.DataGridView1.CurrentRow.Cells(0).Value) Or  Me.DataGridView1.CurrentRow.Cells(0).Value IsNot Nothing then 
  Dim MyString = Me.DataGridView1.CurrentRow.Cells(0).Value.ToString
        If ObjFrm Is Nothing Then ObjFrm = New fdAppCustomer(MyString)
        ObjFrm.Show()
        ObjFrm=Nothing
    End if 
 End if
End Sub
End Class

Open in new window

The fdAppCustomer form seems correct...But you have to add
Public Sub New(ByVal Str as String)
 InitializeComponent()
   Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)
   Me.TextBox1.Text=Str
End Class

Open in new window

Give it a try ...


Yiannis
John (Yiannis) Toutountzoglou

So for the Customers Form
Public Class fdAppCustomer

    Private Sub cmdClose_Click(sender As System.Object, e As System.EventArgs) Handles cmdClose.Click
        Close()
    End Sub

    Private Sub fdAppCustomer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MY_CRM_Data_Set.Customers' table. You can move, or remove it, as needed.
        Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)
    End Sub

    Private Sub SaveToolStripButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveToolStripButton.Click
        Me.Validate()
        Me.CustomersBindingSource.EndEdit()
        If Me.MY_CRM_Data_Set.HasChanges Then
            Dim row() As DataRow = Me.MY_CRM_Data_Set.Customers.Select("", "", DataViewRowState.Added Or DataViewRowState.ModifiedCurrent)
            Me.CustomersTableAdapter.Update(row)
            MessageBox.Show("Data Saved")
        End If
    End Sub

    Public Sub New(ByVal Str as String)
        InitializeComponent()
   Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)
   Me.TextBox1.Text=Str
    End Class
End Class

Open in new window

John (Yiannis) Toutountzoglou

ok..Take a look at here...
The CustomersList contains the datagridview..
The other form as i understand contains the details of the currentRow..
To Avoid overWriting Data you hve to determine the Customers BindingSource Position which is equals with the current row Index...
So in the Cell Content Click event...(just Copy And Paste)
Public Class fdAppCustomerList
    Dim ObjFrm As Form=nothing
    
    Private Sub fdAppCustomerList_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MY_CRM_Data_Set.Customers' table. You can move, or remove it, as needed.
        Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)

    End Sub
    Private Sub cmdClose_Click(sender As System.Object, e As System.EventArgs) Handles cmdClose.Click
        Me.Close()
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs)
     If Me.DataGridView1.CurrentRow IsNot Nothing then 
  Dim Pos As Integer= Me.DataGridView1.CurrentRow.Index
        If ObjFrm Is Nothing Then ObjFrm = New fdAppCustomer(Pos)
        ObjFrm.Show()
        ObjFrm=Nothing
    End if 
End Sub
End Class

Open in new window

So in your fdAppCustomer
Public Class fdAppCustomer

    Private Sub cmdClose_Click(sender As System.Object, e As System.EventArgs) Handles cmdClose.Click
        Close()
    End Sub

    Private Sub fdAppCustomer_Load(sender As System.Object, e As System.EventArgs) 
   'No need for Form Load event
 End Sub

    Private Sub SaveToolStripButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveToolStripButton.Click
        Me.Validate()
        Me.CustomersBindingSource.EndEdit()
        If Me.MY_CRM_Data_Set.HasChanges Then
            Dim row() As DataRow = Me.MY_CRM_Data_Set.Customers.Select("", "", DataViewRowState.Added Or DataViewRowState.ModifiedCurrent)
            Me.CustomersTableAdapter.Update(row)
            MessageBox.Show("Data Saved")
        End If
    End Sub

  Public Sub New(ByVal Pos as Integer)
        InitializeComponent()
   Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)
   Me.CustomersBindingSource.Position=Pos
    End Class
End Class

Open in new window


THIS MIGHT WORK FOR YOU ....
Yiannis
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
TechLad

ASKER
Okay still having problems with the save :( I've attached a screenshot
Untitled.jpg
John (Yiannis) Toutountzoglou

Delete the fill method from the form Load
TechLad

ASKER
Still get the same error :(
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
John (Yiannis) Toutountzoglou

Hi
Did you just Copy Paste Code From post
https://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28087946.html#a39053672
and doesn't work?
I tried it and work OK...Can you please post once more your  Code?
John (Yiannis) Toutountzoglou

Or just Replace
Dim row() As DataRow = Me.MY_CRM_Data_Set.Customers.Select("", "",   DataViewRowState.Added Or DataViewRowState.ModifiedCurrent)
            Me.CustomersTableAdapter.Update(row)

Open in new window

With
CustomersTableAdapter.Update(MY_CRM_Data_Set.Customers)

Open in new window

TechLad

ASKER
So in the fdappCustomer Form I have this code:

Public Class fdAppCustomer
    Public Sub New(ByVal Pos As Integer)
        InitializeComponent()
        Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)
        Me.CustomersBindingSource.Position = Pos
    End Sub

    Private Sub fdAppCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = Me.fdTitle.Text & " " & Me.fdFirstName.Text & " " & Me.fdLastName.Text
        Me.lblTitle.Text = "Customer:" & " " & Me.fdTitle.Text & " " & Me.fdFirstName.Text & " " & Me.fdLastName.Text
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Close()

    End Sub

    Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
        Me.Validate()
        Me.CustomersBindingSource.EndEdit()
        If Me.MY_CRM_Data_Set.HasChanges Then
            Me.CustomersTableAdapter.Update(MY_CRM_Data_Set.Customers)
            MessageBox.Show("Data Saved")
        End If
    End Sub

End Class

Open in new window



And also in the fdappcustomer list I have this
Public Class fdAppCustomerList
    Dim ObjFrm As Form = Nothing

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Close()

    End Sub

    Private Sub fdAppCustomerList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MY_CRM_Data_Set.Customers' table. You can move, or remove it, as needed.
        Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)

    End Sub

    Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
        If Me.DataGridView1.CurrentRow IsNot Nothing Then
            Dim Pos As Integer = Me.DataGridView1.CurrentRow.Index
            If ObjFrm Is Nothing Then ObjFrm = New fdAppCustomer(Pos)
            ObjFrm.Show()
            ObjFrm = Nothing
        End If
    End Sub
End Class

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
John (Yiannis) Toutountzoglou

seems correct ,,Give it a try removing the HasChanges Method
Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
        Me.Validate()
        Me.CustomersBindingSource.EndEdit()
        Me.CustomersTableAdapter.Update(MY_CRM_Data_Set.Customers)
       MessageBox.Show("Data Saved")
          End Sub

Open in new window


and for the  ObjFrm.Show() show it as Dialog ObjFrm.ShowDialog
TechLad

ASKER
Nope I still get the same error on the same line.
John (Yiannis) Toutountzoglou

try this..
1.Delete the fdAppCustomer form and create a new one with the same name
2.From The Datasources Select your Dataset and Expand it.Select the Customers Table And Change From DataGridView to Details
3.Drag the Items you need or All the Table itno your Form ..
4.Double Click Your Form To Launch the Code
5.Delete all the Code
6.Then Paste the All the Code I post you For the fdAppCustomer form ..


Yiannis
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Nasir Razzaq

>Nope I still get the same error on the same line.

UpdateCommand has not been generated for your adapter. Is your select command returning a primary key column? Is it using any joins? You can generate this command manually or use the commandbuilder object to create it.

http://msdn.microsoft.com/en-us/library/tf579hcz.aspx
TechLad

ASKER
I think I may of solved it but I'm still testing to be sure. I read up on an article which pointed me to look at the check box on the data source which says "Refresh data Table" I'll keep you posted on it.
John (Yiannis) Toutountzoglou

Check Also your Table Adapter Configuration Wizard
"Which methods do you want do add in the TableAdapter"
Check if the third CheckBox is Checked
"Create methods to send Updates...."

Yiannis
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
TechLad

ASKER
Hey guys.

The save feature is working now :) I have one other question it's not a major problem but is their any way of altering this section:

It's giving me a few problems with it being a Public sub not allowing me to call items on that page from other forms ?

    Public Sub New(ByVal Pos As Integer)
        InitializeComponent()
        Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)
        Me.CustomersBindingSource.Position = Pos
    End Sub

Open in new window

John (Yiannis) Toutountzoglou

Hi ....
What Items?
TechLad

ASKER
Well in one form I'm trying to insert text into a text box into the from which contains that code but it wont let me. If i take those lines of code out it works fine but then it effects the process.

I get Error Reference to a non-shared member requires an object reference.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
John (Yiannis) Toutountzoglou

try
 Public Sub New(ByVal Pos As Integer,ByVal Str As String)
        InitializeComponent()
        Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)
        Me.CustomersBindingSource.Position = Pos
        Me.TextBox1.Text=str
    End Sub

Open in new window


and when you call the form set:
If ObjFrm Is Nothing Then ObjFrm = New fdAppCustomer(Pos,ThestringYouWantToPass)

Open in new window


Yiannis
TechLad

ASKER
I'm struggling with that code :(
John (Yiannis) Toutountzoglou

I'm trying to insert text into a text box
you  can pass as many variables you want to the new for instance..
with The code above we pass the Datagridview Row index as integer variable to customers form.Also we pass a string variable which you can set the text you want to pass..

Yiannis
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Nasir Razzaq

>I get Error Reference to a non-shared member requires an object reference.

Do you use the New keyword to create instance of the form? Can you show that code where you get the error?
TechLad

ASKER
What I'm confused on is this line

If ObjFrm Is Nothing Then ObjFrm = New fdAppCustomer(Pos,ThestringYouWantToPass

Open in new window

John (Yiannis) Toutountzoglou

Just remember Why we are using this line...We are double click a datagridviewrow to edit the
the Current row and open fdAppCustomer with the details of the row...So each time we Double Click we create a new instance of fdAppCustomer form..
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
TechLad

ASKER
Ah my bad. I don't know If I have explained this correctly. What I've done is created a new form say called form1 and on this form theirs a text box Text1. So it goes like this,

fdAppCasesView.Text1.Text  = Me.Text2.Text

The problem being in form one we have this code:

    Public Sub New(ByVal Pos As Integer)
        InitializeComponent()
        Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)
        Me.CustomersBindingSource.Position = Pos
    End Sub

Open in new window


when removed this code works:

fdAppCasesView.Text1.Text  = Me.Text2.Text   If that public sub is in the form I get Error Reference to a non-shared member requires an object reference.
John (Yiannis) Toutountzoglou

the Public Sub New(ByVal Pos As Integer) is for fdAppCustomer form
For fdAppCasesView you can write this code in fdAppCasesView form
Public Sub New(ByVal Txt As String)
        InitializeComponent()
        Me.Text1.Text=Txt
    End Sub

Open in new window

So when you call the fdAppCasesView form create a new instance
If ObjFrm Is Nothing Then ObjFrm = New fdAppCasesView(Me.TextBox2.Text)

Open in new window

TechLad

ASKER
I get this error Error: Expression is a value and therefore cannot be the target of an assignment.

If ObjFrm Is Nothing Then ObjFrm = New fdAppCasesView(Me.TextBox2.Text)

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
John (Yiannis) Toutountzoglou

TechLad..
Can you please post your code as attatchment CustomerList,Customers aand appCeasesView?
TechLad

ASKER
I've attached the forms with the code I'm trying to achieve
Program.zip
John (Yiannis) Toutountzoglou

hi TechLad ..I will take a look in code and i will back to you ...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
John (Yiannis) Toutountzoglou

TechLand from the attatchment you post i can not understand where is the Problem
Please explain
initialy there were 2 forms
1. fdAppCustomer
2.fdAppCustomerList
The DatagridView is fdAppCustomerList and you click in a row and popup the fdAppCustomer
What are  frdAppCasesView and fdAppCustomerSearchList?
TechLad

ASKER
The problem is that on any other form if you try something like fdAppCasesView.Textbox1.text it will not allow you to see it any controls on that form. However if you remove the code below it will.

    Public Sub New(ByVal Pos As Integer)
        InitializeComponent()
        Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)
        Me.CustomersBindingSource.Position = Pos
    End Sub

Open in new window

John (Yiannis) Toutountzoglou

The code above create a new instance of the form you want to pass a value.
This code is added in fdAppCasesView?
If so change it
Public Sub New(ByVal Pos As Integer,ByVal str as string)
        InitializeComponent()
        Me.CustomersTableAdapter.Fill(Me.MY_CRM_Data_Set.Customers)
        Me.CustomersBindingSource.Position = Pos
        Me.Textbox1.text=str
    End Sub

Open in new window


So When you call the  fdAppCasesView form call it as :
New fdAppCasesView(YouRowIndex,YourTextYouWantToPass)

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
TechLad

ASKER
I'm doing something wrong because I can't get that code to do anything. I don't get this line.

New fdAppCasesView(YouRowIndex,YourTextYouWantToPass)

Open in new window


How does this work if I'm trying to do something like fmappcasesview.textbox1.text = me.textbox2.text ?
John (Yiannis) Toutountzoglou

the fmappcasesview is opened? if no..Post me the code how you open the fmappcasesview form
Nasir Razzaq

I think you can not access controls of form in constructor. Move that code to Form.Load or Form.Shown event instead.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
TechLad

ASKER
Sorry guys I still can't make it work.
TechLad

ASKER
Anyone ?
John (Yiannis) Toutountzoglou

Hi TechLand ...
Because i am also confused..Please attach text files in your post ..and explain what exactly you want ..
1.What text do you need to pass .From form to form ..
2.Which form contains the datagridview and what values do you need to pass.
Explain with details and believe me i will try to solve your issue...

Yiannis
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
TechLad

ASKER
Well what I’m trying to do, now this may not be the correct way, is to cut down on the amount of forms I use by using the same form for adding records and editing records. I’m not sure if this may be the correct way of doing things or whether it would be better to just create a new form.

Because this project contains customer records what I’m trying to do is to take key parts of information from one form and insert it into textbox fields on another form.
Nasir Razzaq

Here is another approach. Leave the form constructor alone (remove any code that you added). Load the form and call Show method and then set all control values

Dim frm As New MyForm
frm.Show
frm.Textbox1.Text = "Text1"
frm.Textbox2.Text = "Text2"
John (Yiannis) Toutountzoglou

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
TechLad

ASKER
I've attached a test copy of the project for you to take a look at if you would please
MYTESTAPP.zip
ASKER CERTIFIED SOLUTION
John (Yiannis) Toutountzoglou

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question