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:
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:
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];
}
}
}
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
{
}
}