简体   繁体   中英

problems with dataset and data table

am working on aa method that extracts results of students from students database in wich all the subjects results for all years and classes are stored in one table , so this function will extract the results of a specific department's class and store it in a datatable in a dataset so this is the code I made so far

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>

my problem is it keeps telling me there is no row at position 0 on the students for loop

You might want to use two different data adapters to fill 2 different datasets. If you look at it, you are re instantiating the data adapter, which will reset the previously loaded "junk" data set. So when you try to access "junk" you don't see anything. Either try not to instantiate or user different adapters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM