Results 1 to 11 of 11

Thread: how to save to a diff db structure

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    how to save to a diff db structure

    i have 3 combo boxes in a form. One is binded to a dataset and the other 2 are constants defined in the item collection. The name of the fields are group(databound combobox), dayofweek(Monday, tue, etc..) and time (12:00am, 1:30am...and so on).

    My question is how to save the information in a database with this structure: grp, mon, tue, wed, thu, fri. The value from the databound combobox will be save to grp. Then the value from time combobox will be save to mon, tue, wed, thu fri depending on the value from the dayofweek combobox.

    For example:

    grp: Accessories
    dayofweek: Monday
    Time: 12:00n

    The db will be:
    grp:Accessories
    mon:12:00nn
    tue:<null>
    wed:<null>
    thu:<null>
    fri:<null>

    Im just starting to learn the .net and Im having a hard time figuring out where and how to start.

    Hope somebody out there could give me an idea.

    Thanks,
    Marivic

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    When you go to save it to the DB just use the one value as the name of the field to put the other value in. Although it would be easier if the filedname matched the Weekday name exactly (i.e. Monday instead of Mon)

    VB Code:
    1. dr.Item(DayOfWeek.GetName(GetType(DayOfWeek), cboDayOfWeek.SelectedValue)) = cboTime.SelectedValue

    Also if your combos aren't set up to use the Item Value the just change SelectedValue in the code to whatever way you are storing the values of the items.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    hi edneeis,

    thanks.

    this is my code for saving records. i cant figure out where and how can i put your code on.



    strSQL = "INSERT into calendar1 ( grpcode " & "," &
    cbdayofweek.SelectedItem.text & ")" & _
    "values (" & cbgroup.ValueMember & "," & _
    txttime.Text & ")"


    sample data:
    grp:Acessories (grpcode:01)
    dayofweek:Monday
    time:10:00am

    insert stmt shld be:
    insert into calendar1(grpcode, monday) values ("01", "10:00am")

    grpcode is the value member of cbgroup. how can i construct my insert statement correctly?

    Thanks again,
    Marivic

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    sorry, it should be

    cbgroup.SelectedValue

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Is the first filed actually called grpcode? And are the values suppose to be strings?

    It looks almost right. If the actual field name is grpcode then it should be this:

    strSQL = "INSERT into calendar1 ( "grpcode ," &
    cbdayofweek.SelectedItem.text & ")" & _
    "values ("' & cbgroup.ValueMember & '","' & _
    txttime.Text & "')"

    Also it might be hard to see but you have to enclose string values in the values part in single quotes.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    hi,

    sorry, actually i've revised my form. currently, it has 4 combo boxes now, 1 combox is databound.

    grpcode is the value member of the grp description which i wanted to save in my db.

    cbfromtime and cbtotime are populated from the items collection. (properties). they are of type varchar(30). values are from 8:00 am to 6:00 pm with a 30 minutes interval.

    this is now my revised code:

    strTime = cbfromtime.SelectedItem.ToString() & " - " & cbtotime.SelectedItem.ToString()

    strSQL = "INSERT into calendar2 ( grpcode" & "," & cbdayofweek.SelectedItem.ToString() & ")" & _
    "values (" & cbgroup.SelectedValue & "," & CStr(strTime) & ")"

    so when this sql is run, the value that shld be saved should be
    grp: 01
    monday: 8:30 am - 11:00 am

    however, ive got an sql error: SQL syntax error near ":"

    i understand that since monday field is of type varchar, this shouldn't be a problem. ive tried running this in the sql query analyzer and it goes fine. what's wrong here?

    thanks again.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Maybe its the datatype of cbgroup.SelectedValue? Try cbgroup.SelectedValue.ToString or CStr(cbgroup.SelectedValue)

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    ive tried it but the error is still there. i think the error is in the cbfromtime or cbtotime, they have ":" on the value. Ex. 10:30 am...But since its varchar, it shld be accepted. Does colon ( in the value a problem in SQL Server?

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    As long as its in quotes then it shouldn't be trouble. I wonder what the problem is. Is the length too long for the field?

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    Hi Edneeis,

    ive already got the answer. you're correct, since the field is char, then it shld be enclosed in single quotes. I just placed the quotes in the wrong place.

    Thanks very much.

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Good I'm glad that is all it was. Glad to help anytime.

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