Q: Deleting a table with constraints

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • G .Net

    #1

    Q: Deleting a table with constraints

    Hi

    I have a DataSet with several DataTables. I have set up relations between
    these tables.

    I want to delete all the tables and re-fill them. However, when I try to do
    so, even after using Relations.Clear () I still get excecptions, such as:

    Cannot remove ForeignKeyConst raint - remove the constraint first

    when trying to remove a table.

    Can anybody help???



  • Cor Ligthert [MVP]

    #2
    Re: Deleting a table with constraints

    Lucky enough is it as that is.

    You can (normally I have seen a situation that does that) not have children
    without a parent.

    Therefore you have first to delete all the childs before you can delete the
    parent.

    If you want to do it than to the database (maybe that is your question),
    than you have to use this.
    http://msdn2.microsoft.com/en-us/library/0f8054fy.aspx

    First do the deleted children rows
    Than the parent rows
    Than the modified and added children.

    Don't forget to do an acceptchanges on the original dataset.

    You see than direct why is that acceptchanges.

    I hope this helps,

    Cor

    "G .Net" <nodamnspam@ema il.comschreef in bericht
    news:sP6dnQLGfJ h8ejbZRVny2g@pi pex.net...
    Hi
    >
    I have a DataSet with several DataTables. I have set up relations between
    these tables.
    >
    I want to delete all the tables and re-fill them. However, when I try to
    do so, even after using Relations.Clear () I still get excecptions, such
    as:
    >
    Cannot remove ForeignKeyConst raint - remove the constraint first
    >
    when trying to remove a table.
    >
    Can anybody help???
    >
    >
    >

    Comment

    • G .Net

      #3
      Re: Deleting a table with constraints

      Hi Cor

      Thanks for that. I'll have a look.

      However, I'm puzzled why clearning the relations doesn't solve it? Surely
      that should make the tables seperate (i.e. with no relationships) so I can
      simple do something like:

      dt.Remove()

      for any table. However, as I indicated earlier, this stil throws an
      exception.


      "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
      news:uB2GvKFoGH A.4728@TK2MSFTN GP03.phx.gbl...
      Lucky enough is it as that is.
      >
      You can (normally I have seen a situation that does that) not have
      children without a parent.
      >
      Therefore you have first to delete all the childs before you can delete
      the parent.
      >
      If you want to do it than to the database (maybe that is your question),
      than you have to use this.
      http://msdn2.microsoft.com/en-us/library/0f8054fy.aspx
      >
      First do the deleted children rows
      Than the parent rows
      Than the modified and added children.
      >
      Don't forget to do an acceptchanges on the original dataset.
      >
      You see than direct why is that acceptchanges.
      >
      I hope this helps,
      >
      Cor
      >
      "G .Net" <nodamnspam@ema il.comschreef in bericht
      news:sP6dnQLGfJ h8ejbZRVny2g@pi pex.net...
      >Hi
      >>
      >I have a DataSet with several DataTables. I have set up relations between
      >these tables.
      >>
      >I want to delete all the tables and re-fill them. However, when I try to
      >do so, even after using Relations.Clear () I still get excecptions, such
      >as:
      >>
      >Cannot remove ForeignKeyConst raint - remove the constraint first
      >>
      >when trying to remove a table.
      >>
      >Can anybody help???
      >>
      >>
      >>
      >
      >

      Comment

      • Jesse

        #4
        Re: Deleting a table with constraints

        Did you figure out what the problem was? I'm running into the same
        thing. I have a dataset with 2 tables in it. Clearing the relations
        doesn't seem to do anything. Instead I can first clear the child
        table, then clear the parent table. I can't remove the table from the
        dataset however. I get the error 'Cannot remove table ... because it
        is referenced in ForeignKeyConst raint ... Remove the constraint first.'

        How can I remove the constraint? Clearing the relations doesn't do it.

        Jesse.


        G .Net wrote:
        Hi Cor
        >
        Thanks for that. I'll have a look.
        >
        However, I'm puzzled why clearning the relations doesn't solve it? Surely
        that should make the tables seperate (i.e. with no relationships) so I can
        simple do something like:
        >
        dt.Remove()
        >
        for any table. However, as I indicated earlier, this stil throws an
        exception.
        >
        >
        "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
        news:uB2GvKFoGH A.4728@TK2MSFTN GP03.phx.gbl...
        Lucky enough is it as that is.

        You can (normally I have seen a situation that does that) not have
        children without a parent.

        Therefore you have first to delete all the childs before you can delete
        the parent.

        If you want to do it than to the database (maybe that is your question),
        than you have to use this.
        http://msdn2.microsoft.com/en-us/library/0f8054fy.aspx

        First do the deleted children rows
        Than the parent rows
        Than the modified and added children.

        Don't forget to do an acceptchanges on the original dataset.

        You see than direct why is that acceptchanges.

        I hope this helps,

        Cor

        "G .Net" <nodamnspam@ema il.comschreef in bericht
        news:sP6dnQLGfJ h8ejbZRVny2g@pi pex.net...
        Hi
        >
        I have a DataSet with several DataTables. I have set up relations between
        these tables.
        >
        I want to delete all the tables and re-fill them. However, when I try to
        do so, even after using Relations.Clear () I still get excecptions, such
        as:
        >
        Cannot remove ForeignKeyConst raint - remove the constraint first
        >
        when trying to remove a table.
        >
        Can anybody help???
        >
        >
        >

        Comment

        • Jesse

          #5
          Re: Deleting a table with constraints

          Nevermind, I figured it out. I had to remove the primary key
          constraint AND the foreign key constraint, THEN clear the relations,
          THEN I could remove the table.

          ds.Tables("dt1" ).Constraints.R emoveAt(0)
          ds.Tables("dt2" ).Constraints.R emoveAt(0)
          ds.Relations.Cl ear()
          dsOTTR.Tables.C lear()



          Jesse wrote:
          Did you figure out what the problem was? I'm running into the same
          thing. I have a dataset with 2 tables in it. Clearing the relations
          doesn't seem to do anything. Instead I can first clear the child
          table, then clear the parent table. I can't remove the table from the
          dataset however. I get the error 'Cannot remove table ... because it
          is referenced in ForeignKeyConst raint ... Remove the constraint first.'
          >
          How can I remove the constraint? Clearing the relations doesn't do it.
          >
          Jesse.
          >
          >
          G .Net wrote:
          Hi Cor

          Thanks for that. I'll have a look.

          However, I'm puzzled why clearning the relations doesn't solve it? Surely
          that should make the tables seperate (i.e. with no relationships) so I can
          simple do something like:

          dt.Remove()

          for any table. However, as I indicated earlier, this stil throws an
          exception.


          "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
          news:uB2GvKFoGH A.4728@TK2MSFTN GP03.phx.gbl...
          Lucky enough is it as that is.
          >
          You can (normally I have seen a situation that does that) not have
          children without a parent.
          >
          Therefore you have first to delete all the childs before you can delete
          the parent.
          >
          If you want to do it than to the database (maybe that is your question),
          than you have to use this.
          http://msdn2.microsoft.com/en-us/library/0f8054fy.aspx
          >
          First do the deleted children rows
          Than the parent rows
          Than the modified and added children.
          >
          Don't forget to do an acceptchanges on the original dataset.
          >
          You see than direct why is that acceptchanges.
          >
          I hope this helps,
          >
          Cor
          >
          "G .Net" <nodamnspam@ema il.comschreef in bericht
          news:sP6dnQLGfJ h8ejbZRVny2g@pi pex.net...
          >Hi
          >>
          >I have a DataSet with several DataTables. I have set up relations between
          >these tables.
          >>
          >I want to delete all the tables and re-fill them. However, when I try to
          >do so, even after using Relations.Clear () I still get excecptions, such
          >as:
          >>
          >Cannot remove ForeignKeyConst raint - remove the constraint first
          >>
          >when trying to remove a table.
          >>
          >Can anybody help???
          >>
          >>
          >>
          >
          >

          Comment

          Working...