vb.net2008を使用してntierデスクトップアプリケーションを作成しようとしています。このコードを試しています
プレゼンテーション層
Imports System
Imports System.ComponentModel
Imports System.Data
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports Pos.Pos.BLL
Imports Pos.Pos.DAL
Imports System.Xml
Imports System.Collections.Generic
Public Class frmLogin
Private user As New User()
Private CnnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data " + "Source= " + Application.StartupPath + "\pos.accdb;Jet OLEDB:Database Password=pos"
Private cnn As New OleDbConnection()
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
'Try
'menu = user.getMenu()
Dim menu As New List(Of String)
If cnn.State = ConnectionState.Open Then
cnn.Close()
End If
Try
Dim jabatan As String = user.login(txtUser.Text, txtPassword.Text)
MsgBox(jabatan)
menu = user.getMenu(jabatan)
'Dim strCommand As String = "select Menu from tblJabatan where JabatanName='" + jabatan + "'"
'cnn.ConnectionString = CnnStr
'cnn.Open()
'Dim Comm As New OleDbCommand(strCommand, cnn)
'Dim dr As OleDbDataReader = Comm.ExecuteReader
'Do While dr.Read
'menu.Add(dr(0).ToString)
'Loop
If Not menu Is Nothing Then
For Each Str As String In menu
MsgBox(Str)
Next
Else
MsgBox("Tidak ditemukan data")
End If
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
ビジネスレイヤー
Public Class User
Private User As New daUser()
Private m_UserID As Integer
Private m_User As String
Private m_Pwd As String
Private m_Jabatan As String
Public Sub New()
MyBase.New()
End Sub
'''<summary>
'''ID User
'''</summary>
Public Property UserID() As Integer
Get
Return m_UserID
End Get
Set(ByVal value As Integer)
m_UserID = value
End Set
End Property
'''<summary>
'''Username
'''</summary>
Public Property Username() As String
Get
Return m_User
End Get
Set(ByVal value As String)
m_UserID = value
End Set
End Property
'''<summary>
'''Password
'''</summary>
Public Property Password() As String
Get
Return m_Pwd
End Get
Set(ByVal value As String)
m_Pwd = value
End Set
End Property
'''<summary>
'''Jabatan
'''</summary>
Public Property Jabatan() As String
Get
Return m_Jabatan
End Get
Set(ByVal value As String)
m_Jabatan = value
End Set
End Property
Public Sub add()
User.add(Me)
End Sub
Public Function login(ByVal Username As String, ByVal Password As String) As String
Return User.Login(Username, Password)
End Function
Public Function getMenu(ByVal Jabatan As String) As List(Of String)
Return User.getMenu(Jabatan)
End Function
End Class
データ層
Public Class daUser
Public Sub New()
cnn.ConnectionString = CnnStr
cnn.Open()
End Sub
Public Function Login(ByVal username As String, ByVal password As String) As String
'Dim role As String
If cnn.State = ConnectionState.Open Then
cnn.Close()
End If
Try
Dim strCom As String = "select Jabatan from tblUser where Username='" + username + "' and Password='" + password + "'"
cnn.ConnectionString = CnnStr
cnn.Open()
Dim Comm As New OleDbCommand(strCom, cnn)
Dim dr As OleDbDataReader = Comm.ExecuteReader
While dr.Read
role = dr(0).ToString
End While
cnn.Close()
Catch ex As Exception
Return ex.ToString
Finally
End Try
Return role
End Function
Public Function getMenu(ByVal jabatan As String) As List(Of String)
If cnn.State = ConnectionState.Open Then
cnn.Close()
End If
Try
Dim strCommand As String = "select Menu from tblJabatan where JabatanName='" + jabatan + "'"
cnn.ConnectionString = CnnStr
cnn.Open()
Dim Comm As New OleDbCommand(strCommand, cnn)
Dim dr As OleDbDataReader = Comm.ExecuteReader
While dr.Read
menu.Add(dr("Menu").ToString)
End While
cnn.Close()
Catch er As OleDb.OleDbException
Catch ex As Exception
Finally
End Try
Return menu
End Function
End Class
私がそれを実行すると、それは私にこの警告を与えます
System.NullReferenceException:C:\ Documents and Settings \ Administrator \ My Documents \ Visual Studio 2008 \ Projects \ POS \ Pos \のPos.frmLogin.btnLogin_Click(Object sender、EventArgs e)にあるオブジェクトのインスタンスにオブジェクト参照が設定されていませんForm \ frmLogin.vb:line 39
私が犯した間違いとその解決方法を教えてください
エラーのコードは次のとおりです。
For Each Str As String In menu
MsgBox(Str)
Next
ちなみに、私が達成したいのは、データをList(Of String)として取得する方法です。これは、getMenu関数をプレゼンテーション層に直接配置すると、期待どおりのデータが得られるためです。 List(Of String)の代わりに文字列を使用すると、単一のデータが得られます。ありがとう...