读书人

ASP.NET做页面后执行当前 Web 请求

发布时间: 2013-09-06 10:17:17 作者: rapoo

求助,ASP.NET做页面后,执行当前 Web 请求期间出现错误!!未将对象引用设置到对象的实例
还是执行web页面时候出现的错误:
“/Web”应用程序中的服务器错误。

未将对象引用设置到对象的实例。

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:


行 43: {
行 44: Console.Write(e.Message);
行 45: throw e;
行 46: }
行 47: }

源文件: D:\HR\HRDAL\ResumeService.cs 行: 45



附ResumeService.cs代码(字数限制,省去一部分用不到的):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HRModels;
using System.Data.SqlClient;

namespace HRDAL
{
public static partial class ResumeService
{
//添加简历
public static Resume AddResume(Resume resume)
{
string sql=
"INSERT Resume (userId, name, sex, identityNo, birthday, stature, degree, school, marriage, occupation, career, specialty, email, phone, address)"+"VALUES(@userId, @name, @sex, @identityNo, @birthday, @stature, @degree, @school, @marriage, @occupation, @career, @specialty, @email, @phone, @address)";
sql +="; SELECT @@IDENTITY";
try
{
SqlParameter[] para=new SqlParameter[]
{
new SqlParameter("@userId",resume.User.UserId),//FK
new SqlParameter("@name",resume.Name),
new SqlParameter("@sex",resume.Sex),


new SqlParameter("@identityNo",resume.IdentityNo),
new SqlParameter("@birthdy",resume.Birthday),
new SqlParameter("@stature",resume.Stature),
new SqlParameter("@degree",resume.Degree),
new SqlParameter("@school",resume.School),
new SqlParameter("@marriage",resume.Marriage),
new SqlParameter("@occupation",resume.Occupation),
new SqlParameter("@career",resume.Career),
new SqlParameter("@specialty",resume.Specialty),
new SqlParameter("@email",resume.Email),
new SqlParameter("@phone",resume.Phone),
new SqlParameter("@address",resume.Address)
};
int newId=DBhelper.GetScalar(sql, para);
return GetResumeByResumeId(newId);
}
catch(Exception e)
{
Console.Write(e.Message);
throw e;
}
}
//修改简历


public static void ModifyResume(Resume resume)
{
string sql =
"update Resume" +
"set" +
"userId=@userId," +//FK
"name=@name," +
"sex=@sex," +
"identityNo=@identityNo," +
"birthday=@birthday," +
"stature=@stature," +
"degree=@degree," +
"school=@school," +
"marriage=@marriage," +
"occupation=@occupation," +
"career=@career," +
"specialty=@specialty," +
"email=@email," +
"phone=@phone," +
"address=@address," +
"where resumeId=@resumeId";
try
{
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@resumeId",resume.ResumeId),
new SqlParameter("@userId",resume.User.UserId),//FK


new SqlParameter("@name",resume.Name),
new SqlParameter("@sex",resume.Sex),
new SqlParameter("@identityNo",resume.IdentityNo),
new SqlParameter("@birthdy",resume.Birthday),
new SqlParameter("@stature",resume.Stature),
new SqlParameter("@degree",resume.Degree),
new SqlParameter("@school",resume.School),
new SqlParameter("@marriage",resume.Marriage),
new SqlParameter("@occupation",resume.Occupation),
new SqlParameter("@career",resume.Career),
new SqlParameter("@specialty",resume.Specialty),
new SqlParameter("@email",resume.Email),
new SqlParameter("@phone",resume.Phone),
new SqlParameter("@address",resume.Address),
};
DBhelper.ExecuteCommand(sql,para);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;


}
}
//获取所有简历
public static IList<Resume> GetAllResumes()
{
string sqlAll = "select * from Resume";
return GetResumeBySql(sqlAll);
}
//根据resumeId获取简历
public static Resume GetResumeByResumeId(int resumeId)
{
string sql = "SELECT * FROM Resume WHERE ResumeId=@ResumeId";
int userId;
try
{
SqlDataReader reader = DBhelper.GetReader(sql, new SqlParameter("@ResumeId", resumeId));
if (reader.Read())
{
Resume resume = new Resume();
resume.ResumeId = (int)reader["resumeId"];
resume.Name = (string)reader["name"];
resume.Sex = (string)reader["sex"];
resume.IdentityNo = (string)reader["identityNo"];
resume.Birthday = (DateTime)reader["birthday"];
resume.Stature = (string)reader["stature"];
resume.Degree = (string)reader["degree"];


resume.School = (string)reader["school"];
resume.Marriage = (string)reader["marriage"];
resume.Occupation = (string)reader["occupation"];
resume.Career = (string)reader["career"];
resume.Specialty = (string)reader["specialty"];
resume.Email = (string)reader["email"];
resume.Phone = (string)reader["phone"];
resume.Address = (string)reader["address"];
userId = (int)reader["userId"];//FK
reader.Close();
resume.User = UserService.GetUserByUserId(userId);
return resume;
}
else
{
reader.Close();
return null;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;


}
}
//根据userId获取简历
public static Resume GetResumeByUserId(int userId)
{
string sql = "select * from Resume where userId=@UserId";
try
{
SqlDataReader reader = DBhelper.GetReader(sql,new SqlParameter("@userId",userId));
if (reader.Read())
{
Resume resume = new Resume();
resume.ResumeId = (int)reader["resumeId"];
resume.Name = (string)reader["name"];
resume.Sex = (string)reader["sex"];
resume.IdentityNo = (string)reader["identityNo"];
resume.Birthday = (DateTime)reader["birthday"];
resume.Stature = (string)reader["stature"];
resume.Degree = (string)reader["degree"];
resume.School = (string)reader["school"];
resume.Marriage = (string)reader["marriage"];
resume.Occupation = (string)reader["occupation"];
resume.Career = (string)reader["career"];


resume.Specialty = (string)reader["specialty"];
resume.Email = (string)reader["email"];
resume.Phone = (string)reader["phone"];
resume.Address = (string)reader["address"];
reader.Close();
resume.User = UserService.GetUserByUserId(userId);
return resume;
}
else
{
reader.Close();
return null;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}
}
}


还有一个问题:为什么我的页面用detailsview控件绑定的headertext显示出来的文字都是竖向排列的:




这个方式的呢?设置了headstyle 的width也是无法改变,有什么办法可以让他横向排列吗?




谢谢大家了先,论坛大神很多,受益很多,在此谢谢先前帮我的大神!!! ASP.NET SQL 对象 Web


[解决办法]
空引用错误,调用了null的对象
这种问题你调一下就知道了,贴一大堆代码上来看不晕么
[解决办法]
以前遇到过这样的问题,貌似就是你传过去的这些SqlParameter,如果里面的某一个或多个参数有为空的情况就会报这样的异常,插入断点单步条跟一下
[解决办法]
SqlParameter传的值可能有空的,对比一下数据库是不是有的字段不能为空

读书人网 >asp.net

热点推荐