未将对象引用设置到对象的实例,请问如何解决

shujian0034 2009-04-23 12:27:45
行 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>
==================================================================================================

还望各位大侠能够帮小弟检查检查,在下感激不尽,送上高分当谢礼!期待回音!
...全文
147 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimmy_jpy 2009-04-23
  • 打赏
  • 举报
回复
跟踪
teerhu 2009-04-23
  • 打赏
  • 举报
回复
st_comm.Parameters["@Course_id"].Value = Dropdownlist_course.SelectedItem.Value==null ? "" : Dropdownlist_course.SelectedItem.Value;
shujian0034 2009-04-23
  • 打赏
  • 举报
回复
可SelectedItem是连接到另一个ST_course表的啊,里面是有内容的
readfuture 2009-04-23
  • 打赏
  • 举报
回复
Dropdownlist_course.SelectedItem.Value;
---------------------------------------------
SelectedItem为空吧;
shujian0034 2009-04-23
  • 打赏
  • 举报
回复
我加入if(Dropdownlist_course.SelectedItem.Value!=null
{
st_comm.Parameters[0].Value = Dropdownlist_course.SelectedItem.Value;
} 后,还是出现未实例化问题
happy664618843 2009-04-23
  • 打赏
  • 举报
回复
值为空啊!
swcszqqq 2009-04-23
  • 打赏
  • 举报
回复
if(Dropdownlist_course.SelectedItem.Value!=null
{
st_comm.Parameters[0].Value = Dropdownlist_course.SelectedItem.Value;
}
mark620 2009-04-23
  • 打赏
  • 举报
回复
估计是哪个地方实例化完成之后没有赋值或没有初始值,多调试下看看。
nosuchtracter 2009-04-23
  • 打赏
  • 举报
回复
if(Dropdownlist_course.SelectedItem.Value!=null
{
st_comm.Parameters[0].Value = Dropdownlist_course.SelectedItem.Value;
}
先验证是否存在这项
wenkekenihao 2009-04-23
  • 打赏
  • 举报
回复
调试看哪里报错!
wuyq11 2009-04-23
  • 打赏
  • 举报
回复
if(Dropdownlist_course.SelectedItem.Value!=null
{
st_comm.Parameters[0].Value = Dropdownlist_course.SelectedItem.Value;
}
xqy_2007 2009-04-23
  • 打赏
  • 举报
回复
未将对象引用设置到对象的实例
应该是这儿Parameters["@Course_id"]的问题吧?
你换成
st_comm.Parameters[0].Value 试试.
youxiamao 2009-04-23
  • 打赏
  • 举报
回复
试下 Dropdownlist_course.SelectedValue

最好到这句的时候,监视看下Dropdownlist_course.SelectedItem.Value的值有没有
如果有,就从ADO方便找原因吧
RHCL 2009-04-23
  • 打赏
  • 举报
回复
不要直接 Dropdownlist_course.SelectedItem.Value使用它来赋值.

string _value=string.Empty;
if( Dropdownlist_course.SelectedItem.Value!=null) //你若是非用它直接赋值,加上try{}catch{}吧
{
_value= Dropdownlist_cours.SelectedItem.Value;
}
//另
Dropdownlist_course.Items.Add()这步你调下有数据没.注意生存周期.
hecong875 2009-04-23
  • 打赏
  • 举报
回复
Dropdownlist_course.SelectedItem.Value; 你看下这个的值是否为空了?
gciyfzx07 2009-04-23
  • 打赏
  • 举报
回复
这说明你的内容并没有获取到,或者是selecteditem没绑定上内容。跟踪一下
xielongjun1 2009-04-23
  • 打赏
  • 举报
回复
调试看看 哪里报错。
查询参数的值 是否为 NULL

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧