未将对象引用设置到对象的实例,请问如何解决
行 57: st_comm.Parameters["@Course_id"].Value = Dropdownlist_course.SelectedItem.Value;
调试总是提示未将对象引用设置到对象的实例。 提示上面的这一句有错
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
STGROUP.ST_addgrade.st_btn_add_Click(Object sender, EventArgs e) in g:\StuPerformanceSys\HeadTeahcer\ST_addgrade.aspx.cs:57
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +97
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4921
======================================================================================================
下面是该页面aspx.cs代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace STGROUP
{
/// <summary>
/// ST_addgrade 的摘要说明。
/// </summary>
public partial class ST_addgrade : System.Web.UI.Page
{
SqlConnection st_conn;
protected void Page_Load(object sender, System.EventArgs e)
{
string st_connstr= ConfigurationSettings.AppSettings["st_dbconn"];
// 在此处放置用户代码以初始化页面
st_conn=new SqlConnection(st_connstr);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.st_cv_id.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.st_cv_id_ServerValidate);
}
#endregion
protected void st_btn_add_Click(object sender, System.EventArgs e)
{
string st_sqlstr="update ST_student_course set ST_Student_grade=@Student_grade where ST_Student_id=@Student_id and ST_Course_id=@Course_id and ST_Course_year=@Course_year";
SqlCommand st_comm=new SqlCommand(st_sqlstr,st_conn);
st_comm.Parameters.Add(new SqlParameter("@Student_id",SqlDbType.VarChar,50));
st_comm.Parameters["@Student_id"].Value=st_tbx_studentid.Text;
st_comm.Parameters.Add(new SqlParameter("@Course_id",SqlDbType.VarChar,50));
st_comm.Parameters["@Course_id"].Value = Dropdownlist_course.SelectedItem.Value;
st_comm.Parameters.Add(new SqlParameter("@Course_year",SqlDbType.Char,10));
st_comm.Parameters["@Course_year"].Value=st_tbx_term.Text;
st_comm.Parameters.Add(new SqlParameter("@Student_grade",SqlDbType.Int,4));
st_comm.Parameters["@Student_grade"].Value=st_tbx_grade.Text;
st_comm.Connection.Open();
try
{
st_comm.ExecuteNonQuery();
Response.Redirect("ST_grade_manage.aspx");
}
catch(SqlException)
{
st_lbl_note.Text="添加失败";
st_lbl_note.Style["color"]="red";
}
st_comm.Connection.Close();
}
protected void st_tbx_studentid_TextChanged(object sender, System.EventArgs e)
{
//课程名称下拉列表框绑定
string st_connstr = ConfigurationSettings.AppSettings["st_dbconn"];
SqlConnection st_conn0 = new SqlConnection(st_connstr);
st_conn0.Open();
string st_sql1 = "select ST_course.* from ST_student_course,ST_course where ST_student_course.ST_Student_id='" + st_tbx_studentid.Text + "'and ST_student_course.ST_Course_id=ST_course.ST_Course_id";
SqlCommand st_comm1 = new SqlCommand(st_sql1, st_conn0);
SqlDataReader st_dr1 = st_comm1.ExecuteReader();
while (st_dr1.Read())
{
Dropdownlist_course.Items.Add(new ListItem(st_dr1["ST_Course_name"].ToString(), st_dr1["ST_Course_id"].ToString()));
}
st_conn0.Close();
}
private void st_cv_id_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string st_connstr= ConfigurationSettings.AppSettings["st_dbconn"];
st_conn=new SqlConnection(st_connstr);
st_conn.Open();
string st_sqlstr="select * from ST_student where ST_Student_id='"+st_tbx_studentid.Text+"'";
SqlCommand st_comm=new SqlCommand(st_sqlstr,st_conn);
SqlDataReader st_dr=st_comm.ExecuteReader();
if(st_dr.Read())
{
args.IsValid=true;
}
else
{
args.IsValid=false;
}
st_conn.Close();
}
protected void Dropdownlist_course_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
}
}
=====================================================================================================
下面是该页面的aspx跟这有关的代码:
<TABLE id="Table2" cellSpacing="1" cellPadding="1" width="80%" align="center" border="0">
<TR>
<TD style="WIDTH: 130px" align="right"><FONT face="宋体">学号:</FONT></TD>
<TD><asp:textbox id="st_tbx_studentid" runat="server" Width="133px" AutoPostBack="True" ontextchanged="st_tbx_studentid_TextChanged"></asp:textbox><asp:customvalidator id="st_cv_id" runat="server" ControlToValidate="st_tbx_studentid" ErrorMessage="无此学生!"></asp:customvalidator></TD>
</TR>
<TR>
<TD style="WIDTH: 130px" align="right"><FONT face="宋体">课程:</FONT></TD>
<TD><asp:dropdownlist id="Dropdownlist_course" runat="server" Width="108px" onselectedindexchanged="Dropdownlist_course_SelectedIndexChanged"></asp:dropdownlist></TD>
</TR>
<TR>
<TD style="WIDTH: 130px" align="right"><FONT face="宋体">年度:</FONT></TD>
<TD><asp:textbox id="st_tbx_term" runat="server" Width="134px"></asp:textbox><FONT face="宋体" color="#ff0000" size="2">例:2005-2006</FONT></TD>
</TR>
<TR>
<TD style="WIDTH: 130px" align="right"><FONT face="宋体">成绩:</FONT></TD>
<TD><asp:textbox id="st_tbx_grade" runat="server" Width="135px"></asp:textbox></TD>
</TR>
<TR>
<TD style="WIDTH: 130px"></TD>
<TD><asp:button id="st_btn_add" runat="server" Text="录入" onclick="st_btn_add_Click"></asp:button><FONT face="宋体">
</FONT>
<asp:label id="st_lbl_note" runat="server"></asp:label></TD>
</TR>
</TABLE>
==================================================================================================
还望各位大侠能够帮小弟检查检查,在下感激不尽,送上高分当谢礼!期待回音!