fj8283888 Posted September 11, 2004 Posted September 11, 2004 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 -------------------------------------------------------------------------------- Quote
Administrators PlausiblyDamp Posted September 11, 2004 Administrators Posted September 11, 2004 Which line raises the error? Also when it crashes check if any of your variables are currently null. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
fj8283888 Posted September 11, 2004 Author Posted September 11, 2004 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 Quote
Administrators PlausiblyDamp Posted September 11, 2004 Administrators Posted September 11, 2004 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
fj8283888 Posted September 11, 2004 Author Posted September 11, 2004 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. Quote
Administrators PlausiblyDamp Posted September 11, 2004 Administrators Posted September 11, 2004 If you step through the code in the debugger does it assign a value to mydv before you use it? If so is it being reset anywhere?. Failing that could you attach the whole code file to see if that helps. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
fj8283888 Posted September 11, 2004 Author Posted September 11, 2004 (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, Fj82838883Tier.zip Edited September 11, 2004 by PlausiblyDamp Quote
Administrators PlausiblyDamp Posted September 12, 2004 Administrators Posted September 12, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
fj8283888 Posted September 12, 2004 Author Posted September 12, 2004 Reply Hi, I tried to do it the way that you told me. but the errors it's still occured. Thanks, fj8283888 Quote
Administrators PlausiblyDamp Posted September 12, 2004 Administrators Posted September 12, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
fj8283888 Posted September 12, 2004 Author Posted September 12, 2004 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 Quote
Administrators PlausiblyDamp Posted September 12, 2004 Administrators Posted September 12, 2004 Strange, is there nothing above the lines begining with Option (e.g. no other import statements or namespace declarations?) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
fj8283888 Posted September 12, 2004 Author Posted September 12, 2004 Reply Hi, I put some Imports command above it even I try to put those below it, it does not work Thanks, Fj8283888 Quote
Administrators PlausiblyDamp Posted September 12, 2004 Administrators Posted September 12, 2004 Could you attach the file in question, I will check to see if it works here. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
fj8283888 Posted September 12, 2004 Author Posted September 12, 2004 (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 September 12, 2004 by PlausiblyDamp Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.