EDN Admin
Well-known member
I got 4 tables:
<span style="font-size:9pt
<div style="color:black; background-color:white
<pre>plain_table(plain_id(PK), plain_name, FK_temperature_id(FK), FK_material_id(FK), FK_drive_id(FK))
temperature(temperature_id(PK), temperature_name)
material(material_id(PK), material_name)
drive(drive_id(PK), drive_name)
[/code]
<br/>
Now i have an function which download all names from plain_table:
<div style="color:black; background-color:white
<pre>listBox1.Items.Clear();
conn.Open();
<span style="color:blue string query = (<span style="color:#a31515 "SELECT plain_name FROM plain_table");
SqlCommand command = <span style="color:blue new SqlCommand(query, conn);
command.ExecuteNonQuery();
SqlDataReader dr = command.ExecuteReader(Commandbehavior
.CloseConnection
)
;
<span style="color:blue while (dr.Read())
{
<span style="color:blue string plain_name = Convert.ToString(dr[<span style="color:#a31515 "plain_name"]);
<span style="color:blue this.listBox1.Items.Add(plain_name);
}
conn.Close();
[/code]
<br/>
I got event on listbox:
<div style="color:black; background-color:white
<pre><span style="color:blue private <span style="color:blue void listBox1_MouseUp(<span style="color:blue object sender, MouseEventArgs e)
{
PobranieWartosciDoKontrolek();
}
<span style="color:blue private <span style="color:blue void PobranieWartosciDoKontrolek()
{
plain_name = listBox1.Items[listBox1.SelectedIndex].ToString();
conn.Open();
<span style="color:blue string zapytanie = (<span style="color:#a31515 "SELECT plain_id, plain_name, FK_temperature_id, FK_material_id, FK_drive_id, temperature.temperature_id, material.material_id, drive.drive_id, temperature.temperature_name, material.material_name, drive.drive_name FROM plain_table LEFT JOIN temperature ON temperature.temperature_id = plain_table.FK_temperature_id LEFT JOIN material ON material.material_id = plain_table.FK_material_id LEFT JOIN drive ON drive.drive_id = plain_table.FK_drive_id WHERE plain_name=" + plain_name + <span style="color:#a31515 ";");
SqlCommand command = <span style="color:blue new SqlCommand(zapytanie, conn);
SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
dr.Read();
textBox1.Text = Convert.ToString(dr[<span style="color:#a31515 "plain_name"]);
conn.Close(); <span style="color:green // <====== HERES...
SqlDataAdapter da = <span style="color:blue new SqlDataAdapter(zapytanie, conn);
DataTable table = <span style="color:blue new DataTable();
da.Fill(table);
comboBox1.DataSource = table;
comboBox1.DisplayMember = <span style="color:#a31515 "temperature_name";
comboBox1.ValueMember = <span style="color:#a31515 "temperature_id";
comboBox2.DataSource = table;
comboBox2.DisplayMember = <span style="color:#a31515 "material_name";
comboBox2.ValueMember = <span style="color:#a31515 "material_id";
comboBox3.DataSource = table;
comboBox3.DisplayMember = <span style="color:#a31515 "drive_name";
comboBox3.ValueMember = <span style="color:#a31515 "drive_id";
conn.Close();
[/code]
<br/>
So whats my problem?:
<br/>
I got an error in my code theres a comment, if i erase this connn.Open() i got an error:
<div style="color:black; background-color:white
<pre>There is already an open DataReader associated with <span style="color:blue this Command which must be closed first.
[/code]
<br/>
I think it is because i got data reader not close, but where should i do it?
tahts first problem...
The second is bigger i think.
I want to download to this comboboxes all names from relational tables and set first value on what is selected from listbox record relation..
I hope u know what i mean...
-- Now i got only 3 actual values in comboxoes represent a relation with plain_table, i want in comboboxes all names but set on actual relation..
I think i have to make a 4 separate queries populate comboboxes then set to control but i don;t know how.
Later i want make update from this control of course..
Please help me on my example. How this code should view?
best regards
<br/>
<br/>
View the full article
<span style="font-size:9pt
<div style="color:black; background-color:white
<pre>plain_table(plain_id(PK), plain_name, FK_temperature_id(FK), FK_material_id(FK), FK_drive_id(FK))
temperature(temperature_id(PK), temperature_name)
material(material_id(PK), material_name)
drive(drive_id(PK), drive_name)
[/code]
<br/>
Now i have an function which download all names from plain_table:
<div style="color:black; background-color:white
<pre>listBox1.Items.Clear();
conn.Open();
<span style="color:blue string query = (<span style="color:#a31515 "SELECT plain_name FROM plain_table");
SqlCommand command = <span style="color:blue new SqlCommand(query, conn);
command.ExecuteNonQuery();
SqlDataReader dr = command.ExecuteReader(Commandbehavior
.CloseConnection
)
;
<span style="color:blue while (dr.Read())
{
<span style="color:blue string plain_name = Convert.ToString(dr[<span style="color:#a31515 "plain_name"]);
<span style="color:blue this.listBox1.Items.Add(plain_name);
}
conn.Close();
[/code]
<br/>
I got event on listbox:
<div style="color:black; background-color:white
<pre><span style="color:blue private <span style="color:blue void listBox1_MouseUp(<span style="color:blue object sender, MouseEventArgs e)
{
PobranieWartosciDoKontrolek();
}
<span style="color:blue private <span style="color:blue void PobranieWartosciDoKontrolek()
{
plain_name = listBox1.Items[listBox1.SelectedIndex].ToString();
conn.Open();
<span style="color:blue string zapytanie = (<span style="color:#a31515 "SELECT plain_id, plain_name, FK_temperature_id, FK_material_id, FK_drive_id, temperature.temperature_id, material.material_id, drive.drive_id, temperature.temperature_name, material.material_name, drive.drive_name FROM plain_table LEFT JOIN temperature ON temperature.temperature_id = plain_table.FK_temperature_id LEFT JOIN material ON material.material_id = plain_table.FK_material_id LEFT JOIN drive ON drive.drive_id = plain_table.FK_drive_id WHERE plain_name=" + plain_name + <span style="color:#a31515 ";");
SqlCommand command = <span style="color:blue new SqlCommand(zapytanie, conn);
SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
dr.Read();
textBox1.Text = Convert.ToString(dr[<span style="color:#a31515 "plain_name"]);
conn.Close(); <span style="color:green // <====== HERES...
SqlDataAdapter da = <span style="color:blue new SqlDataAdapter(zapytanie, conn);
DataTable table = <span style="color:blue new DataTable();
da.Fill(table);
comboBox1.DataSource = table;
comboBox1.DisplayMember = <span style="color:#a31515 "temperature_name";
comboBox1.ValueMember = <span style="color:#a31515 "temperature_id";
comboBox2.DataSource = table;
comboBox2.DisplayMember = <span style="color:#a31515 "material_name";
comboBox2.ValueMember = <span style="color:#a31515 "material_id";
comboBox3.DataSource = table;
comboBox3.DisplayMember = <span style="color:#a31515 "drive_name";
comboBox3.ValueMember = <span style="color:#a31515 "drive_id";
conn.Close();
[/code]
<br/>
So whats my problem?:
<br/>
I got an error in my code theres a comment, if i erase this connn.Open() i got an error:
<div style="color:black; background-color:white
<pre>There is already an open DataReader associated with <span style="color:blue this Command which must be closed first.
[/code]
<br/>
I think it is because i got data reader not close, but where should i do it?
tahts first problem...
The second is bigger i think.
I want to download to this comboboxes all names from relational tables and set first value on what is selected from listbox record relation..
I hope u know what i mean...
-- Now i got only 3 actual values in comboxoes represent a relation with plain_table, i want in comboboxes all names but set on actual relation..
I think i have to make a 4 separate queries populate comboboxes then set to control but i don;t know how.
Later i want make update from this control of course..
Please help me on my example. How this code should view?
best regards
<br/>
<br/>
View the full article