Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I am making login process in 3Tire mode, When I start to run the program

 

it shows the error messages as following

"An unhandled exception of type 'System.NullReferenceException' occurred in datalayer.dll

 

Additional information: Object reference not set to an instance of an object.

"

 

It refers to this code

Imports System.Data
Imports System.Data.SqlClient

Public Class Class1
   Dim myconnection As SqlConnection = New SqlConnection("server=IRtt;database=PDR;" & _
 "uid=sa;pwd=jj2003;")
   Dim myadapter As SqlDataAdapter = New SqlDataAdapter("select UserName,Password FROM Staff", myconnection)
   Dim myds As DataSet
   Dim mydv As DataView

   Dim mydv1 As DataView


  Public Sub filldatasetandview()

       myds = New DataSet

       myadapter.Fill(myds, "Staff")


       mydv = New DataView(myds.Tables("Staff"))

   End Sub

   Public Function Search(ByVal test3 As String)
       Dim a As Integer
       mydv.Sort = "StaffID DESC"

       a = mydv.Find(test3)
       Return a

   End Function
 End Class

--------------------------------------------------------------------------------

Posted

Error

 

Hi,

 

The error was happeded in this line

mydv.Sort = "StaffID DESC"

 

When I compiled it, it did not have any problem.

 

After I enter the User ID and the Password and pressed the submit button, then it starts to have this problem

 

Thanks,

Fj8283888

Posted

Reply

 

Looks like you haven't assigned an instance of the DataView to mydv. I can see that the filldatasetandview function creates an instance - is this function being called before the Search routine?

Yes, that's correct, the filldatasetandview function runs before the search routine.

Posted (edited)

attachment

 

Hi,

 

I attached the file with this message, the file contains three layers files. Presentation,Application,Datalayer.

 

Please tell the problem of the program after you view the coding

 

Regards,

Fj8283888

3Tier.zip

Edited by PlausiblyDamp
  • Administrators
Posted

Not had chance to have a proper look - haven't got VS installed on this machine yet.

However I think the problem lies with how you are creating / instantiating your variables.

 

For example in your form you have the following code:


   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim a As String

       Dim newappl As New application.Class1

       newappl = New application.Class1
       newappl.AppUser(TextBox1.Text)
       'newappl.AppPassword(TextBox2.Text)
   End Sub
 
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       Dim newappl As New application.Class1
       newappl = New application.Class1
       newappl.Start()

   End Sub

You are declaring an instance of application.Class1 and calling .Start on it in the form_load but declaring a seperate instance in the button click event. You may find the following is closer to what you need

Dim newappl As application.Class1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim a As String

       newappl.AppUser(TextBox1.Text)
       'newappl.AppPassword(TextBox2.Text)
End Sub
 
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       newappl = New application.Class1
       newappl.Start()

   End Sub

You are also doing a similar thing in application.Class1 try the following instead to see if it helps

Public Class Class1
Dim newdata1 As datalayer.Class1
   Public Function AppUser(ByVal test1 As String)

       Dim a As Integer

       a = newdata.Search(test1)

       If a <> -1 Then
           MsgBox("Yes")

       End If
       'newdata.AddStadd(test1, test2)

       'Return newdata.Final(test1, test2)
   End Function

   'Public Function AppPassword(ByVal test2 As String)

   '    'newdata.AddStadd(test1, test2)
   '    'Return newdata.Final(test1, test2)
   'End Function

   Public Sub Start()
       newdata1 = New datalayer.Class1
       newdata1.filldatasetandview()
   End Sub

End Class

Also code like

       Dim newdata As New datalayer.Class1
       newdata = New datalayer.Class1

is redundant as the first line declares a new instance of the class and then the second line declares another new instance.

 

Hopefully I should have VS installed by tomorrow and can have a better look (notepad isn't the best editor around ;)) - so if there are any other problems jusr reply here.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Administrators
Posted

Did you change all the pieces of your code to match my suggestions? If so are you getting the same error in the same place or something new?

If possible could you repost the code including those changes? Also as I suggestion I would recommend renaming your files / classes to reflect the purpose rather than leaving them at Class1 - will make the code a lot more maintainable in the long run.

 

I would also recomend putting the following 2 lines at the very top of your source files

Option Explicit On
Option Strict On

and fixing any errors that are now raised.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Reply

 

Thanks for your reply. Itry to do the way that you told me, but it does not work.

 

I attached the code below which is Datalayer (class)

When I type option explicit on and Option strict on, it has a blue color curly line under the option.

And it said 'Option' statements must precede any declarations or 'Imports' statements.

 


Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.SqlClient

Public Class Class1


   Dim myconnection As SqlConnection = New SqlConnection("server=ABC;database=657yt;" & _
 "uid=sa;pwd=56re;")
   Dim myadapter As SqlDataAdapter = New SqlDataAdapter("select StaffID FROM Staff", myconnection)
   Dim myds As DataSet
   Dim mydv As DataView

   Dim mydv1 As DataView


   Public Sub filldatasetandview()

       myds = New DataSet

       myadapter.Fill(myds, "Staff")


       mydv = New DataView(myds.Tables("Staff"))

   End Sub

   Public Function Search(ByVal test3 As String)

       Dim a As Integer


       mydv.Sort = "StaffID"
       If mydv.Find(test3) Then
           Return a
       End If

      

   End Function

End Class

Thanks,

fj8283888

Posted (edited)

reply

 

Thanks,

I fixed the problem, but another problem comes,

:o

The code does not have any error when compiling, how it has some problem with the return number from this code

dim dv as dataview

dataview.find("values")

 

let say I want to find staffid in the staff table

the attribute of the staff table as following

 

(row) StaffID, FirstName, LastName, Address

0 1 Peter MNer fdsfsdf

1 2 Andy Liern dsfsdfsdf

 

 

If I use find function to find the staffid which is 1, then it return value to zero which is make sense. When I tried to find the lastname, let say's the first record Mner, it will return a value, which is not zero.

Could anyone please tell me what's happend?

the code as following

 

Public Class Class1
   Private myds As DataSet
   Sub Class1()
       Dim myconnection As SqlConnection = New SqlConnection("server=ABC;database=abc;" & "uid=sa;pwd=jj2003;")
       Dim myadapter As SqlDataAdapter = New SqlDataAdapter("select StaffID, UserName,Password FROM Staff", myconnection)
       Dim mydv As DataView
       myds = New DataSet
       myadapter.Fill(myds, "Staff")
       'If you do search on different columns move the line below to the appropriate function
       'otherwise leave it in here so that you don't have to sort the column everytime you do a search
       myds.Tables("Staff").DefaultView.Sort = "StaffID"
   End Sub

   Public Function Search(ByVal test3 As String) As Integer
       If Not myds Is Nothing Then
           If (myds.Tables.Count > 0) Then
               Return myds.Tables("Staff").DefaultView.Find(test3)
           Else
               Return -1
           End If
       Else
           Return -1
       End If
   End Function
End Class

test.zip

Edited by PlausiblyDamp

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...