Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
i have a form with a listBox and a comboBox, the combobox contains the subjects, and the listBox must show only the articles of that subject. i'm using an sql server db and i dont find how to show in the listBox only the articles of the subject selected in the comboBox, help me please!

        table_subjects                               table_articles
idSubject       NameSubject            idArticle      NameArticle     idSubject
   1            Subject1         /          1       ArticleOfSubject1       1    
   2            Subject2         /          2       ArticleOfSubject2       2
   3            Subject3         /          3       Article2OfSubject2      2
                 ...                                        ...
Posted
Updated 5-May-13 14:32pm
v5

try this code:


using System.Data.SqlClient;


SqlConnection con = new SqlConnection();
DataSet ds = new DataSet();
DataTable dt = new DataTable();

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string subject = comboBox1.SelectedText;
int idsubject= comboBox1.SelectedIndex;
dt=subject_view(idsubject);
listBox1.Items.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
listBox1.Items.Add(dt.Rows[i]["NameArticle"].ToString());
}
}

public DataTable subject_view(int idsubject)
{
try
{
con.Open();
SqlCommand command = new SqlCommand("select * from subject_table where idSubject ="+idsubject, con);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
adapter.Fill(ds, "subject");
adapter.Dispose();
command.Dispose();
con.Close();
}
catch (Exception ex)
{
//MessageBox.Show("Can not open connection ! ");
}
dt = ds.Tables["subject"];
return dt;
 
Share this answer
 
v2
Comments
Nnorss 6-May-13 9:39am    
Infinite thanks!! i still have a little problem, when i click another subject, the old articles stays so the list box show articlesofSubject1+articlesOfSubject2, i want that the listBox be refreshed when i change the subject in the combobox, so the listBox must show only articlesOfSubjectSelected!
Member 10012743 6-May-13 14:27pm    
try to add this statement before assigning values to listBox1:

listBox1.Items.Clear();

which will clear all the data in listBox1 before assigning new values.
Nnorss 6-May-13 15:19pm    
I tried this statement in several places of the code, always the same problem!
Member 10012743 7-May-13 8:39am    
could you send the coding?
Nnorss 8-May-13 0:54am    
here is the actual code:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
string theme = comboBox1.SelectedText;
int idtheme = Convert.ToInt32(comboBox1.SelectedValue);
dt = sources_view(idtheme);
for (int i = 0; i < dt.Rows.Count; i++)
{

listBox1.Items.Add(dt.Rows[i]["Url"]);
}
}

public DataTable sources_view(int idtheme)
{
try
{
myConnection.Open();
SqlCommand command = new SqlCommand("select * from Sources where IdTheme =" + idtheme, myConnection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
adapter.Fill(ds, "theme");
adapter.Dispose();
command.Dispose();
myConnection.Close();

}
catch (SqlException exp)
{
MessageBox.Show(exp.Message);
}

dt = ds.Tables["theme"];
return dt;
}
using System.Data.SqlClient;


SqlConnection con = new SqlConnection();
DataSet ds = new DataSet();
DataTable dt = new DataTable();

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string subject = comboBox1.SelectedText;
listBox1.Items.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
listBox1.Items.Add(dt.Rows[i][/*subjet_column_no*/].ToString());
}
}

public DataTable subject_view()
{
try
{
con.Open();
SqlCommand command = new SqlCommand("select * from subject_table", con);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
adapter.Fill(ds, "subject");
adapter.Dispose();
command.Dispose();
con.Close();
}
catch (Exception ex)
{
//MessageBox.Show("Can not open connection ! ");
}
dt = ds.Tables["subject"];
return dt;
}
 
Share this answer
 
Comments
Nnorss 5-May-13 18:02pm    
The listBox must show the "NameArticle" collection of the subject selected, "table_articles" is not exploited in your solution!! Thanks for help anyway!
Member 10012743 6-May-13 14:31pm    
instead of :


SqlCommand command = new SqlCommand("select * from subject_table", con);

type:

SqlCommand command = new SqlCommand("select * from table_articles", con);

its would be ok.
just change the values according to your need.
just follow the format or the idea how its been performed.

if you like it use it.
better dont try to copy and paste.
First understand how it is done; so you can do this type of problems by yourself.

-Irshad
Nnorss 6-May-13 14:49pm    
thank you very much Irshad! i still have a little problem, when i click another subject, the old articles stays so the list box show articlesofSubject1+articlesOfSubject2, i want that the listBox be refreshed when i change the subject in the combobox, so the listBox must show only articlesOfSubjectSelected!

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