我发现了一个关于使用 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
如果你看到任何东西,请告诉我。谢谢!