问题描述
只是从网站点书中做一个例子,我收到错误;
hi just doing an example from the site point book and i am getting an error;
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 20: SqlDataReader reader;
Line 21:
Line 22: string connectionString =
Line 23: ConfigurationManager.ConnectionStrings[
Line 24: "Dorknozzle1"].ConnectionString;
can有人请帮助
这是代码
使用System.Data.SqlClient;
使用System.Configuration;
名称空间Dorknozzle
{
公共部分类HelpDesk:System.Web.UI.Pa ge
{
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
SqlConnection conn;
SqlCommand categoryComm;
SqlCommand subjectComm;
SqlDataReader阅读器;
string connectionString =
ConfigurationManager.ConnectionStrings [
Dorknozzle1]。ConnectionString;
conn = new SqlConnection(connectionString);
categoryComm = new SqlCommand(
SELECT CategoryID,Category FROM HelpDeskCategories,
conn);
subjectComm = new SqlCommand(
SELECT SubjectID,Subject From HelpDeskSubkects,conn);
t ry
{
conn.Open();
reader = categoryComm.ExecuteReader();
categoryList。 DataSource = reader;
categoryList.DataValueField =CategoryID;
categoryList.DataTextField =Category;
categoryList.DataBind();
reader.Close();
reader = subjectComm.ExecuteReader();
subjectList.DataSource = reader;
subjectList.DataValueField =SubjectID;
subjectList.DataTextField =Subject;
subjectList.DataBind();
reader。关闭();
reader = categoryComm.ExecuteReader();
categoryList.DataSource = reader;
categoryList.DataValueField =CategoryID;
categoryList.DataTextField =Category;
categoryList.DataBind();
reader.Close( );
}
终于
{
conn.Close();
}
}
}
protected void submitButton_Click(object sender,EventArgs e)
{
if(Page.IsValid)
{
SqlConnection conn;
SqlCommand comm;
string connectionString =
ConfigurationManager.ConnectionStrings [
Dorknozzle1]。ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand(
INSERT INTO HelpDesk(EmployeeID,StationNumber,+
CategoryID,SubjectID,Description,StatusID)+
VALUES(@ EmployeeID,@ stationNumber,@ ClassoryID,+
@ SubjectID,@ Description,@ StatusID),conn);
comm.Parameters。添加(@ EmployeeID,System.Data.SqlDbType.Int);
comm.Parameters [@ EmployeeID]。值= 5;
comm.Parameters。添加(@ StationNumber,
System.Data.SqlDbType.Int);
comm.Parameters [@ StationNumber]。Value = stationTextBox.Text;
comm.Parameters.Add(@ CategoryID,System.Data.SqlDbType.Int);
comm.Parameters [@ CategoryID]。值=
categoryList.SelectedItem.Value;
comm.Parameters.Add(@ SubjectID,System .Data.SqlDbType.Int);
comm.Parameters [@ SubjectID]。值=
subjectList.SelectedItem.Value;
comm.Parameters.Add(@ Description,
System.Data.SqlDbType.NVarChar,50);
comm.Parameters [@ Description]。Value =
descriptionTextBox.Text;
comm.Parameters.Add(@ StatusID,System.Data.SqlDbType.Int);
comm。参数[@ StatusID]。值= 1;
尝试
{
conn.Open();
comm.ExecuteNonQuery();
Response.Redirect(HelpDesk.aspx);
}
catch
{
dbErrorMessage.Text =
提交服务台请求时出错!请+
稍后再试,和/或更改输入的数据!;
}
终于
{
conn.Close();
}
}
}
}
}
can anybody help please
this is the code
using System.Data.SqlClient;
using System.Configuration;
namespace Dorknozzle
{
public partial class HelpDesk : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn;
SqlCommand categoryComm;
SqlCommand subjectComm;
SqlDataReader reader;
string connectionString =
ConfigurationManager.ConnectionStrings[
"Dorknozzle1"].ConnectionString;
conn = new SqlConnection(connectionString);
categoryComm = new SqlCommand(
"SELECT CategoryID, Category FROM HelpDeskCategories",
conn);
subjectComm = new SqlCommand(
"SELECT SubjectID, Subject FROM HelpDeskSubkects", conn);
try
{
conn.Open();
reader = categoryComm.ExecuteReader();
categoryList.DataSource = reader;
categoryList.DataValueField = "CategoryID";
categoryList.DataTextField = "Category";
categoryList.DataBind();
reader.Close();
reader = subjectComm.ExecuteReader();
subjectList.DataSource = reader;
subjectList.DataValueField = "SubjectID";
subjectList.DataTextField = "Subject";
subjectList.DataBind();
reader.Close();
reader = categoryComm.ExecuteReader();
categoryList.DataSource = reader;
categoryList.DataValueField = "CategoryID";
categoryList.DataTextField = "Category";
categoryList.DataBind();
reader.Close();
}
finally
{
conn.Close();
}
}
}
protected void submitButton_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
SqlConnection conn;
SqlCommand comm;
string connectionString =
ConfigurationManager.ConnectionStrings[
"Dorknozzle1"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand(
"INSERT INTO HelpDesk (EmployeeID, StationNumber, " +
"CategoryID, SubjectID, Description, StatusID) " +
"VALUES (@EmployeeID, @StationNumber, @CategoryID, " +
"@SubjectID, @Description, @StatusID)", conn);
comm.Parameters.Add("@EmployeeID", System.Data.SqlDbType.Int);
comm.Parameters["@EmployeeID"].Value = 5;
comm.Parameters.Add("@StationNumber",
System.Data.SqlDbType.Int);
comm.Parameters["@StationNumber"].Value = stationTextBox.Text;
comm.Parameters.Add("@CategoryID", System.Data.SqlDbType.Int);
comm.Parameters["@CategoryID"].Value =
categoryList.SelectedItem.Value;
comm.Parameters.Add("@SubjectID", System.Data.SqlDbType.Int);
comm.Parameters["@SubjectID"].Value =
subjectList.SelectedItem.Value;
comm.Parameters.Add("@Description",
System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["@Description"].Value =
descriptionTextBox.Text;
comm.Parameters.Add("@StatusID", System.Data.SqlDbType.Int);
comm.Parameters["@StatusID"].Value = 1;
try
{
conn.Open();
comm.ExecuteNonQuery();
Response.Redirect("HelpDesk.aspx");
}
catch
{
dbErrorMessage.Text =
"Error submitting the help desk request! Please " +
"try again later, and/or change the entered data!";
}
finally
{
conn.Close();
}
}
}
}
}
推荐答案
这篇关于ASP.NET站点点dorknozzle示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!