Results 1 to 12 of 12

Thread: databinding to hidden textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Austria
    Posts
    52

    databinding to hidden textbox

    hi!

    I've a problem binding a datafield to a textbox which has the property visible set to false. After reading the data from the database the textbox doesn't contain a value... if i set visible to true it work's.

    I'm using the hidden textbox to store an ID-value for further db-operations (update, delete...) Is this a bug? How can i handle this problem?

    thanks
    Robert

  2. #2
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Unhappy

    Personally i never bind anything to a database, but i do i have a suggestion.

    It sounds like a bug to me, so i would suggest you leave the TextBox visible, and in the form load event, just change the .Left property of the TextBox to be off the left side of the form (ie: a value of -100).

    You might also want to turn off the tab ability for that control as well - just so the user can't get to it.
    ~Peter


  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Do not set the Visible Property to False Just hide that textbox on load event of form.
    Textbox1.Hide()
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    As another perfect option is : FORGET binding . It's messy . I've read a lot of articles say it's full of bugs . Another reason when you have your own code is to make your methods *reusable* .
    Just a thought

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Austria
    Posts
    52
    forget binding... ok... that's an idea! in vb6-projects i never used databinding but in .net i never had problems with it. BUT i do not like it.

    So what is the best way to 'forget it'?

    onload:
    - fill a dataset
    - assign the values of the dataset to the textboxes (or whatever)

    event - save:
    - assign the valuse of the textboxes to to dataset
    - update dataset

    before closing a form i always check if changes are made but i think this will not be a problem because doing it without databinding i also can check the dataset.haschanges-property.

    pirate you wrote about reusable methods... what do you mean with this?

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by escape0
    pirate you wrote about reusable methods... what do you mean with this?
    When you make your code reusable it means you don't have to rewrite the code all over again (could be little changes) . Just build these functions in a class or module then you can export it to dll file , whenever you need it just call that file . This saves time and effort .

    assigning values to db field/textbox is a peice of cake .See my exmple in this thread ...http://www.vbforums.com/showthread.p...hreadid=240656
    hope that's clear !

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Austria
    Posts
    52
    you're right... it's much easier to use own code instead of databinding but there is one problem i mentioned above.

    if the user quits the transaction (=form) i want to check if changes were made and ask the user if he want's to save them. i assign the values of the textboxes to the dataset-fields without checking whether the user changed them or not.

    the dataset.haschanges-property is always true... i think .net sets this property if only a value is assigned to a field even if it is the same value.

    anybody an idea how to solve this?

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Is your problem have to do with saving or editing data ?
    I built this class which shows your how to save to db .
    Put your verification and checking rules before updating it .
    VB Code:
    1. Public Class Class1
    2.  
    3.     'Libraries needed for running this Code
    4.         Imports System.Data
    5.         Imports System.Data.OleDb
    6.  
    7.     'open Connection to the Database with Password
    8.     Dim MyPath As String = Application.StartupPath & "\__mydb__.mdb"
    9.     Dim MyPassword As String = "passme"
    10.     Dim StrSQL As String = "Select * From MyTable"
    11.     Dim MyConnection As New
    12. OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
    13. MyPath & ";Jet OLEDB:Database Password=" & MyPassword)
    14.         MyConnection.Open()
    15.  
    16.     'This Saves data to a DataSet and then uses update method
    17.     'against Database Source File
    18.     Dim MyCommand As New OleDbCommand(StrSQL, MyConnection)
    19.     Dim MyDataset As DataSet = New DataSet()
    20.     Dim MyAdapter As New OleDb.OleDbDataAdapter(StrSQL, MyConnection)
    21.  
    22.  
    23.         MyAdapter.Fill(MyDataset, "MyTable")
    24.     Dim MyDataRow As DataRow = MyDataset.Tables("MyTable").NewRow
    25.     'Fill the data in Four Columns in the Database (Fields)
    26.         MyDataRow("1_Column") = Textbox1.Text
    27.         MyDataRow("2_Column") = Textbox2.Text
    28.         MyDataRow("3_Column") = Textbox3.Text
    29.         MyDataRow("4_Column") = Textbox4.Text
    30.         MyDataset.Tables("MyTable").Rows.Add(MyDataRow)
    31.         MyAdapter.Update(MyDataset, "MyTable")
    32.         MessageBox.Show("Data Saved..", "Saved",
    33.  
    34.     'if you want to write the data as well to XML file then you
    35.     'include(this)before update method
    36.         MyDataset.WriteXml(Application.StartupPath & "\_MyXMLFile_.XML")
    37.     End Sub
    38.  
    39. End Class
    40. '''''''''''''''''''''
    41. 'By Pirate
    42. '..............

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Austria
    Posts
    52
    the problem is to save the data... the user should only be asked to save if the data was changed.

    i think i've to check whether the value of the datasetfield is different from that of the textbox and only assign if there is a difference.

    in your example you add a row to the db... i do this in a similar way but i want to know which ID the added row gets from the db (autoimcrement field).

    this is my coding... in austria we would say 'with the cross around the curch' (means not efficient or complicated). maybe there's a much easier way!?!? (look at the comments --->1 & 2)

    VB Code:
    1. Public Function addnew(ByVal iArt As Integer, _
    2.                            ByVal iMandantID As Integer, _
    3.                            ByVal iParentID As Integer) As dsBilanzaufbau
    4.         Try
    5.             Dim dr As SqlDataReader
    6.  
    7.             ' nächste freie SortID ermitteln
    8.             Dim iNextSortID As Integer
    9.             strSQL = "SELECT TOP 1 Sort_ID FROM tbl_cu_bilanzaufbau WHERE Mandant_ID = " & iMandantID & " AND Parent_ID = " & iParentID & " ORDER BY Sort_ID DESC"
    10.             dr = objSQL.getDataReader(strSQL)
    11.             If dr.Read() = True Then
    12.                 iNextSortID = CInt(dr.Item("Sort_ID")) + 1
    13.             Else
    14.                 iNextSortID = 1
    15.             End If
    16.             objSQL.closeDataReader(dr)
    17.  
    18.             Dim dRow As DataRow
    19.             dRow = ds.tbl_cu_bilanzaufbau.NewRow()
    20.             objSQL.initDataRow(dRow)
    21.  
    22.             dRow.Item("ID") = 0
    23.             dRow.Item("Mandant_ID") = iMandantID
    24.             Select Case iArt
    25.                 Case 1
    26.                     dRow.Item("Typ") = "O"
    27.                     dRow.Item("Bezeichnung") = "neuer Ordner"
    28.                 Case 2
    29.                     dRow.Item("Typ") = "K"
    30.                     dRow.Item("Bezeichnung") = "neue Bilanzzeile"
    31.             End Select
    32.             dRow.Item("Parent_ID") = iParentID
    33.             dRow.Item("Sort_ID") = iNextSortID
    34.             dRow.Item("AendBenutzer") = aktBenutzer
    35.             dRow.Item("AendTimestamp") = Format(DateTime.Now(), "dd.MM.yyyy") & " " & Format(DateTime.Now(), "HH:mm")
    36.  
    37.             ds.tbl_cu_bilanzaufbau.Rows.Add(dRow)
    38.  
    39.             strSQL = "SELECT TOP 1 * FROM tbl_cu_bilanzaufbau ORDER BY ID DESC"
    40.             da = objSQL.configAdapter(strSQL)
    41.             save()
    42.             ds.Clear()
    43.  
    44.             ' --->1
    45.             ' read ID of the row added before
    46.             Dim iID As Integer
    47.             iID = objSQL.getLastID(strTablename, "ID")
    48.  
    49.             ' --->2
    50.             ' fill dataset with row added before
    51.             ds = readByID(iMandantID, iID)
    52.  
    53.             Return ds
    54.  
    55.         Catch ex As Exception
    56.             objError.raiseSysError("cls_sql_bilanzaufbau", "", "save", ex.Message, "")
    57.         End Try
    58.  
    59.     End Function

    As you see i have to read the row i added befor in a second step (ds = readByID(iMandantID,iID)). Is there a way to get the autoincremended ID-Value when updating the dataset?

    I know that this has nothing to do with my original problem but maybe you also had this problem some time ago...

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Well , I don't know why I don't like autonumber field . It doesn't make any sense to me at least .

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2002
    Location
    Austria
    Posts
    52
    ok but without autonumber-filds you've the same problem as me.

    my problem:
    1. save row with id = 0
    2. read row with highest id

    your problem:
    1. read row with highest id (=x)
    2. save row with id = (x+1)

    the time between step 1 and 2 are critical because in a multiuser-environment this could cause problems in the database. in my opinion this has to be a feature of ADO.NET in order to make such a db-operation secure.

    BUT... that's another story.

    thank you pirate for your help

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I'm still unable to well understand your problem (been working on such db apps more than 3years) . Establish your own rules of opening your database (for ex . exclusive use or not) and data entering (one or two or whatever at specific time with/out limited access ) . When you add new row , it will be added to the database (here you have to check first if it's existed already then prevent the user if not add new record ) . For me , I never consider what you posted as problem , never .



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