Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
SqlCommand com = new SqlCommand();
con.Open();
com.Connection = con;

//get course id's for this teacher
string cmd1 = "select course_id from course_teacher where teacher_id='" + Session["t_id"] + "'";
SqlDataAdapter da1 = new SqlDataAdapter(cmd1, con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
string cmd2 = "";
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
int courseid = Convert.ToInt32(ds1.Tables[0].Rows[i]["course_id"].ToString());
cmd2 += "select (course_name+'-'+class_name)from courses where course_id='" + courseid + "'";
}
SqlDataAdapter da2 = new SqlDataAdapter(cmd2, con);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
DropDownList1.DataSource = ds2;
DropDownList1.DataBind();

the problem is the adapter in the loop gets just the first statement...
Posted
Comments
[no name] 15-Apr-13 12:30pm    
Probably because,
1. you are concatenating select (course_name+'-'+class_name)from courses where course_id='" + courseid + "'" over and over which is going to turn out to be an invalid SQL statement
and 2 courseid is an integer and presumably an int in your database also but you are treating as if it were a string in your SQL statement.

Hi,

This code block:

SqlDataAdapter da2 = new SqlDataAdapter(cmd2, con);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
DropDownList1.DataSource = ds2;
DropDownList1.DataBind();


is outside for loop statement and that is the reason why you can't get results from the second adapter.

Cheers,
JAFC
 
Share this answer
 
Comments
amr mustafa 15-Apr-13 12:32pm    
i tried this but i want to do it without decrease the performance while get the data from the Database
José Amílcar Casimiro 15-Apr-13 12:46pm    
What do you mean by decrease the performance? Have the UI locked? If so, make an async thread to get the data.
all of the above you want to have the data in a dataset or datatable right. so pls do the following which should solve your problem

//get course ids for this teacher
string cmd1 = "select course_id from course_teacher where teacher_id='" + Session["t_id"] + "'";
SqlDataAdapter da1 = new SqlDataAdapter(cmd1, con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
string cmd2 = "";
Datatable dt-new DataTable();
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
int courseid = Convert.ToInt32(ds1.Tables[0].Rows[i]["course_id"].ToString());
cmd2 = "select (course_name+'-'+class_name)from courses where course_id='" + courseid + "'";
SqlDataAdapter da2 = new SqlDataAdapter(cmd2, con);
DataSet ds2 = new DataSet();
da2.Fill(ds2);

if(i=0)
{
dt=ds2.tables[0];
}
else
{
dt.merge(ds2.tables[0]);
}
}

DropDownList1.DataSource = dt;
DropDownList1.DataBind();


and i am sure it will solve your problem.....
 
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