用ASP构建登录..NET在VB中

本文关键字:VB NET 登录 ASP 构建 | 更新日期: 2023-09-27 18:11:03

我发现了一个关于用asp.net建立网站登录的youtube视频,问题是它是在c#中,但我一直在将其转换为VB,当我运行程序时,我得到了这个错误

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web       request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: 
The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:
1. Add a "Debug=true" directive at the top of the file that generated the error. Example:
or:
2) Add the following section to the configuration file of your application:
<configuration>
<system.web>
   <compilation debug="true"/>
</system.web>
</configuration>

我知道这意味着c#中有些东西,但我找不到它是什么。下面是代码。

       Imports System
       Imports System.Collections.Generic
       Imports System.Linq
       Imports System.Web
       Imports System.Web.UI
       Imports System.Web.UI.WebControls
       Imports System.Data.SqlClient
       Imports System.Configuration

Partial Class Registration
Inherits System.Web.UI.Page

Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click
    Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").ConnectionString)
    con.Open()
    Dim insCmd As String = "Insert into Registration (Username, Password, EmailAddress, FullName, Country) values (@UserName, @Password, @EmailAddress, @FullName, @Country)"
    Dim InsertUser As SqlCommand = New SqlCommand(insCmd, con)
    InsertUser.Parameters.AddWithValue("@UserName", TextBoxUN.Text)
    InsertUser.Parameters.AddWithValue("@Password", TextBoxPass.Text)
    InsertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text)
    InsertUser.Parameters.AddWithValue("@FullName", TextBoxFN.Text)
    InsertUser.Parameters.AddWithValue("@Country", DropDownListCountry.SelectedItem.ToString())
    Try
        InsertUser.ExecuteNonQuery()
        con.Close()
        Response.Redirect("Login.aspx")
    Catch ex As Exception
        Response.Write("You have just launched a nuclear warhead and started WWIII seek shelter ASAP")
    End Try
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (IsPostBack) Then

        Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").ConnectionString)
        con.Open()
        Dim cmdStr As String = "SELECT COUNT(*) FROM Registration WHERE UserName='" + TextBoxUN.Text + "'"
        Dim userExist As SqlCommand = New SqlCommand(cmdStr, con)
        Dim temp As Integer = Convert.ToInt32(userExist.ExecuteScalar().ToString())
        con.Close()
        If temp = 1 Then
            Response.Write("Username already exist. Please choose another username")
        End If
    End If
End Sub
End Class

如果你看到什么,请告诉我。谢谢!

用ASP构建登录..NET在VB中

老实说,我唯一要做的就是一步一步,确保没有"无值",我不熟悉if语句设置,我更愿意=true或not(条件)而不是isPostback,不记得答案是否默认为true但我会看看那两个

Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").ConnectionString)

可以用

替换上面的代码吗?

Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("RegconnectionString").toString())