我该如何解决null referce变量 [英] how do i solve null referce variable

查看:118
本文介绍了我该如何解决null referce变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi.i am sandhya.i想要访问登录页面hen i gv only user specified userid和pwd .otherthan登录页面uerid和pwd,它会显示错误信息。

代码是

Hi.i am sandhya.i want to acces login page hen i gv only user specified userid and pwd .otherthan login page uerid and pwd,it will display error msg.
the code is

Imports System.Data.OleDb

Partial Class Default2
    Inherits System.Web.UI.Page

    Private Property UserName As Object

    Private Property auth As Boolean



    Private Property txtPwd As Object

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connString As String = "Data Source=C:\Users\Administrator\Desktop\login.mdb;Provider=Microsoft.Jet.OleDb.4.0"
        Dim qryString As String = "SELECT username, password FROM login;"
        Dim objConn As New OleDbConnection(connString)
        Dim objCmd As New OleDbCommand(qryString, objConn)
        Dim myReader As OleDbDataReader

        Try

            Dim gotUser As Boolean = False
            objConn.Open()
            myReader = objCmd.ExecuteReader

            While myReader.Read
               .. If myReader.Item("username") = UserName.Text Then.. ' This is the error line
                    gotUser = True
                    auth = CheckPwd(myReader.Item("username"), myReader.Item("password"))
                End If

            End While
            If gotUser = False Then
                Label3.Text = "You are not authorized for access"
            Else
                Select Case auth
                    Case True
                        FormsAuthentication.RedirectFromLoginPage(UserName.Text, False)
                    Case Else
                        Label3.Text = "Incorrect Password"
                End Select
            End If

        Catch ex As Exception

            Label3.Text = ex.ToString

        Finally
            objConn.Close()
        End Try
        
    End sub

    Public Function CheckPwd(ByVal username As String, ByVal password As String) As Boolean

        Dim Authorized As Boolean = False
        Dim pwdFromUser As String = txtPwd.Text

        If pwdFromUser = password Then
            Authorized = True
        Else
            Authorized = False
        End If

        Return Authorized

    End Function

End Class





at ... line我收到了错误





at ... line i got the error as

System.NullReferenceException: Object variable or With block variable not set. at Microsoft.VisualBasic.CompilerServices.Symbols.Container..ctor(Object Instance) at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack) at Default2.Button1_Click(Object sender, EventArgs e) in C:\Users\Administrator\Documents\Visual Studio 2010\WebSites\WebSite9\Default2.aspx.vb:line 28



请帮我


pls help me

推荐答案

好吧尝试使用username.ToString()方法而不是username.Text 。我看到你将用户属性定义为对象,所以我认为你需要使用Tostring方法将其转换为字符串。我也看到你没有为用户名对象赋值,为对象用户名写入get并设置



username =somename;



希望对您有所帮助。
okay try using username.ToString() method rather than username.Text . i see that you define user property as object so i think you need to cast it as a string using Tostring Method. also i see that you did not assign value to the username object, write get set for object username and set

username = "somename";

hope this help to you.


这篇关于我该如何解决null referce变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆