Results 1 to 18 of 18

Thread: add row to datatable that contains a foreigkey

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    add row to datatable that contains a foreigkey

    Hello dear friends i want to add a new row to my datatable, this datatable has 2 columns, first one is int and second is the foreignkeycolumn.

    I try to do it by dataset1.datatable.addmytablerow(someInt, herecomestheforeignkeycolumn)
    whatever i try it fails. i want the foreingkey to be inserrted automaticly just like when i click into the datagridview and the foreingkey of the parent table is inserted automaticly. so how would i do that? to say it short. i want to add a new row to the child datatable with textboxes not manualy when the user clciks on the datagridview and insert data into the cells. greetings.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: add row to datatable that contains a foreigkey

    What you say you're doing should work, so you're obviously doing something wrong. Maybe if you were to tell us what actually happens, rather than just saying that it fails, we could work out what the specific cause

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: add row to datatable that contains a foreigkey

    Dear friend,
    the problem is, i dont know what exactly i should write in that section, where i said "herecomestheforeingkey"

    intelisense wants something with the parenttable row but whatever i try to insert in that part it is red underlined.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: add row to datatable that contains a foreigkey

    The problem is that you're withholding relevant information from us. Intellisense is telling you what is expected and rather than passing that information on to us, you're giving us a vague approximation. You haven't told us what you've actually tried or what actually happened when you tried it. We can't see through your eyes or know what's in your head so you have to provide us with ALL the relevant information.

    There are only two things that I can possibly think of that would be expected there. The first is the foreign key value itself, i.e. the primary key from the related row. The second is the related row itself. I'm assuming that you've tried those because they seem like obvious choices, but maybe they're not as obvious as I think.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: add row to datatable that contains a foreigkey

    I think the problem is that he litteraly has
    dataset1.datatable.addmytablerow(someInt, herecomestheforeignkeycolumn)
    in his code... VB is complaining (rightfully) that it doesn't know what herecomestheforeignkeycolumn is, so it thinks that you might want a function there, and is trying to enforce the () found when calling functions.

    So... DO you even HAVE the foreighkey value to insert? It's not a magical piece of code... you have to insert something, get back the PKey from that and use that as the FKey in the next insert. It's not magic.

    In JMC's signature block, click on the VBForums Database Development FAQ link ... In the first post of that thread is "How can I get the auto-generated AutoNumber/Identity value for the record just added?" ... that's probably what you need.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: add row to datatable that contains a foreigkey

    Sorry for the bad description of my problem.
    Name:  Unbenannt.jpg
Views: 135
Size:  6.4 KB

    When you call AddTablerow of a dataset table like Dataset1.artikel.AddartikelRow() and you open brackets then intelisense expects the values for this table, depending how my columsn you have in your certain table. In my case its is 2 columns, first is string and the second column is a foreingkey. so my problem is here, whatever i try to insert as 2. value into the addrow brackets i doesnt work. i tried parentkategorieRowBykategorie_artikel:=
    also it doesnt work an it is red underlinded.

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: add row to datatable that contains a foreigkey

    Quote Originally Posted by moxid View Post
    Sorry for the bad description of my problem.
    Name:  Unbenannt.jpg
Views: 135
Size:  6.4 KB

    When you call AddTablerow of a dataset table like Dataset1.artikel.AddartikelRow() and you open brackets then intelisense expects the values for this table, depending how my columsn you have in your certain table. In my case its is 2 columns, first is string and the second column is a foreingkey. so my problem is here, whatever i try to insert as 2. value into the addrow brackets i doesnt work. i tried parentkategorieRowBykategorie_artikel:=
    also it doesnt work an it is red underlinded.
    Right... yes.. which is what I thought...

    so ... as I said:
    Quote Originally Posted by techgnome View Post
    So... DO you even HAVE the foreighkey value to insert? It's not a magical piece of code... you have to insert something, get back the PKey from that and use that as the FKey in the next insert. It's not magic.
    Again, before you can insert the foreign key value you have to know what that value is ... it has to come from somewhere. Usually from the table you're referencing. What is it linking to?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: add row to datatable that contains a foreigkey

    Dear friends,

    the value comes form the parent datatable, to be more accurate , it is the id from the selected row of parent table.

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: add row to datatable that contains a foreigkey

    Ok, great. Even better... so get the value and pass it along.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: add row to datatable that contains a foreigkey

    if you tell me how than i will :> whatever i try to insert it says this value can not be converted to dataset1.ParentRow

  11. #11
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: add row to datatable that contains a foreigkey

    If you have access to the parent table then there's a way to get the value but we can't tell you how without seeing your code. You need to post all relevant code. Don't post a picture of it, it's to hard to read, cut and past the actual code.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: add row to datatable that contains a foreigkey

    There is no code...
    its only 1 dataset generated in the designer , 2 datatables and they are both connected to each with a relationship.

  13. #13
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: add row to datatable that contains a foreigkey

    There has to be code. Probably in the form load event where the TableAdapters load the datatables. How are you displaying the data? In a Parent/Child format where the parent is shown in Detail and the Child shown in a DataGridView? We need more info.

    My guess is that you have used the wrong Child table. If you look at your datasource, open the Parent Table, at the bottom after the fields list, you will see the Child table name, this is the one that needs to be dragged onto the form. The Child table is also listed with all the Tables, this is not the one you want.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: add row to datatable that contains a foreigkey

    yes dear friend i drapgged the right table it works perfect when i click into the child datagridview the foreignkey is inserted automaticly to the child dgv but like i said, i want to add row manualy not with clicking on datagridview . and here is the problem, i just dont know how. with dataset.tablename.tableaddrow(values,value) but he doesnt accept my 2. value where the foreignkey comes in. u can test it out :/

  15. #15
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: add row to datatable that contains a foreigkey

    You still haven't posted the relevant code. In fact you tell me there's no code. I'd like to know how your trying to add to row manually without code. In fact no data would be displayed on the form without code. So I can't help you.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: add row to datatable that contains a foreigkey

    all i want to know is, how i can add a row to a child table manualy (not by clicking into the dgv and write into the cells)... thats simple!
    Dataset1.datatable.datatabeladdrow(columns1,column2) column2 is the foreignkey so this is the problem, it doenst accept anything there.

  17. #17
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: add row to datatable that contains a foreigkey

    I'm not sure how many more times I can say it... I guess I could just copy & paste it....

    get the value and pass it along.
    it's not that hard. it isn't going to fill itself in for you. you have to GO. GET. THE. VALUE. and then pass it into the parameter where it is expecting it.

    it has to come from somewhere. Usually from the table you're referencing.

    still not sure why you're trying to add through code rather than letting the user add through the UI ... personally I'm not sure I care any more.

    The short of it is: you need that value in order to pass it to the method that's requiring it. So you need to figure out what the value should be in the first place.


    It's not brain surgery, it's not that complicated. Your .Add method is expecting two values... you need to pass them both.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    May 2014
    Posts
    184

    Re: add row to datatable that contains a foreigkey

    Sorry one last thing, is there a way to get the current id of the parent table automaticly or do i have to declare it myself? Thanks.

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