Click here to Skip to main content
15,906,569 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got 3 tables:

tb_plane          tb_attributes        tb_as_plane_attributes
plane_id          attribute_id         plane_attribute_id
plane_name        attribute_name       FK_plane_id
                                       FK_attribute_id
                                       value


What I want to is to relate attributes to plane and set a value for each attribute. I did make a combo which i populate with all plane_name's:

C#
private void PopulatePlainComboBox()
        {
            try
            {
            string ConnectionString = conSettings.ConnectionString;

                DataSet dataSet = new DataSet();
                using (SqlConnection connection = new SqlConnection(ConnectionString))
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
                        {
                            command.CommandText = ("SELECT * FROM tb_plane");
                            dataAdapter.Fill(dataSet, "tb_plane");

                            command.CommandText = ("SELECT plane_id, plane_name FROM tb_plane");
                            dataAdapter.Fill(dataSet, "tb_plane");

                            CboPlains.DataSource = dataSet.Tables["tb_plane"];
                            CboPlains.DisplayMember = "plane_name";
                            CboPlains.ValueMember = "plane_id";
                        }
                    }
                    connection.Close();
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string str;
                str = "Source:" + ex.Source;
                str += "\n" + "Message:" + ex.Message;
                MessageBox.Show(str, "Database Exception");
            }
            catch (System.Exception ex)
            {
                string str;
                str = "Source:" + ex.Source;
                str += "\n" + "Message:" + ex.Message;
                MessageBox.Show(str, "Generic Exception");
            }
            finally
            {
            }
        }


Now I want to populate a datagrid with all attributes by attribute_name's from tb_attributes. One coulmn for all attribute_name's and second for Column - Value. I want to set value to this attributes I want to relate with plane's and by clicking button to make relations and save to table tb_as_plane_attributes. rest attributes where I don't put values are not save in assocation table. At last button click event to accept.

I try to make this work but I have problems. I try to implement datagrid for that:
C#
private void PopulateGridByAttributes()
        {
            try
            {
            string ConnectionString = conSettings.ConnectionString;

                DataSet dataSet = new DataSet();
                using (SqlConnection connection = new SqlConnection(ConnectionString))
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
                        {
                            command.CommandText = ("SELECT attribute_id, attribute_name FROM tb_attributes");
                            dataAdapter.Fill(dataSet, "tb_attributes");

                            GrdAllAttributes.Columns.Add("attribute_name", "name of attribute");
                            GrdAllAttributes.Columns.Add("dd", "Value");
                            int row = dataSet.Tables["tb_attributes"].Rows.Count - 1;
                            for (int r = 0; r <= row; r++)
                            {
                                GrdAllAttributes.Rows.Add();
                                GrdAllAttributes.Rows[r].Cells[0].Value = dataSet.Tables["tb_Atributtes"].Rows[r].ItemArray[1];   //attribute_name
                            }
                        }
                    }
                    connection.Close();
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string str;
                str = "Source:" + ex.Source;
                str += "\n" + "Message:" + ex.Message;
                MessageBox.Show(str, "Database Exception");
            }
            catch (System.Exception ex)
            {
                string str;
                str = "Source:" + ex.Source;
                str += "\n" + "Message:" + ex.Message;
                MessageBox.Show(str, "Generic Exception");
            }
            finally
            {
            }
        }
Posted
Updated 21-Sep-11 20:28pm
v2

I would say that you need to look at inner joins. When trying to get the data from multiple tables you need to use inner joins to ensure the correct data is retrieved. Take a look at this link:

Inner Joins[^]

Hope this helps
 
Share this answer
 
Comments
carlito_brigante 22-Sep-11 6:45am    
I think to this operation tom insert i don't need the inner joni statment cause i populate combobox by plains and i populate datagrid by attribute values. I just need to make 1 + column to set value to attributes and make insert command (on button click) to tb_as_plane_attributes. The inner join will be for update when i will have to retrive this attributes which is relate with plane...
For these who want to help here's my project:
Project

And here's a database script for sql server name "plane":
database

mayby some could help..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900