Results 1 to 31 of 31

Thread: Critical problem ??[Resolved by Lunatic3]

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Talking Critical problem ??[Resolved by Lunatic3]

    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.

  2. #2
    New Member
    Join Date
    Mar 2003
    Location
    UK
    Posts
    7
    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?

    Hope that helps.

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    The way I would approach it would be something like this:

    Add a function to your SaveDB class that just adds an attribute to the current row.

    Once you have added all of the attributes to the current row, then run a final routine called SaveDB which writes your data to the DB.

    Here is a fake example of what I mean:

    VB Code:
    1. Public Class clsSaveDB
    2.     Private arrData As ArrayList
    3.     Private mstrTable As String
    4.     Structure structData
    5.         Public txtData As String
    6.         Public txtColumn As String
    7.     End Structure
    8.     Public Sub New(ByVal strTable As String)
    9.         arrData = New ArrayList()
    10.         mstrTable = strTable
    11.     End Sub
    12.  
    13.     Public Function AddItem(ByVal txtData As String, ByVal txtColumn As String)
    14.         Dim tmp As structData
    15.         tmp.txtData = txtData
    16.         tmp.txtColumn = txtColumn
    17.         arrData.Add(tmp)
    18.     End Function
    19.  
    20.     Public Function SaveDB()
    21.         Dim strSQL As String
    22.         Dim x As Long
    23.         strSQL = "INSERT INTO " & mstrTable & " ("
    24.         For x = 0 To arrData.Count - 1
    25.             strSQL = strSQL & arrData(x).txtColumn & ","
    26.         Next
    27.         'remove the last comma
    28.         strSQL = Left(strSQL, strSQL.Length - 1) & ") VALUES ("
    29.         For x = 0 To arrData.Count - 1
    30.             strSQL = strSQL & "'" & arrData(x).txtData & "',"
    31.         Next
    32.         strSQL = Left(strSQL, strSQL.Length - 1) & ")"
    33.  
    34.         yourdb.execute(strSQL)
    35.  
    36.     End Function
    37. End Class
    38.  
    39. 'Calling code
    40.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    41.         Dim x As New clsSaveDB("table1")
    42.         x.AddItem("x", "7y")
    43.         x.AddItem("x", "7yz")
    44.         x.AddItem("x", "7ya")
    45.         x.AddItem("x", "7yt")
    46.  
    47.         x.SaveDB()
    48.  
    49.     End Sub

  4. #4

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Thanks a lot both of you guys . I will try either ways and feeb back here .

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Negative0 , your trick is cool but don't you thing this would make all this process slow ?? Since you've used Loop and arrays ?
    Thank you

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    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.

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    That's right . but ...

    Hmmm , how would change this line to save values to datarow

    VB Code:
    1. MyDataRow(ColumStr) = ctrl.Text

    How to save these values to db ??
    Thank you dude

  8. #8
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    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.

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    umm , I'm ganna follow your code . Let me figure how to add the array to datarow obj .
    I really appreciate that Negative0 .

  10. #10

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It looks hard to use DataRow in this case I'm not sure though. any hints please ?

  11. #11

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    see the template ....
    Last edited by Pirate; Mar 23rd, 2003 at 01:15 PM.

  12. #12
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  13. #13

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It's declared and being filled in "Additem_datarow" function . I guess all this crap won't work . Do you have any other code to share Lunatic3 ?

  14. #14
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  15. #15

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Thank you my friend Lunatic3 .

    I'm ganna create a little simulation for this situation and send it here so that it becomes more easier to try on the same proj !

  16. #16

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Ok , here is a template proj that we can both play with it . Thanks for your time.
    Attached Files Attached Files

  17. #17

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    just to be notified , that I've used XP access . It should be compatiable with Access 2000 .

  18. #18
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I get no error running your project and the data are added just fine! do you face errors??
    '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

  19. #19

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Lunatic3 , do you know what I'm talking about ? This is only sample of my code .I know it's running well .

    Please read the post carefully , if you didn't understand what I need then , let me know and I will explain that in details .
    Thanks

  20. #20
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I thought your problem was that it added the fileds in this way:
    1
    -- 2
    ----3
    ......

    so please describe more, sorry for being dumb
    '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

  21. #21

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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:
    1. MyDataRow("C_URL") = ctrls(0).Text
    2. MyDataRow("C_username") = ctrls(1).Text
    3. MyDataRow("C_password") = ctrls(2).Text
    4. 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 ?

  22. #22
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Ok, see if it is what you want:

    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:
    1. 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

  23. #23

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I'm sorry I don't follow !! I can understand code rather than talks .
    Thanks

  24. #24
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    ok

    This code goes into your form where you want to save to database:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim desiredvalues() As Object = {TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text}
    3. ' Alternativley you can loop through your controls and add their values to this array
    4. '
    5. '
    6.             SaveDB(desiredvalues, "MyTab")
    7. '
    8.  
    9. End Sub

    and in your class:
    VB Code:
    1. '
    2. '
    3. Public Shared Function SaveDB(ByVal desiredvalues As Object, ByVal TableStr As String)
    4.  
    5. '
    6. '
    7. '
    8. Dim MyDataRow As DataRow = mydataset.Tables(TableStr).NewRow
    9.  
    10. MyDataRow.ItemArray = desiredvalues 'here you add them all
    11.  
    12. mydataset.Tables(TableStr).Rows.Add(MyDataRow)
    13. MyAdapter.Update(mydataset, TableStr)
    14. MyConnection.Close()
    15. '
    16. '
    '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

  25. #25

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    aah thanx , I will give it a shot !

  26. #26

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Thumbs up

    Perfect !

    My friend Lunatic3 I owe , Thank you so much . You just did it . I was afraid to use arrays and fail . Thank you all over !

  27. #27

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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

  28. #28
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    First:

    You are most welcome

    Second:

    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

  29. #29

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Now , this makes sense at least ! but my question now How would I include these features ? I need ideas or a link would be great though !

  30. #30
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I guess you dont need any extra coding. All you have to do is adding what you need to your array.
    '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

  31. #31

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    lol , That's enough for today !
    enjoy your coding !

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