Link to home
Start Free TrialLog in
Avatar of TechLad
TechLadFlag 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 ?
Avatar of plusone3055
plusone3055
Flag of United States of America image

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
Avatar of 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.
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

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
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....
Avatar of 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 :(
Is there a CustomerTableAdapter in your fdAppCustomer form?
Avatar of 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
Private ObjFrm as Form=nothing
the Initialize is already called
Avatar of TechLad

ASKER

Hmm maybe you can look to see whats going on
fdAppCustomer.vb
fdAppCustomerList.vb
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
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

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
Avatar of TechLad

ASKER

Okay still having problems with the save :( I've attached a screenshot
Untitled.jpg
Delete the fill method from the form Load
Avatar of TechLad

ASKER

Still get the same error :(
Hi
Did you just Copy Paste Code From post
https://www.experts-exchange.com/questions/28087946/VB-2010-textbox-databinding.html?anchorAnswerId=39053672#a39053672
and doesn't work?
I tried it and work OK...Can you please post once more your  Code?
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

Avatar of 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

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
Avatar of TechLad

ASKER

Nope I still get the same error on the same line.
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
Avatar of 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
Avatar of 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.
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
Avatar of 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

Hi ....
What Items?
Avatar of 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.
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
Avatar of TechLad

ASKER

I'm struggling with that code :(
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 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?
Avatar of TechLad

ASKER

What I'm confused on is this line

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

Open in new window

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..
Avatar of 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.
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

Avatar of 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

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

ASKER

I've attached the forms with the code I'm trying to achieve
Program.zip
hi TechLad ..I will take a look in code and i will back to you ...
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?
Avatar of 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

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

Avatar of 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 ?
the fmappcasesview is opened? if no..Post me the code how you open the fmappcasesview form
I think you can not access controls of form in constructor. Move that code to Form.Load or Form.Shown event instead.
Avatar of TechLad

ASKER

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

ASKER

Anyone ?
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
Avatar of 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.
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"
Avatar of 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
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial