|
-
Apr 15th, 2003, 07:39 AM
#1
Thread Starter
Member
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
-
Apr 15th, 2003, 12:31 PM
#2
-
Apr 15th, 2003, 05:43 PM
#3
Frenzied Member
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
-
Apr 15th, 2003, 06:00 PM
#4
Sleep mode
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
-
Apr 16th, 2003, 03:16 AM
#5
Thread Starter
Member
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?
-
Apr 16th, 2003, 03:25 AM
#6
Sleep mode
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 !
-
Apr 16th, 2003, 05:11 AM
#7
Thread Starter
Member
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?
-
Apr 16th, 2003, 06:28 AM
#8
Sleep mode
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:
Public Class Class1
'Libraries needed for running this Code
Imports System.Data
Imports System.Data.OleDb
'open Connection to the Database with Password
Dim MyPath As String = Application.StartupPath & "\__mydb__.mdb"
Dim MyPassword As String = "passme"
Dim StrSQL As String = "Select * From MyTable"
Dim MyConnection As New
OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
MyPath & ";Jet OLEDB:Database Password=" & MyPassword)
MyConnection.Open()
'This Saves data to a DataSet and then uses update method
'against Database Source File
Dim MyCommand As New OleDbCommand(StrSQL, MyConnection)
Dim MyDataset As DataSet = New DataSet()
Dim MyAdapter As New OleDb.OleDbDataAdapter(StrSQL, MyConnection)
MyAdapter.Fill(MyDataset, "MyTable")
Dim MyDataRow As DataRow = MyDataset.Tables("MyTable").NewRow
'Fill the data in Four Columns in the Database (Fields)
MyDataRow("1_Column") = Textbox1.Text
MyDataRow("2_Column") = Textbox2.Text
MyDataRow("3_Column") = Textbox3.Text
MyDataRow("4_Column") = Textbox4.Text
MyDataset.Tables("MyTable").Rows.Add(MyDataRow)
MyAdapter.Update(MyDataset, "MyTable")
MessageBox.Show("Data Saved..", "Saved",
'if you want to write the data as well to XML file then you
'include(this)before update method
MyDataset.WriteXml(Application.StartupPath & "\_MyXMLFile_.XML")
End Sub
End Class
'''''''''''''''''''''
'By Pirate
'..............
-
Apr 16th, 2003, 07:00 AM
#9
Thread Starter
Member
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:
Public Function addnew(ByVal iArt As Integer, _
ByVal iMandantID As Integer, _
ByVal iParentID As Integer) As dsBilanzaufbau
Try
Dim dr As SqlDataReader
' nächste freie SortID ermitteln
Dim iNextSortID As Integer
strSQL = "SELECT TOP 1 Sort_ID FROM tbl_cu_bilanzaufbau WHERE Mandant_ID = " & iMandantID & " AND Parent_ID = " & iParentID & " ORDER BY Sort_ID DESC"
dr = objSQL.getDataReader(strSQL)
If dr.Read() = True Then
iNextSortID = CInt(dr.Item("Sort_ID")) + 1
Else
iNextSortID = 1
End If
objSQL.closeDataReader(dr)
Dim dRow As DataRow
dRow = ds.tbl_cu_bilanzaufbau.NewRow()
objSQL.initDataRow(dRow)
dRow.Item("ID") = 0
dRow.Item("Mandant_ID") = iMandantID
Select Case iArt
Case 1
dRow.Item("Typ") = "O"
dRow.Item("Bezeichnung") = "neuer Ordner"
Case 2
dRow.Item("Typ") = "K"
dRow.Item("Bezeichnung") = "neue Bilanzzeile"
End Select
dRow.Item("Parent_ID") = iParentID
dRow.Item("Sort_ID") = iNextSortID
dRow.Item("AendBenutzer") = aktBenutzer
dRow.Item("AendTimestamp") = Format(DateTime.Now(), "dd.MM.yyyy") & " " & Format(DateTime.Now(), "HH:mm")
ds.tbl_cu_bilanzaufbau.Rows.Add(dRow)
strSQL = "SELECT TOP 1 * FROM tbl_cu_bilanzaufbau ORDER BY ID DESC"
da = objSQL.configAdapter(strSQL)
save()
ds.Clear()
' --->1
' read ID of the row added before
Dim iID As Integer
iID = objSQL.getLastID(strTablename, "ID")
' --->2
' fill dataset with row added before
ds = readByID(iMandantID, iID)
Return ds
Catch ex As Exception
objError.raiseSysError("cls_sql_bilanzaufbau", "", "save", ex.Message, "")
End Try
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...
-
Apr 16th, 2003, 07:23 AM
#10
Sleep mode
Well , I don't know why I don't like autonumber field . It doesn't make any sense to me at least .
-
Apr 16th, 2003, 07:36 AM
#11
Thread Starter
Member
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
-
Apr 16th, 2003, 07:54 AM
#12
Sleep mode
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|