I'm almost done with my big database class . anyways , it's reuseable except one function that saves to db . I used this code below but it it doesn't save all the data to the same record . It follow this sequence :
1
_ 2
__ 3
___ 4
____ 5 etc
Thanks in advance !
[Highlight=VB]
see the template
Last edited by Pirate; Mar 23rd, 2003 at 03:58 PM.
I have to say I'm no expert at ADO.NET (still learning for my 70-306 exam) but I have noticed the below line of code:
mydataset.Tables(TableStr).Rows.Add(MyDataRow)
In the way you have coded it, it will add a new row to the table per field that is passed in. Possible solutions:
* The function checks for the existence of a record via a unique identifier, and then inserts/updates accordingly.
* Separate functions for inserting and updating records.
* All the fields could be inserted/updated at once but the code could be tricky if you want to make your class truly scalable.
Also, another suggestion for your function is to make it more scalable in the way you pass in controls. With your code, you are assuming that every control you pass in has a .text property, this might not always be the case, what about Radio Buttons and Checkboxes?
I dont think it would be too slow even if you had a lot of columns. If you notice, the only time you are going to the database is after the information is already gathered.
In your example, you were hitting the database on every single add statement. If anything, it would speed up the processing.
If you want to keep doing it the way you had it, then you need to create a new row outside of your "SaveDB" function. You should only write things to the db or the datarow when you have the entire row stored in memory.
The reason you are getting your
1
_ 2
__ 3
___ 4
____ 5 etc
effect is because you are creating a new row everytime you add a column. You need to create a new row once and then keep adding things to that row. Once you are done with the row, then update the DB.
You are defining a new dataset called fill_dataset. This dataset does not contain any datatable (you have not added any) so you cant make a refrence to t_table.thats why the first exception is thrown.
'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
Sure, let me review the posts carefully and see what i could do about it, however it will be great if you clearly tell me what you have (connections, datasets...) and what you need.
Thanks
'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
hmm , I'll do my best to describe it , if you still unable to get it , notify me
You see this piece of code :
VB Code:
MyDataRow("C_URL") = ctrls(0).Text
MyDataRow("C_username") = ctrls(1).Text
MyDataRow("C_password") = ctrls(2).Text
MyDataRow("C_comment") = ctrls(3).Text
It gets data FROM four textboxes and saves them TO four Columns in the database . It's working , but I need it to be more scalable . Meaning , I can add data in the future without rewriting soure code . Got it ?
by any means - that you are convinient with- gather an array of objects and instead of ctrls() pass that array -that I call it desiredvalues()- to your function. This array lenght should be the same as number of the fileds in that table and the order you add data to it, the same as you like it to be added to your table.
Then in your class just write this line of code instead of that you mentioned above:
VB Code:
MyDataRow.ItemArray = desiredvalues
'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
Originally posted by WuzZeH I have to say I'm no expert at ADO.NET (still learning for my 70-306 exam) but I have noticed the below line of code:
In the way you have coded it, it will add a new row to the table per field that is passed in. Possible solutions:
* The function checks for the existence of a record via a unique identifier, and then inserts/updates accordingly.
* Separate functions for inserting and updating records.
* All the fields could be inserted/updated at once but the code could be tricky if you want to make your class truly scalable.
Also, another suggestion for your function is to make it more scalable in the way you pass in controls. With your code, you are assuming that every control you pass in has a .text property, this might not always be the case, what about Radio Buttons and Checkboxes?
Hope that helps.
Can you guess what does this guy mean by "Radio Buttons and Checkboxes" ?
I really need ideas to have more scalable database . I don't want to build my code again . I overloeaded a lot of functions for search , delete , addition , working on editing now ,
Do you think I am missing something important ??
Thanks
Let me tell you what he means:
In your code you passed the 'Control' to your function, and there used its .Text property, but there are cases that you dont want to pass text, you may need to pass a True/False (based on a radio button or check box) or DateTIme based on a DateTimePicker to your database. In such cases that code was useless. But with the current code you pass your array of objects to the save function and you shoulnt face any problem I guess.
'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