我写了一个跟数据库表结构一样的Table 然后放进一个DataSet里面 在下面按键事件里使用QuestionDataSet()方法
为什么所以的DataRow都是空的啊 我已经给他们赋值了啊DataTable dtQuestion = new DataTable("Question");
public string QuestionDataSet()
{
DataColumn question = new DataColumn("Question", typeof(string));
question.MaxLength = 200;
dtQuestion.Columns.Add(question);//DataRow drQuestion = dtQuestion.NewRow();
drQuestion["Question"] = this.rbxQuestion.Text.Trim();
dtQuestion.Rows.Add(drQuestion);
DataSet dsQuestion = new DataSet();
dsQuestion.Tables.Add(dtQuestion); 
} private void btnAdd_Click(object sender, EventArgs e)
        {
            if (checkNull())
            {
                subManage.NewSub(QuestionDataSet(), this.rbxQuestion);
            }     
        }

解决方案 »

  1.   

    subManage.NewSub()是一个增加数据的方法
    把用户界面接受的参数放进上面的DataSet里面然后传给sql语句
    不知道这样调用对不对` 
      

  2.   

    我理解,函数QuestionDataSet()是不是应该返回一个DataSet  类型
    public string QuestionDataSet() 
    是不是应该改成
    public DataSet QuestionDataSet() 
      

  3.   

    public string QuestionDataSet() 
    返回值是string 型的..
    你没有返回值怎么能用??
      

  4.   

    那个String返回类型是我复制错了 我想返回一个String来看看每行里面是不是值
      

  5.   

    DataTable dtQuestion = new DataTable("Question"); 
    public DataSet QuestionDataSet() 

    DataColumn question = new DataColumn("Question", typeof(string)); 
    question.MaxLength = 200; 
    dtQuestion.Columns.Add(question);// DataRow drQuestion = dtQuestion.NewRow(); 
    drQuestion["Question"] = this.rbxQuestion.Text.Trim(); 
    dtQuestion.Rows.Add(drQuestion); 
    DataSet dsQuestion = new DataSet(); 
    dsQuestion.Tables.Add(dtQuestion); 
    return dsQuestion;
    } private void btnAdd_Click(object sender, EventArgs e) 
            { 
                if (checkNull()) 
                { 
                    subManage.NewSub(QuestionDataSet(), this.rbxQuestion); 
                }    
            }
    上面那段代码是这样的 return dsQuestion;里面的值是空的!
      

  6.   

    返回一个DataSetreturn dsQuestion
      

  7.   

    我复制了你的代码,QuestionDataSet()中的DataTable[0]的Rows数不为0,也就是说是有数据的,你在哪里发现它DataRow为空?
      

  8.   

    cmd.Parameters.Add("@Question", SqlDbType.VarChar, 200).Value = ds.Tables["Question"].Rows[0]["Question"];
    难道是这里错了么?
      

  9.   

     public int NewQuestion(DataSet ds)
            {
                int count;
                string sql = "insert into Question values(@Question,@Answer, @Difficulty ,@SubjectId,@OptionA ,@OptionB,@OptionC ,@OptionD)";
                SqlConnection conn = new SqlConnection(conStr);
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add("@Question", SqlDbType.VarChar, 200).Value = ds.Tables["Question"].Rows[0]["Question"];
                cmd.Parameters.Add("@Answer", SqlDbType.VarChar, 100).Value = ds.Tables["Question"].Rows[0]["Answer"];
                cmd.Parameters.Add("@Difficulty", SqlDbType.Int, 4).Value = ds.Tables["Question"].Rows[0]["Difficulty"];
                cmd.Parameters.Add("@SubjectId", SqlDbType.Int, 4).Value = ds.Tables["Question"].Rows[0]["SubjectId"];
                cmd.Parameters.Add("@OptionA", SqlDbType.VarChar, 100).Value = ds.Tables["Question"].Rows[0]["OptionA"];
                cmd.Parameters.Add("@OptionB", SqlDbType.VarChar, 100).Value = ds.Tables["Question"].Rows[0]["OptionB"];
                cmd.Parameters.Add("@OptionC", SqlDbType.VarChar, 100).Value = ds.Tables["Question"].Rows[0]["OptionC"];
                cmd.Parameters.Add("@OptionD", SqlDbType.VarChar, 100).Value = ds.Tables["Question"].Rows[0]["OptionD"];
                conn.Open();
                count= cmd.ExecuteNonQuery();
                conn.Close();
                conn.Dispose();
                return count;
            }
      

  10.   

     这个是中间层 就这点代码 
    public class NewSubManage
        {
            NewSubService newSubService = new NewSubService();        public DataSet GetAllSub()
            {
                return newSubService.GetAllSub();
            }        public string NewSub(DataSet ds,string questionName)
            {
                string massage = string.Empty;                if(newSubService.GetAllQuestion(questionName)!=0)
                    {
                        massage = "已经存在的试题";
                    }
                    if (newSubService.NewQuestion(ds) != 1)//这里调用了那个增加数据的方法
                    {
                        massage = "增加试题失败!";
                    }
                    else
                    {
                        massage = "增加试题成功!";
                        //newSubService.NewQuestion(
                    } 
                return massage;
            }
        }
    谢谢你了啊`  浪费你这么长时间`