Results 1 to 14 of 14

Thread: [Resolved] [2008] Changing DataBindings Text Property for TextBoxes Using Code

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    101

    Talking [Resolved] [2008] Changing DataBindings Text Property for TextBoxes Using Code

    Sorry if that title didn't make much sense. I'm new to databindings and VB.NET - I used to use ADODC with VB6. Once I've set up databindings I can set a text box or other controls to link with a database's specific field by changing the Text property under Data > DataBindings. I want to change the field the text box links to at different times using code. How can I do this?
    Last edited by ShaunC193; Jan 16th, 2009 at 10:45 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    As far as I'm aware, once a Binding has been created you cannot change the property of the data source that it's associated with. You'd have to create a new Binding object to replace the existing one, which would look like this:
    vb.net Code:
    1. Me.TextBox1.DataBindings.Remove(Me.TextBox1.DataBindings("Text"))
    2. Me.TextBox1.DataBindings.Add("Text", dataSource, "new property name")
    That's an unusual thing to want to do, although not unheard of. Do you mind if I ask why?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    101

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    Sorry I'm replying late. I've been without access to VB.NET for a few days, so couldn't respond properly. I tried the code, but unfortunately got errors. First, I'll explain why I want to do this. For the second year of my college system, I'm expanding upon my supermarket management system.



    I've designed a tab interface and have done the programming in VB.NET. Switching from tab to tab works fine. When switching from tab to tab though, the same text boxes that are on top of the tab control remain in place. Hopefully you can see what I'm getting at here. This is the code I used to test it...

    Code:
    Me.DataBindings.Remove(Me.txtBox1.DataBindings("ColleagueID"))
    Me.txtBox1.DataBindings.Add("ColleagueHoursPerWeek", UberData, "Staff")
    Unfortunately, I get an error. See the attachment. I'm guessing I've done something stupid. And yes, I did link txtBox1 to ColleagueID before I compiled.


    Edit: Didn't realise the attachment would display it in my post like that - sorry it's so huge. =/
    Attached Images Attached Images  
    Last edited by ShaunC193; Jan 13th, 2009 at 06:36 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    Let's look at that line that's throwing the exception and analyse what it's doing. First, you're trying to get a Binding from a TextBox that is bound to the TextBox's ColleagueID property? Does the TextBox have a ColleagueID property? You're then trying to take that Binding from the TextBox and remove it from the form. That doesn't make sense either. I think you need to take another look at the code I posted because you haven't done what I did.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    101

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    Right, I can see the multiple stupid errors I made. First of all, I read it in a hurry and mustn't have took in the part about removing the Text property. D'oh. So I ended up thinking "Text" was something to replace. Secondly, even if that was the case I didn't type your code up properly. I can be a blundering fool at times.

    I might still be making stupid errors, but I've changed the code to what I believe it should/may be. Unfortunately I think I've done something wrong on the second line as I'm having problems. (I'm assuming dataSource is to be replaced with this.) Also, I'm wanting to make it bind to the ColleagueHoursPerWeek field in the Staff table in this case.


  6. #6
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    You should read the IntelliSense hints. When you typed "Me.txtBox1.DataBindings.Add(", it shows you that you must add an object with type 'System.Windows.Forms.Binding'. You instead entered 3 strings....
    It should be:
    Code:
    Me.txtBox1.DataBindings.Add(New Binding("Text", UberData, "ColleagueID"))
    Last edited by Hamish; Jan 15th, 2009 at 09:15 AM.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    Quote Originally Posted by Hamish
    You should read the IntelliSense hints. When you typed "Me.txtBox1.DataBindings.Add(", it shows you that you must add an object with type 'System.Windows.Forms.Binding'. You instead entered 3 strings....
    It should be:
    Code:
    Me.txtBox1.DataBindings.Add(New Binging("Text", UberData, "ColleagueID"))
    What Intellisense tells you is that there are seven overloads of the Add method, only one of which requires you to pass a Binding object. The other six have signatures that match the six overloads of the Binding constructor. There's no point creating a Binding explicitly and passing that to the Add method unless you need to create the Binding and add it in different methods. Otherwise you would always just let the Add method create the Binding object for you. It doesn't hurt to creat it yourself but why do this:
    vb.net Code:
    1. Me.txtBox1.DataBindings.Add(New Binging("Text", UberData, "ColleagueID"))
    when you can do this:
    vb.net Code:
    1. Me.txtBox1.DataBindings.Add("Text", UberData, "ColleagueID")
    and get the same result?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    101

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    I changed the code...

    Code:
    Me.txtBox1.DataBindings.Remove(Me.txtBox1.DataBindings("Text"))
    Me.txtBox1.DataBindings.Add(New Binding("Text", UberData, "ColleagueHoursPerWeek"))
    But I still get an unhandled exception error upon execution of the second line. Sorry I lack the experience to figure this one out. I can give you the code if you're generous enough to fix it.
    Last edited by ShaunC193; Jan 15th, 2009 at 08:45 AM.

  9. #9
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    Quote Originally Posted by jmcilhinney
    It doesn't hurt to creat it yourself but why do this:
    vb.net Code:
    1. Me.txtBox1.DataBindings.Add(New Binding("Text", UberData, "ColleagueID"))
    when you can do this:
    vb.net Code:
    1. Me.txtBox1.DataBindings.Add("Text", UberData, "ColleagueID")
    and get the same result?
    I stand corrected.

    Quote Originally Posted by ShaunC193
    But I still get an unhandled exception error upon execution of the second line. Sorry I lack the experience to figure this one out. I can give you the code if you're generous enough to fix it.
    What is the error message?
    Last edited by Hamish; Jan 15th, 2009 at 09:15 AM.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    101

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    Click here to see the error. I've got a feeling I've made a silly mistake, so if you prefer I can upload the source code onto my server. I just wish this didn't require such a workaround in the first place.

    I might consider changing the programming method entirely, and instead have the text boxes' text change by retrieving it from the database with code, and have the database's data changed by replacing it with the text boxes' text, if that made sense. That way I wouldn't be binding the text boxes, which isn't necessarily a requirement anyway.

    Oh, and I realise I should have changed the second line to jmcilhinney's recommendation, but since they produce the same result it shouldn't matter right now.
    Last edited by ShaunC193; Jan 16th, 2009 at 09:41 AM.

  11. #11
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    That error suggest that you don't have a column "ColleagueHoursPerWeek" in your DataSource.... do you?

    [edit]
    I went back up to your original explanation of what you want to do and see that you're emulating a tab control by changing the bindings on your text boxes when another 'tab' is selected. Just wanted to point out that there is a built-in tab control in VB.NET that you might want to check out.
    [/edit]
    Last edited by Hamish; Jan 16th, 2009 at 10:11 AM.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    101

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    I most certainly do. Unless it's not pointing to the staff table for some reason?



    Another computing student has told me about the tab control, but he's also aware that I'm generally an awkward and stubborn piece of work, and have to finish what I start. Thanks for the suggestion, but I've got quite far with this tab system and definitely want to see if I can make something great out of it... even if that means having to ask for a little too much help!

  13. #13
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    Hmmm. Not sure about this, but I think you should user UberData.Staff as DataSource for your binding then. You could have multiple fields with the same name in different tables in your database, so you need to be specific which one you want to use.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    101

    Re: [2008] Changing DataBindings Text Property for TextBoxes Using Code

    ...You fixed it.

    I'm an idiot - I can't believe I didn't even think of trying UberData.Staff. Hahaha. Thanks. It works flawlessly now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width