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) 语法错误
{
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) 语法错误
if ((int.Parse(sdr[0].ToString())) > 0)
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.