Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlConnection sqlnew2 = new SqlConnection();
       sqlnew2.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=Training;Integrated Security=True";

       sqlnew2.Open();


       SqlCommand cmd = new SqlCommand("select DepId from Department where DepName=@DeptName;", sqlnew2);
       cmd.Parameters.Add(new SqlParameter("@DeptName", DepNameDropDown.SelectedItem.ToString()));
       SqlDataReader sqlread = cmd.ExecuteReader();



       while (sqlread.Read())
       {
           SqlConnection sqlnew3 = new SqlConnection();
           sqlnew3.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=Training;Integrated Security=True";

           sqlnew3.Open();

           string depid = sqlread["DepId"].ToString();

           SqlCommand cmd2 = new SqlCommand("select EmpID,EmpName,EmpAddress from Employee where DepId='" + depid + "'", sqlnew3);



           SqlDataAdapter da = new SqlDataAdapter();

           da.SelectCommand = cmd2;
           DataSet newds = new DataSet();
           da.Fill(newds);
           DataListView.DataSource = newds;
           DataListView.DataBind();



       }


basically
here is the code
i have 2 tables in my data base
one table holds

Dep ID[prmary key]
Dep name

other table contains
EmpId[Primary key]
EmpName
EmpAddress
DepId[Foreign Key]

my cod = first select code will get the DepId
and depid stored as a string

second query will compare the data with that string
and view all the data according to that string

and i need to view it in listbox or gridview

the scenario is like this.....
i have a combo box and when software starts sql data will load to the combo box
when combo box value select
all the data should view in the datagridview..

the problem is when i click the combo box.....
data will not load into the datagridview
Posted
Updated 19-Nov-13 8:55am
v2
Comments
Maciej Los 19-Nov-13 12:38pm    
And what have you done till now? Where are you stuck?
The Manoj Kumar 19-Nov-13 15:24pm    
This seems fairly straight forward and I am sure if you will do a search by keywords, you will find what you are looking for.
anglo0072 19-Nov-13 16:51pm    
Please post your full code so we can understand that what you are trying to do actually and then can help you.. it`should not be a big deal
JoCodes 19-Nov-13 22:59pm    
Where are you writing this code , OnselectedIndexchanged event of combobox?Have you tried to debug the code . Let us know about the specific error you are facing...
Jobless Creature 20-Nov-13 1:06am    
May be you need to rebind the grid inside the combo box selected index event.

1 solution

 
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