数据集和数据表的问题

时间:2013-04-01 17:01:25

标签: c# asp.net

正在研究一种方法,该方法从学生数据库中提取学生的结果,所有年级的所有科目结果都存储在一个表中,因此该函数将提取特定部门的类的结果并将其存储在一个表中。数据集中的数据表,所以这是我到目前为止所做的代码

public void Button1_Click(object sender, EventArgs e)
{
        con.Open();
        cmd.CommandText = "SELECT Course.Course_code, Course.Course_name, courseyear.level FROM Course INNER JOIN courseyear ON Course.Course_code = courseyear.Course_code WHERE (courseyear.dep = '" + dep.SelectedValue + "') AND (courseyear.subdep = '" + subdep.SelectedValue + "') AND (courseyear.year = '" + year.SelectedValue + "') AND (courseyear.level = '" + level2.SelectedValue + "')";
        da = new SqlDataAdapter(cmd.CommandText, con);
        ds.Clear();
        da.Fill(ds, "junk");
        cmd.CommandText = "SELECT Course.Course_code, Course.Course_name, courseyear.level FROM Course INNER JOIN courseyear ON Course.Course_code = courseyear.Course_code WHERE (courseyear.dep = '" + dep.SelectedValue + "') AND (courseyear.subdep = '" + subdep.SelectedValue + "') AND (courseyear.year = '" + year.SelectedValue + "') AND (courseyear.level = '" + level2.SelectedValue + "')";
        da = new SqlDataAdapter(cmd.CommandText, con);
        da.Fill(ds, "subs");
        ds.Tables.Add("res");
        cnum =  ds.Tables["junk"].Rows.Count;

        //l7de hna kweseen


        ds.Tables["res"].Columns.Add(new DataColumn("index",typeof(string)));
        ds.Tables["res"].Columns.Add(new DataColumn("name", typeof(string)));
        //int rnum = ds.Tables["res"].Rows.Count;
        for (int x = 0; x < cnum-1; x++) {

            ds.Tables["res"].Columns.Add(new DataColumn(ds.Tables["junk"].Rows[x].ItemArray[0].ToString(), typeof(string)));

        }//done inserting subjects 

        dr = ds.Tables["res"].NewRow();
        ds.Tables["junk"].Clear();
        cmd.CommandText = "SELECT id, name FROM Students WHERE (dep ='"+dep.SelectedValue+"') AND (class = '"+level2.SelectedValue+"') AND (subDep ='"+subdep.SelectedValue+"')";
        da = new SqlDataAdapter(cmd.CommandText, con);
        da.Fill(ds, "junk");
        int snum = ds.Tables["junk"].Rows.Count;
        for (int x = 0; x < snum; x++)
        {
            dr = ds.Tables["res"].NewRow();
            dr.ItemArray[0] = ds.Tables["junk"].Rows[x].ItemArray[0];
            dr.ItemArray[1] = ds.Tables["junk"].Rows[x].ItemArray[1];
            //ds.Tables["res"].Rows.Add(dr);
        }//done inserting students

        ShowPopUpMsg(ds.Tables["res"].Rows.Count.ToString());//for  testing
        ShowPopUpMsg(snum.ToString());// for testing
        for (int x = 0; x < cnum ;x++ )//for subjects
        {
            for (int y =0; y < snum;y++)//for students 
            {
                ds.Tables["junk"].Clear();
                cmd.CommandText = "select course_work+final_exam as exp from results where student_id = '" + ds.Tables["res"].Rows[y ].ItemArray[0] + "' and course_code = '"+ds.Tables["subs"].Rows[x]+"' ";
                da.Fill(ds, "junk");
                ds.Tables["res"].Rows[y].ItemArray[x] = ds.Tables["junk"].Rows[0].ItemArray[0];                   
            }
        }
        ShowPopUpMsg(ds.Tables["junk"].Rows[0].ItemArray[0].ToString());
    }
    private void ShowPopUpMsg(string msg)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("alert('");
        sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
        sb.Append("');");
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
    }
}</i>

我的问题是它一直告诉我学生循环

的第0位没有行

1 个答案:

答案 0 :(得分:0)

您可能希望使用两个不同的数据适配器来填充2个不同的数据集。如果你看它,你将重新实例化数据适配器,这将重置以前加载的“垃圾”数据集。因此,当您尝试访问“垃圾”时,您看不到任何内容。尝试不实例化或使用不同的适配器。