-3

我发现了一个关于使用 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

如果你看到任何东西,请告诉我。谢谢!

4

2 回答 2

1

老实说,我唯一要做的就是逐步检查并确保没有任何“没有价值”的东西,而且我不熟悉 if 语句设置,我会更喜欢 =true or not(condition) 而不是 isPostback,不记得答案是否默认为真,但我会看看那些 2

于 2013-04-25T13:34:47.650 回答
0

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

你能把上面的代码替换为

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

于 2013-04-25T15:23:26.817 回答