下面我的代码
DataSet ds = new DataSet();
public void bind()
{
        SqlConnection myconnection = data.CreateConnection();
        SqlDataAdapter da = new SqlDataAdapter("select*from Student", myconnection);
        myconnection.Open();
        da.Fill(ds, "Student");
        datalist绑定;
        myconnection.Close();
}
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection myconnection = DB.CreateCon();
        myconnection.Open();
        SqlDataAdapter da=new SqlDataAdapter("select*from Student",myconnection);
        SqlCommandBuilder bu = new SqlCommandBuilder(da);
        foreach (DataListItem item in Suppliers.Items)
        {
            int ID = Convert.ToInt32(Suppliers.DataKeys[item.ItemIndex]);
            DataRow[] myrow = newds.Tables["Student"].Select("ID='ID'");
            string sex= ((TextBox)item.FindControl("sex")).ToString();
            string age = ((TextBox)item.FindControl("age")).ToString();
            string nclass = ((TextBox)item.FindControl("class")).ToString();
            myrow["sex"] = sex;sex是char(2)
            myrow["age"] =Convert.ToInt32(age);age是int
            myrow["Class"] =nclass; Class是varchar(20)      
       }
      dar.Update(newds.Tables["Student"]); 
      myconnection.Close();
    }运行就出错:
行 46: myrow["sex"] = sex;请指点,如何更新到数据库;
先谢谢了!

解决方案 »

  1.   

    myrow是个数组,不是DataRow对象
      

  2.   

    那么如何改?怎么做才能dataset更新到数据库?
      

  3.   

    DataRow[] myrow = newds.Tables["Student"].Select("ID='ID'"); 
                string sex= ((TextBox)item.FindControl("sex")).ToString(); 
                string age = ((TextBox)item.FindControl("age")).ToString(); 
                string nclass = ((TextBox)item.FindControl("class")).ToString(); 
                myrow[0]["sex"] = sex;sex是char(2) 
                myrow[0]["age"] =Convert.ToInt32(age);age是int 
                myrow[0]["Class"] =nclass; Class是varchar(20)   
      

  4.   

     myrow[0]["sex"] = sex;sex是char(2) 
      

  5.   

    改了现在出错"未将对象引用设置到对象的实例";
    是不是DataRow[] myrow = newds.Tables["Student"].Select("ID='ID'"); 的Select("ID='ID'"); 
    大家帮帮忙!!!!帮解决的再加分
    非常急!
      

  6.   

    未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 是不是DataRow[] myrow = newds.Tables["Student"].Select("ID='ID'"); 的Select("ID='ID'"); 
    大哥帮我看一下!
      

  7.   

    新年快乐单步调试阿.. 都不知道哪里出错..
    "未将对象引用设置到对象的实例"的错误是有null.***这种, 
    看看你myconnection, newds, newds.Tables["Student"], item.FindControl("...")有没有那个为null不。。Select条件看起来是有错的,每次都是查ID为"ID"的行 
    不过即使有错不会报"未将对象引用设置到对象的实例"这个错误,查不到任何行也应该是数组下标越界之类的。另外这个地方不知道你写的什么:
    string sex= ((TextBox)item.FindControl("sex")).ToString(); 
    string age = ((TextBox)item.FindControl("age")).ToString(); 
    string nclass = ((TextBox)item.FindControl("class")).ToString(); 
    一个TextBox.ToString()有什么意义? 
    至少应该是((TextBox)item.FindControl("sex")).Text.ToString()吧?
      

  8.   

    就是这个句DataRow[] myrow = newds.Tables["Student"].Select("ID='ID'");
    大哥应该怎么改呢?
      

  9.   

    挺DataRow[] myrow = newds.Tables["Student"].Select("ID='ID'"); 
    大哥应该怎么改呢?
      

  10.   

    挺DataRow[] myrow = newds.Tables["Student"].Select("ID='ID'"); 
    大哥应该怎么改呢?