private void BindGridView()
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string ValidateStr = "select max(Score) as score from Elect where courceID='" + ddlCource.SelectedValue + "' and teaID='"+Session["userName"].ToString()+"'";
        SqlConnection conn = new SqlConnection(connStr);    //创建连接对象
        if (conn.State.ToString() == "Closed")              //连接如果关闭,打开
            conn.Open();
        SqlCommand cmd = new SqlCommand(ValidateStr,conn);  //创建操作数据库对象
        SqlDataReader sdr = cmd.ExecuteReader();            //执行查询
        if (sdr.Read())
        {
            if ((int.Parse(sdr["score"].ToString())) > 0)   //判断成绩是否提交
            {
                GridView1.Visible = false;
                Response.Write("<script language=javascript>alert('该课程成绩已经提交,不能再次提交!')</script>");
            }
            else
            {
                sdr.Close();
                GridView1.Visible = true;
                string SqlStr = "SELECT student.*,Elect.* FROM Student ,Elect where Student.stuID=Elect.stuID and Elect.courceID='" + ddlCource.SelectedValue + "'order by Student.stuID, Student.stuGrade,Student.stuClass";
                DataSet ds = new DataSet();
                try
                {
                    if (conn.State.ToString() == "Closed")          //连接如果关闭,打开
                        conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
                    da.Fill(ds);                                    //向DataSet数据集填充数据                    GridView1.DataSource = ds.Tables[0].DefaultView;//为GridView指名数据源
                    GridView1.DataBind();                           //绑定数据
                }
                catch (Exception ex)                                //异常处理
                {
                    Response.Write("数据库错误,错误原因:" + ex.Message);
                    Response.End();
                }
                finally
                {
                    if (conn.State.ToString() == "Open")            //如果连接打开,关闭
                        conn.Close();
                }
            }
        }
        else
        {
            if (conn.State.ToString() == "Open")            //如果连接打开,关闭
                conn.Close();
        }
    }
预览时错误提示:if ((int.Parse(sdr["score"].ToString())) > 0)  语法错误

解决方案 »

  1.   

    印象中SqlDataReader 好像只能用Index来读数据吧(错了别骂我),你试着改成
    if ((int.Parse(sdr[0].ToString())) > 0)  
      

  2.   

    我都说了,SqlDataReader只支持Index读取数据,楼主以Column Name来访问当然会报语法错误了.....还奇怪呢,我这个几乎3年都没写过代码的人都看得出来问题在哪里。
      

  3.   


    MSDN:
    Gets the value of a column in its native format.
    Item[([(Int32])])  Gets the value of the specified column in its native format given the column ordinal.  
    Item[([(String])])  Gets the value of the specified column in its native format given the column name.  
      

  4.   

    呵呵,我怎么记得sqldatereader也是可以通过列名来读取值的啊,难道我错了?
      

  5.   

    如果数据库中Score一列的值为空会不会出现语法错误呢?
      

  6.   

      你确实错了, 我两年前就一直用的他的columnName来取值的~!~