2010-11-22 23 views
0

我想要創建一個使用vb.net 2008.I'm嘗試這種代碼錯誤在返回字符串vb.net桌面應用程序

表示層

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 

業務一個n層桌面應用程序列表。層

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:對象未在Pos.frmLogin.btnLogin_Click(對象發件人,EventArgs的在c)中設置爲一個對象 的實例:\的Documents and Settings \ Administrator的\我的文檔\ Visual Studio 2008的\項目 \ POS \波什\表格\ frmLogin.vb:行39

請點我沒有我了哪些錯誤,以及如何解決它

錯誤的代碼是

For Each Str As String In menu 
MsgBox(Str) 
Next 

通過什麼我想要實現的方式是如何得到的數據作爲一個列表(串),因爲如果我直接把使用getMenu功能分爲表示層,它給了我,因爲我所期望的數據..如果我返回作爲一個字符串而不是列表(字符串),它給了我單一的數據。 謝謝...

+2

錯誤發生在'btnLogin_Click'事件(「對象引用未設置爲'Pos.frmLogin.btnLogin_Click'處的對象實例」)。請張貼那段代碼。 – 2010-11-22 05:58:36

+0

嗨科迪,第39行是For Each Str As String In菜單。謝謝 – 2010-11-22 14:32:01

+0

如果沒有代碼中的完整方法和*名稱*,我仍然很難看到發生了什麼。我很難理解你的項目是如何佈置的。這個問題在表現層中很明顯,所以這就是我們真正需要看到的代碼。如果你不想(或覺得它太多)在這裏發佈,你可以將它粘貼到[Pastie](http://www.pastie.org)並鏈接到這裏。 – 2010-11-23 08:34:19

回答

0

這是由於你沒有實例化菜單。將菜單的聲明更改爲

Dim menu As New List(Of String) 
+0

非常感謝,它現在的作品.. – 2010-11-27 18:11:31

+0

@yesaya handoyo - 沒問題。如果答案有幫助,將其標記爲適當的答案將不勝感激。 – 2010-11-29 18:38:34

0

有人應該告訴您,您在這方面有許多不妥之處。首先,也是最重要的是,像這樣構建動態SQL會使您的應用程序容易受到SQL注入的影響。另一件事是異常細節不應該返回給用戶。通過這樣做,你基本上已經給調用者一個「oracle」與SQL漏洞一起使用。只有這兩個問題,任何擁有基本SQL知識的人都可以爲自己的數據做任何他們想做的事情。還有一些其他項目(最後清空,空捕獲,不使用「使用」,對象命名衝突等),但安全項目更重要。

+0

嗨StingyJack,感謝您的輸入,我會改變代碼,順便說一下,你可以指出如何獲得數據,如我所料?如果我把getMenu函數直接放入表示層,我得到的數據..謝謝很多.. – 2010-11-22 17:46:29

+0

回覆時,請使用約定「@username」來獲得海報的注意。我沒有看到實例化類的業務層中的任何代碼。這是必需的,除非你使用共享/靜態功能 – StingyJack 2010-11-22 19:30:08

+0

我編輯了代碼,但我仍然無法得到結果,因爲我預期..謝謝你.. – 2010-11-23 01:31:02