ユーザーを管理者の場合は「A」、標準ユーザーの場合は「U」に分類するユーザー名、パスワード、およびユーザー識別子を含むWebフォームがあります。
フォームを送信すると、現在他のユーザーがいるVisualStudioでセットアップしたデータベースに書き込む必要があります。
Webフォームをテストすると、「NullReferenceExceptionがユーザーコードによって処理されませんでした」、「オブジェクト参照がオブジェクトのインスタンスに設定されていません」というエラーが表示されます。そして、それは私のWebフォームページのこのコード行を指しています。
clsDataLayer.SaveUser(Server.MapPath("PayrollSystem_DB.mdb"), Session["txtUserName"].ToString(), Session["txtPassword"].ToString(), Session["drpdwnlstSecurityLevel"].ToString());
このコード行に何か問題がありますか?
「txtPassword」というラベルの付いたテキストボックス、「txtPassword」というラベルの付いたテキストボックス、および「drpdwnlstSecurityLevel」というラベルの付いたUまたはAのオプションを含むドロップダウンリストがあります。
情報を送信すると、次のようなclsDataLayer.csSaveUserメソッドに送信されることになっています。
public static bool SaveUser(string Database, string UserName, string UserPassword, string SecurityLevel)
{
bool userSaved;
try
{
// Define SQLConnClass
OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Database);
conn.Open();
OleDbCommand command = conn.CreateCommand();
string strSQL;
// this insert data to user table
strSQL = "Insert into tblUserLogin " +
"(UserName, UserPassword, SecurityLevel) values ('" +
UserName + "', '" + UserPassword + "', " + SecurityLevel + "')";
// this gives a command to get or set values
command.CommandType = CommandType.Text;
command.CommandText = strSQL;
// This sql statements brings out the affacted rows
command.ExecuteNonQuery();
// closes the connection
conn.Close();
userSaved = true;
}
catch (Exception ex)
{
userSaved = false;
}
return userSaved;
}
私のWebフォームで新しいユーザーを作成しようとすると、レコードは作成されず、前述のエラーのみが発行されます。
この質問に関連するすべてのコードは次のとおりです。
ファイルclsDataLayer.cs:
//この関数は、ユーザーデータを保存しますpublic static bool SaveUser(string Database、string UserName、string UserPassword、string SecurityLevel){
bool userSaved;
try
{
// Define SQLConnClass
OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Database);
conn.Open();
OleDbCommand command = conn.CreateCommand();
string strSQL;
// this insert data to user table
strSQL = "Insert into tblUserLogin " +
"(UserName, UserPassword, SecurityLevel) values ('" +
UserName + "', '" + UserPassword + "', " + SecurityLevel + "')";
// this gives a command to get or set values
command.CommandType = CommandType.Text;
command.CommandText = strSQL;
// This sql statements brings out the affacted rows
command.ExecuteNonQuery();
// closes the connection
conn.Close();
userSaved = true;
}
catch (Exception ex)
{
userSaved = false;
}
return userSaved;
}
ファイルfrmManageUsers.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class frmManageUsers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAddUser_Click(object sender, EventArgs e)
{
string userName, userPassword;
if (txtUserName.Text == "" || txtUserName.Text == null)
{
lblUserError.Text = ("User Name may not be empty");
lblUserError.ForeColor = System.Drawing.Color.Red;
return;
}
else
userName = (txtUserName.Text);
if (txtPassword.Text == "" || txtPassword.Text == null)
{
lblUserError.Text = ("Password may not be empty");
lblUserError.ForeColor = System.Drawing.Color.Red;
return;
}
else
{
userPassword = (txtPassword.Text);
}
// clsDataLayer.SaveUser(Server.MapPath("PayrollSystem_DB.mdb"), Session["txtUserName"].ToString(), Session["txtPassword"].ToString(), Session["drpdwnlstSecurityLevel"].ToString());
clsDataLayer.SaveUser(
Server.MapPath("PayrollSystem_DB.mdb"),
txtUserName.Text,
txtPassword.Text,
drpdwnlstSecurityLevel.SelectedValue
);
Server.Transfer("frmManageUsers.aspx");
}
}
ファイルfrmManageUsers.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmManageUsers.aspx.cs" Inherits="frmManageUsers" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<a href="frmMain.aspx">
<font color="black" size="2" style="text-align: center"><strong>
<font color="blue" face="Comic Sans MS" size="4">Cool</font>
<font color="#ff6600" face="Comic Sans MS" size="4">Biz</font>
<font face="Comic Sans MS" size="4"> <font color="#993366">Productions</font>,
Inc.</font></strong></font>
</a>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Manage Users"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="User Name: "></asp:Label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="Password: "></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lblUserError" runat="server"></asp:Label>
<br />
<asp:Label ID="Label4" runat="server" Text="Security Level: "></asp:Label>
<asp:DropDownList ID="drpdwnlstSecurityLevel" runat="server"
DataSourceID="SqlDataSource2" DataTextField="SecurityLevel"
DataValueField="SecurityLevel">
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:PayrollSystem_DBConnectionString %>"
ProviderName="<%$ ConnectionStrings:PayrollSystem_DBConnectionString.ProviderName %>"
SelectCommand="SELECT [SecurityLevel] FROM [tblUserLogin]">
</asp:SqlDataSource>
<br />
<br />
<asp:Button ID="btnAddUser" runat="server" onclick="btnAddUser_Click"
Text="Add User" />
<br />
<br />
<asp:GridView ID="grdUserLogin" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="UserID" HeaderText="UserID" InsertVisible="False"
SortExpression="UserID" />
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" />
<asp:BoundField DataField="UserPassword" HeaderText="UserPassword"
SortExpression="UserPassword" />
<asp:BoundField DataField="SecurityLevel" HeaderText="SecurityLevel"
SortExpression="SecurityLevel" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:PayrollSystem_DBConnectionString %>"
InsertCommand="INSERT INTO [tblUserLogin] ([UserID], [UserName], [UserPassword], [SecurityLevel]) VALUES (?, ?, ?, ?)"
ProviderName="<%$ ConnectionStrings:PayrollSystem_DBConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM [tblUserLogin]">
<InsertParameters>
<asp:Parameter Name="UserID" Type="Int32" />
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="UserPassword" Type="String" />
<asp:Parameter Name="SecurityLevel" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>