Visual Basic > Visual Basic Forums > Visual Basic General > Basic questions on databound ussage
Ask a questionAsk a question
 

AnswerBasic questions on databound ussage

  • Monday, November 02, 2009 5:10 PMRyn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    This problem must be so basic that it’s unbelievable.

     

    I have a 'ClientInfo' data table from where various fields are displayed in databound textboxes.   This can be navigated with a normal BindingNavigator.  One of the fields in the ClientInfo table is the ‘ClentId’, which is also in a ‘BankDetails’ table. 

    I want to display fields from the ‘BankDetails’ for the current client (matching ‘ClentId’) as selected by the BindingNavigator.

     

    I am trying to do as much as possible with data-binding as opposed to code.

     

    I can’t figure out how to get a field from of the current row (in ‘ClientInfo’) when the field is not bound to a textbox (and I am not going to bind it just because of my own stupidity).

     

    I also can’t figure out how to select the row in the ‘BankDetails’ that contains the matching ‘ClientId’ in the ‘BankDetails’ table without using a BindingNavigator.  

    (I cant find documentation on the properties etc of the various data binding objects).

     

    I am relatively experienced with VB, but very much a novice with database centric stuff.

     

    Using VS 2008. Please use VB for any examples, I’m still avoiding c#.


    Ryn

Answers

  • Tuesday, November 03, 2009 8:45 PMDig-Boy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I think you only need to look at the Current property on your binding source -- which is the current object bound to the fields.  You may need to convert it to the type of your bound object, but you can there access the entire record including the ClientId.

    • Marked As Answer byRyn Wednesday, November 04, 2009 9:02 AM
    •  
  • Tuesday, November 03, 2009 10:06 PMMalange Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    No its not about ID's. As I said in my initial post "This problem must be so basic that it’s unbelievable. " I'm sure I'll kick myself if  when I figure it out!!

    It is about getting the value of a field. ClientID is a column in a table and I can bind its value to a textbox. But I dont want to bind it, I just want to get it.  If this table contains colums x, y, z I simply want to get the value of x, y or z for the current row, like 

    dim value as string = something.Z

    What should  this "something" be? 

    The following seemed logical but does not work:

    dim value as string = myDataSet.myTableName.Z  '(where myDatasSet and myTableName are valid names)


    If I bind column Z to textboxZ then I can get Z as

    dim value as string = textboxZ.text

    But I dont want to bind it.




     
    Ryn

    Ryn, if you use the code I posted you will be able to achiev it.
    and you can asign it to your textbox or string

    like:

     Me.TextBox3.DataBindings.Add("Text", Me.BindingSource1, "ID", True)

    whatever you want:

    you can use sqldatareader as well like:

     Me.TextBox3.Text=("Text", Me.BindingSource1, "ID", True)


    or

     

    With RReader

     

    If .HasRows Then

    .Read()

     

    Dim CounteMe As Integer = .FieldCount

     

     Me.TextBox4.Text =.Item("ID").ToString

    End If

    End With


    Don't judge me, just Upgrade me. Thanks!
    • Marked As Answer byRyn Wednesday, November 04, 2009 9:05 AM
    •  

All Replies

  • Monday, November 02, 2009 8:27 PMMalange Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    why you dont use select to get it from the table?

    Dim Query As String="SELECT *
    FROM   employee
           RIGHT JOIN department
              ON employee.DepartmentID = department.DepartmentID
    WHERE  employee.DepartmentID=@ClientID.DepartmentID=@ClientID
    you can use with bindingsource.
    Using CNN as New SqlCommand(connectionString)
    Dim command As New Sqlcommand(query, CNN)
    CNN.Open()
    command.parametrs.AddNewValue("@ClientID",Me.textme.text)
    Dim RReader As SqlDataReader=command.ExecuteReader()
    With RReader
    If .HasRows Then
    .Read()
    Dim BindingMe As New BindingSource
    BindingMe.Dataource=RReader
    .......use the rest here..
    End IF
    End with
    OBS: You can use Where in your select String....
    or you can use link
    Dim Client = for Each Database in Rows.....
    something like that.....





















    "

    Don't judge me, just Upgrade me. Thanks!
  • Tuesday, November 03, 2009 5:55 AMRyn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    why you dont use select to get it from the table?


    Don't judge me, just Upgrade me. Thanks!

    Because I'm ignorant and batteling to find the right answers. I will try your suggestion thanks.
    Ryn
  • Tuesday, November 03, 2009 7:25 AMCor LigthertMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Ryn,

    If you are thinking about databases there is always in this situation a parent and there are childs more to more is as well possible but mostly more dificult.

    However, you have to make one forever the leading one which you bind to your textbox, you simply cannot bind anything to 2 tables.

    In principle you can bind two database properties to each other but I always avoid it, it gives often unpredictable results.


    Success
    Cor
  • Tuesday, November 03, 2009 9:08 AMRyn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks Cor,

    Maybe my question was not clear, let me try again.

     

    I have text fields bound to both ‘ClientInfo’ and ‘BankDetails’ and I can navigate them with a binding navigator. Here the ClientInfo is the parent and BankDetails is a child.  I can bind ‘ClientInfo – ClientID’ to a textbox and show this. I can also get the ClientID from the bound textbox text property as in

     

    Dim client as string = ClientIdTextbox.Text

     

    But how do I get the ClientID if is do not have it bound to a text filed?  (Why should I bind it if I don’t plan to show it?)

     

    Logic say there must be an object such that:

     

    Dim client as string = object.ClientId

     

    But I can’t find this object or am I barking UP the wrong tree?  

     

    Also, if I have ClientID (from ClientInfo) I want the text fields bound ‘BankDetails’ to show the row that contains the ClientID (in the BankDetails) table. I suppose I can set the filter property on the BankDetailsBindingSource, but the help text is a bit unclear. I think it should be

     

                    BankDetailsBindingSource.Filter = "ClientID = client”

     

    Does this make sense?


    Ryn
  • Tuesday, November 03, 2009 11:54 AMCor LigthertMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Ryn,

    As it is about ID's then it is important in .Net using AdoNet to know how you have created that ID.

    Is it a GUID? is it an automatic incremental integer? are you sure that you did set the primary key? those things you have to give if you have questions about ID's,

    You are right that you should never bind an database ID to a control.


    Success
    Cor
  • Tuesday, November 03, 2009 1:04 PMRyn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    No its not about ID's. As I said in my initial post "This problem must be so basic that it’s unbelievable. " I'm sure I'll kick myself if  when I figure it out!!

    It is about getting the value of a field. ClientID is a column in a table and I can bind its value to a textbox. But I dont want to bind it, I just want to get it.  If this table contains colums x, y, z I simply want to get the value of x, y or z for the current row, like 

    dim value as string = something.Z

    What should  this "something" be? 

    The following seemed logical but does not work:

    dim value as string = myDataSet.myTableName.Z  '(where myDatasSet and myTableName are valid names)


    If I bind column Z to textboxZ then I can get Z as

    dim value as string = textboxZ.text

    But I dont want to bind it.




     
    Ryn
  • Tuesday, November 03, 2009 6:48 PMCor LigthertMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'm not sure what you are doing, but I get the idea that you need the currencymanager.


    http://www.vb-tips.com/CurrencyManager.aspx


    It does not look direct to your problem, but you can of course use the index to set the current position in the datatable and set then the column value to your textbox.

    Be aware, this approach from mixed databinding and setting gives mostly much disapointing.



    Success
    Cor
  • Tuesday, November 03, 2009 8:45 PMDig-Boy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I think you only need to look at the Current property on your binding source -- which is the current object bound to the fields.  You may need to convert it to the type of your bound object, but you can there access the entire record including the ClientId.

    • Marked As Answer byRyn Wednesday, November 04, 2009 9:02 AM
    •  
  • Tuesday, November 03, 2009 10:06 PMMalange Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    No its not about ID's. As I said in my initial post "This problem must be so basic that it’s unbelievable. " I'm sure I'll kick myself if  when I figure it out!!

    It is about getting the value of a field. ClientID is a column in a table and I can bind its value to a textbox. But I dont want to bind it, I just want to get it.  If this table contains colums x, y, z I simply want to get the value of x, y or z for the current row, like 

    dim value as string = something.Z

    What should  this "something" be? 

    The following seemed logical but does not work:

    dim value as string = myDataSet.myTableName.Z  '(where myDatasSet and myTableName are valid names)


    If I bind column Z to textboxZ then I can get Z as

    dim value as string = textboxZ.text

    But I dont want to bind it.




     
    Ryn

    Ryn, if you use the code I posted you will be able to achiev it.
    and you can asign it to your textbox or string

    like:

     Me.TextBox3.DataBindings.Add("Text", Me.BindingSource1, "ID", True)

    whatever you want:

    you can use sqldatareader as well like:

     Me.TextBox3.Text=("Text", Me.BindingSource1, "ID", True)


    or

     

    With RReader

     

    If .HasRows Then

    .Read()

     

    Dim CounteMe As Integer = .FieldCount

     

     Me.TextBox4.Text =.Item("ID").ToString

    End If

    End With


    Don't judge me, just Upgrade me. Thanks!
    • Marked As Answer byRyn Wednesday, November 04, 2009 9:05 AM
    •  
  • Wednesday, November 04, 2009 9:07 AMRyn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Malange,

    I did not realise one could bind things at run time.

    Origional problem solved.
    Ryn