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. Database & SysAdmin
  3. Database
  4. Problem : Retrive Data with ref table

Problem : Retrive Data with ref table

Scheduled Pinned Locked Moved Database
securityhelp
18 Posts 3 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.
  • A Ahmed R El Bohoty

    Yes, u can bind 2 dimensional array to DataGrid and i found article in codeproject that talk about it. from opinion when i make 2 arraylist and store all in one arraylist to make centralize store so all data will be in one arraylist but i can not reach to the methodology to do this because its very important to do this code. http://www.codeproject.com/KB/database/BindArrayGrid.aspx?fid=31160&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26

    W Offline
    W Offline
    Wendelius
    wrote on last edited by
    #8

    The situation in the article is a little bit different. When you setup an array of entities, all entities have properties, so now you have used two dimensions (for example if student is a row and name property is a column). What kind of layout are you trying to achieve in DataGrid? Something like the following?

    PersonID Name Age
    1 John 32
    2 Kendra 20
    3 Steve 29
    ...

    The need to optimize rises from a bad design. My articles[^]

    A 1 Reply Last reply
    0
    • W Wendelius

      The situation in the article is a little bit different. When you setup an array of entities, all entities have properties, so now you have used two dimensions (for example if student is a row and name property is a column). What kind of layout are you trying to achieve in DataGrid? Something like the following?

      PersonID Name Age
      1 John 32
      2 Kendra 20
      3 Steve 29
      ...

      The need to optimize rises from a bad design. My articles[^]

      A Offline
      A Offline
      Ahmed R El Bohoty
      wrote on last edited by
      #9

      Yes

      W 1 Reply Last reply
      0
      • A Ahmed R El Bohoty

        Yes

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #10

        Okay, then you have modify your method so that you return an arraylist that contains only instances of one class. Now you return an arraylist that contains nested arraylists and I don't believe it will work. If your layout should be like that, my original reply is misleading. You should combine CatgBL and Student classes to one class only. After that you can create instances of that class to an arraylist and then return the list and display it.

        The need to optimize rises from a bad design. My articles[^]

        A 1 Reply Last reply
        0
        • W Wendelius

          Okay, then you have modify your method so that you return an arraylist that contains only instances of one class. Now you return an arraylist that contains nested arraylists and I don't believe it will work. If your layout should be like that, my original reply is misleading. You should combine CatgBL and Student classes to one class only. After that you can create instances of that class to an arraylist and then return the list and display it.

          The need to optimize rises from a bad design. My articles[^]

          A Offline
          A Offline
          Ahmed R El Bohoty
          wrote on last edited by
          #11

          Thanks for u to be interested to help me to correct code to work well but i say when i post the question " if i make all table in one class it work" and what motivate me to find solution and make each classes are separated is that when i make all attributes in one class this make problem if i want to display the data of any table only without display other so if i do this when i add object, the other attributes which i skip from class give default values to skipped values by zero so this force me to make one class for the state of joining when i want to display record that have a relation with other table and make one when i want to display the data of one table. :) ,thanks again for u and may Allah bless u for u effort.

          W 1 Reply Last reply
          0
          • A Ahmed R El Bohoty

            Thanks for u to be interested to help me to correct code to work well but i say when i post the question " if i make all table in one class it work" and what motivate me to find solution and make each classes are separated is that when i make all attributes in one class this make problem if i want to display the data of any table only without display other so if i do this when i add object, the other attributes which i skip from class give default values to skipped values by zero so this force me to make one class for the state of joining when i want to display record that have a relation with other table and make one when i want to display the data of one table. :) ,thanks again for u and may Allah bless u for u effort.

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #12

            Basically there is nothing wrong if you keep the data in two separate classes like in the first post. The problem is mainly in DataGrid. Perhaps you could use more powerful DataGridView instead. With that control you could create a combo box column for category so you wouldn't have problems with empty foreign key properties. This way you could have a combobox for user to select what category he wants to use for a person and if category is selected, it will be shown.

            The need to optimize rises from a bad design. My articles[^]

            A 1 Reply Last reply
            0
            • W Wendelius

              Basically there is nothing wrong if you keep the data in two separate classes like in the first post. The problem is mainly in DataGrid. Perhaps you could use more powerful DataGridView instead. With that control you could create a combo box column for category so you wouldn't have problems with empty foreign key properties. This way you could have a combobox for user to select what category he wants to use for a person and if category is selected, it will be shown.

              The need to optimize rises from a bad design. My articles[^]

              A Offline
              A Offline
              Ahmed R El Bohoty
              wrote on last edited by
              #13

              I Make some changes in code and data displayed but not displayed in normal form but each data in column displayed in row and no header text to columns.and the question also if i make another method to update data , my work is to do changes in destination array and compare the id that hold in array with the record in db and apply changes or wat?

              public ArrayList RetriveStdentInfo()
              {
              SqlCommand cmd = new SqlCommand();
              cmd.Connection = myCon;
              cmd.CommandText = "dbo.StoredProcedure2";
              //cmd.Parameters=ParameterDirection.ReturnValue;
              cmd.CommandType = CommandType.StoredProcedure;
              myCon.Open();
              IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
              ArrayList seqCat = new ArrayList();
              ArrayList seqStudent = new ArrayList();
              ArrayList destnation = new ArrayList();
              int prevCatId = 0;
              while (reader.Read())
              {

                      MyStudent std = new MyStudent();
                      std.Stid = reader.GetInt32(0);
                      std.Sname = reader.GetString(reader.GetOrdinal("stname"));
                      std.Catgid = reader.GetInt32(reader.GetOrdinal("Catgid"));
                      if (prevCatId != reader.GetInt32(reader.GetOrdinal("Catgid")))
                      {
                          CatgBL cat = new CatgBL();
                          cat.CatgName = reader.GetString(reader.GetOrdinal("CatgName"));
                          seqCat.Add(cat);
                          prevCatId = reader.GetInt32(reader.GetOrdinal("Catgid"));
                      }
                      seqStudent.Add(std);
                     
                  }
                
               `   foreach ( MyStudent var in seqStudent)         {             destnation.Add((int)var.Catgid);             destnation.Add((string)var.Sname);             destnation.Add((int)var.Stid);         }         foreach (CatgBL var in seqCat)         {             destnation.Add((int)var.Cid);             destnation.Add((string)var.CatgName);         }          return destnation;`
              }
              
              W 1 Reply Last reply
              0
              • A Ahmed R El Bohoty

                hi , i make simple method that retrieve data from tables this is easy but my problem happen when i retrieve data from table that have relationship with other table, i make class to set value into properties when i read and bind it in Gridview. i make for each table class that contains to properties and if i make all table in one class it work so i want to find solution to it with do this. Tables

                Create Table Catgerories (
                CatgId int PRIMARY KEY,
                Catgname nvarchar(50)
                )

                Create Table StudentInfo(
                stId int PRIMARY KEY,
                Stname nvarchar(50),
                CatgId int REFERENCES Catgerories (CatgId)
                )

                create PROCEDURE dbo.StoredProcedure2
                AS
                SELECT StudentInfo.*, Catgerories.Catgname
                FROM Catgerories INNER JOIN
                StudentInfo ON Catgerories.CatgId = StudentInfo.CatgId
                RETURN

                Code:

                public class CatgBL
                {
                int cid = 0;

                public int Cid
                {
                    get { return cid; }
                    set { cid = value; }
                }
                string catgName;
                
                public string CatgName
                {
                    get { return catgName; }
                    set { catgName = value; }
                }
                

                }

                public class MyStudent
                {
                int stid;

                public int Stid
                {
                    get { return stid; }
                    set { stid = value; }
                }
                string sname;
                
                public string Sname
                {
                    get { return sname; }
                    set { sname = value; }
                }
                int catgid;
                
                public int Catgid
                {
                    get { return catgid; }
                    set { catgid = value; }
                }
                

                }

                SqlConnection myCon = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\TestDb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
                public ArrayList RetriveStdentInfo()
                {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = myCon;
                cmd.CommandText = "dbo.StoredProcedure2";
                cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Parameters=ParameterDirection.ReturnValue;
                myCon.Open();
                IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                    ArrayList sequence = new ArrayList();
                    ArrayList seqCat = new ArrayList();
                    ArrayList seqStudent = new ArrayList();
                    while (reader.Read())
                    {
                        MyStudent std = new MyStudent();
                        CatgBL cat = new CatgBL();
                        std.Stid = reader.GetInt32(0);
                        std.Sname = reader.GetString(read
                
                R Offline
                R Offline
                Rami Said Abd Alhalim
                wrote on last edited by
                #14

                you can use constraint on C# code to joid two tables

                A 1 Reply Last reply
                0
                • R Rami Said Abd Alhalim

                  you can use constraint on C# code to joid two tables

                  A Offline
                  A Offline
                  Ahmed R El Bohoty
                  wrote on last edited by
                  #15

                  but why ? and what is the benefit if i do ?

                  R 1 Reply Last reply
                  0
                  • A Ahmed R El Bohoty

                    but why ? and what is the benefit if i do ?

                    R Offline
                    R Offline
                    Rami Said Abd Alhalim
                    wrote on last edited by
                    #16

                    can you explain your problems

                    A 1 Reply Last reply
                    0
                    • R Rami Said Abd Alhalim

                      can you explain your problems

                      A Offline
                      A Offline
                      Ahmed R El Bohoty
                      wrote on last edited by
                      #17

                      go to the Subject , i explain the problem and code also.

                      1 Reply Last reply
                      0
                      • A Ahmed R El Bohoty

                        I Make some changes in code and data displayed but not displayed in normal form but each data in column displayed in row and no header text to columns.and the question also if i make another method to update data , my work is to do changes in destination array and compare the id that hold in array with the record in db and apply changes or wat?

                        public ArrayList RetriveStdentInfo()
                        {
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = myCon;
                        cmd.CommandText = "dbo.StoredProcedure2";
                        //cmd.Parameters=ParameterDirection.ReturnValue;
                        cmd.CommandType = CommandType.StoredProcedure;
                        myCon.Open();
                        IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                        ArrayList seqCat = new ArrayList();
                        ArrayList seqStudent = new ArrayList();
                        ArrayList destnation = new ArrayList();
                        int prevCatId = 0;
                        while (reader.Read())
                        {

                                MyStudent std = new MyStudent();
                                std.Stid = reader.GetInt32(0);
                                std.Sname = reader.GetString(reader.GetOrdinal("stname"));
                                std.Catgid = reader.GetInt32(reader.GetOrdinal("Catgid"));
                                if (prevCatId != reader.GetInt32(reader.GetOrdinal("Catgid")))
                                {
                                    CatgBL cat = new CatgBL();
                                    cat.CatgName = reader.GetString(reader.GetOrdinal("CatgName"));
                                    seqCat.Add(cat);
                                    prevCatId = reader.GetInt32(reader.GetOrdinal("Catgid"));
                                }
                                seqStudent.Add(std);
                               
                            }
                          
                         `   foreach ( MyStudent var in seqStudent)         {             destnation.Add((int)var.Catgid);             destnation.Add((string)var.Sname);             destnation.Add((int)var.Stid);         }         foreach (CatgBL var in seqCat)         {             destnation.Add((int)var.Cid);             destnation.Add((string)var.CatgName);         }          return destnation;`
                        }
                        
                        W Offline
                        W Offline
                        Wendelius
                        wrote on last edited by
                        #18

                        Now you're adding both students and categories to same array. That't not quite what I meant. Have a look at this excellent article. Especially take a look at the part which has a heading: DataGridViewComboBoxColumn. It gives the idea how combo can fill from another array than where the rows are coming from. http://www.codeproject.com/KB/books/PresentDataDataGridView.aspx[^] Another article I noticed was: http://www.codeproject.com/KB/miscctrl/GenericDataGridView.aspx[^]

                        The need to optimize rises from a bad design. My articles[^]

                        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