Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. problem using dataRelation in dot net

problem using dataRelation in dot net

Scheduled Pinned Locked Moved C#
helpdata-structuresdebugging
25 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Richard Blythe

    System.NullReferenceException is an exception that never lies. You are obviously typing a string in wrong. To eliminate all possible confusion, try this code:

    if (!ds.Tables.Contains("category") || !ds.Tables["category"].Columns.Contains("ID"))
    throw new ArgumentException("The category table or column does not exists!")
    //
    if (!ds.Tables.Contains("item") || !ds.Tables["item"].Columns.Contains("catID"))
    throw new ArgumentException("The item table or column does not exists!")

    DataColumn c1 = ds.Tables["category"].Columns["ID"];Line
    DataColumn c2 = ds.Tables["item"].Columns["catID"];
    dRel = new DataRelation("categoryItem", c1, c2);

    The mind is like a parachute. It doesn’t work unless it’s open.

    D Offline
    D Offline
    Dhyanga
    wrote on last edited by
    #12

    Thanks for the reply. yes you were right. it threw the argument exception. the query i used for the dataset ds is:

    select i.ID,j.Item,j.catID from category i, item j where i.ID = j.catID

    and the dataset is having data . Then why it gave that error ?

    suchita

    R 1 Reply Last reply
    0
    • L Luc Pattyn

      I can't tell what is wrong by seeing only some part of the relevant code. if you think everything is correct, how come the exception? now replace

      objDA.Fill(ds);
      ...

      by

      try {
      if (objDA==null) MessageBox.Show("Oh dear, objDA seems to be null");
      objDA.Fill(ds);
      ...
      } catch(Exception exc) {
      MessageBox.Show("exception: "+exc.ToString());
      }

      :|

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      R Offline
      R Offline
      Richard Blythe
      wrote on last edited by
      #13

      Don't take it personal SayamiSuchi. Luc is not partial to any codeproject member. He gripes at all of us! :-D

      The mind is like a parachute. It doesn’t work unless it’s open.

      L 1 Reply Last reply
      0
      • L Luc Pattyn

        I can't tell what is wrong by seeing only some part of the relevant code. if you think everything is correct, how come the exception? now replace

        objDA.Fill(ds);
        ...

        by

        try {
        if (objDA==null) MessageBox.Show("Oh dear, objDA seems to be null");
        objDA.Fill(ds);
        ...
        } catch(Exception exc) {
        MessageBox.Show("exception: "+exc.ToString());
        }

        :|

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

        D Offline
        D Offline
        Dhyanga
        wrote on last edited by
        #14

        Thank you for the reply. my code

        DataColumn c1 = ds.Tables["category"].Columns["ID"];

        is throwing that error. but since my dataset has values, i dont know how come that error ?

        suchita

        R L 2 Replies Last reply
        0
        • D Dhyanga

          Thanks for the reply. yes you were right. it threw the argument exception. the query i used for the dataset ds is:

          select i.ID,j.Item,j.catID from category i, item j where i.ID = j.catID

          and the dataset is having data . Then why it gave that error ?

          suchita

          R Offline
          R Offline
          Richard Blythe
          wrote on last edited by
          #15

          Which one threw the exception? I agree with Eddy that you need to learn how to perform effecient debugging. Visual Studio wouldn't be worth two cents to me if it didn't have a good debugger.

          The mind is like a parachute. It doesn’t work unless it’s open.

          D 2 Replies Last reply
          0
          • R Richard Blythe

            Which one threw the exception? I agree with Eddy that you need to learn how to perform effecient debugging. Visual Studio wouldn't be worth two cents to me if it didn't have a good debugger.

            The mind is like a parachute. It doesn’t work unless it’s open.

            D Offline
            D Offline
            Dhyanga
            wrote on last edited by
            #16

            Thank you. Both of the argument exception is executing.

            suchita

            1 Reply Last reply
            0
            • L Lost User

              You're having trouble debugging? Start here[^] in order to learn how to inspect those values.

              I are Troll :suss:

              D Offline
              D Offline
              Dhyanga
              wrote on last edited by
              #17

              thank you

              suchita

              1 Reply Last reply
              0
              • D Dhyanga

                Thank you for the reply. my code

                DataColumn c1 = ds.Tables["category"].Columns["ID"];

                is throwing that error. but since my dataset has values, i dont know how come that error ?

                suchita

                R Offline
                R Offline
                Richard Blythe
                wrote on last edited by
                #18

                When I use a DataAdapter to fill a DataTable, I have to manually set the DataTable's name. If the same logic applies, You may need to manually set the names of your DataSet tables.

                The mind is like a parachute. It doesn’t work unless it’s open.

                D 1 Reply Last reply
                0
                • D Dhyanga

                  Thank you for the reply. my code

                  DataColumn c1 = ds.Tables["category"].Columns["ID"];

                  is throwing that error. but since my dataset has values, i dont know how come that error ?

                  suchita

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #19

                  assuming ds is a DataSet, that line would throw a NullRefExc only if ds were null or there were no table called "category" suggestion: split the line into many, and check what gives

                  if (ds==null) MessageBox.Show("ds is null");
                  DataTable dtCat=ds.Tables["category"];
                  if (dtCat==null) MessageBox.Show("dtCat is null");
                  DataColumn c1=dtCat.Columns["ID"];
                  MessageBox.Show("we got a DataColumn!");

                  :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                  1 Reply Last reply
                  0
                  • R Richard Blythe

                    When I use a DataAdapter to fill a DataTable, I have to manually set the DataTable's name. If the same logic applies, You may need to manually set the names of your DataSet tables.

                    The mind is like a parachute. It doesn’t work unless it’s open.

                    D Offline
                    D Offline
                    Dhyanga
                    wrote on last edited by
                    #20

                    ok. But since i'm using join of two tables, how i'm gonna give two tables name in there ? Or is it just whatever name I'm giving for the dataset ?

                    suchita

                    R 1 Reply Last reply
                    0
                    • R Richard Blythe

                      Don't take it personal SayamiSuchi. Luc is not partial to any codeproject member. He gripes at all of us! :-D

                      The mind is like a parachute. It doesn’t work unless it’s open.

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #21

                      You know pretty well an open mind is the numero uno prerequisite when debugging some code... :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                      1 Reply Last reply
                      0
                      • D Dhyanga

                        ok. But since i'm using join of two tables, how i'm gonna give two tables name in there ? Or is it just whatever name I'm giving for the dataset ?

                        suchita

                        R Offline
                        R Offline
                        Richard Blythe
                        wrote on last edited by
                        #22

                        What is your SELECT query for your data adapter?

                        The mind is like a parachute. It doesn’t work unless it’s open.

                        D 1 Reply Last reply
                        0
                        • R Richard Blythe

                          What is your SELECT query for your data adapter?

                          The mind is like a parachute. It doesn’t work unless it’s open.

                          D Offline
                          D Offline
                          Dhyanga
                          wrote on last edited by
                          #23

                          select i.ID,j.Item,j.catID from category i, item j where i.ID = j.catID

                          suchita

                          1 Reply Last reply
                          0
                          • R Richard Blythe

                            Which one threw the exception? I agree with Eddy that you need to learn how to perform effecient debugging. Visual Studio wouldn't be worth two cents to me if it didn't have a good debugger.

                            The mind is like a parachute. It doesn’t work unless it’s open.

                            D Offline
                            D Offline
                            Dhyanga
                            wrote on last edited by
                            #24

                            ok now i changed them a little bit.

                                    string query = @"select i.ID,j.catID,j.item from category i, item j where i.ID = j.catID";
                                    SqlCommand objCmd = new SqlCommand(query, cnx);
                                    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
                                    DataSet ds = new DataSet();
                                    objDA.Fill(ds,"category");
                                    objDA.Fill(ds, "item");
                                    DataColumn c2 = ds.Tables\["category"\].Columns\["ID"\];
                                    DataColumn c1 = ds.Tables\["item"\].Columns\["catID"\];
                                    dRel = new DataRelation("category\_Item", c1, c2);
                                    ds.Relations.Add(dRel);
                            

                            And here ID is the unique key for category and catID is the foreign key . But it throws error like:

                            These columns don't currently have unique values.
                            Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

                            Exception Details: System.ArgumentException: These columns don't currently have unique values.

                            Source Error:

                            Line 1231:
                            Line 1232: dRel = new DataRelation("category_Item", c1, c2);
                            Line 1233: ds.Relations.Add(dRel);
                            Line 1234: Repeater1.DataSource = ds;
                            Line 1235: Repeater1.DataBind();

                            But in ID is the unique key in category table and i have unique ID in item table too. I tried replacing catID by ID for the item table but in both the cases, error is same.

                            suchita

                            1 Reply Last reply
                            0
                            • D Dhyanga

                              I have been trying to solve the problem related with using dataRelation but still couldn't figure it out. I hope somebody can help me with this. I have two repeaters used one nested another. part of code is shown below:

                              void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
                              {
                              RepeaterItem item = e.Item;
                              if ((item.ItemType == ListItemType.Item) ||
                              (item.ItemType == ListItemType.AlternatingItem))
                              {

                                          Repeater2 = (Repeater)e.Item.FindControl("Repeater2");
                                          DataRowView drv = (DataRowView)item.DataItem;
                                          Repeater2.DataSource = drv.CreateChildView("categoryItem");
                                          Repeater2.DataBind();
                                      }
                                  }
                              

                              This categoryItem is from this code:

                              objDA.Fill(ds);
                              DataColumn c1 = ds.Tables["category"].Columns["ID"];
                              DataColumn c2 = ds.Tables["Item"].Columns["catID"];
                              dRel = new DataRelation("categoryItem", c1, c2);
                              ds.Relations.Add(dRel);
                              Repeater1.DataSource = ds;
                              Repeater1.DataBind();

                              And the error message is:

                              Object reference not set to an instance of an object.
                              Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

                              Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

                              Source Error:

                              Line 1222: objDA.Fill(ds);
                              Line 1223:
                              Line 1224: DataColumn c1 = ds.Tables["category"].Columns["ID"];
                              Line 1225: DataColumn c2 = ds.Tables["item"].Columns["catID"];
                              Line 1226: dRel = new DataRelation("categoryItem", c1, c2);

                              Well the "ID" of table "category" is the foreign key in "item" table and that foreignKey name in the "item" table is "catID". Any help will be appreciated.. I really need some help in this.

                              suchita

                              D Offline
                              D Offline
                              Dhyanga
                              wrote on last edited by
                              #25

                              Thank you so much to all who has given interest in this topic. i got my program running now.

                              suchita

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups