0

我有一个编辑个人资料页面,用户可以在其中编辑他们的个人资料信息。目前,新用户无法使用此页面,因为他们在“userprofiles”表中没有相应的记录。我在同一个数据库中使用带有相应 aspnet_User 表的 aspnet_membership 系统。'userprofiles' 和 aspnet 表之间没有链接

我有一个名为“DisplayData()”的子程序,它检查表中是否有用户记录,并在文本框中显示他们的个人资料信息。不幸的是,对于新用户,表中没有记录,因此会引发错误

这是我的 Page_Load 子:

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("userName") = User.Identity.Name
        Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        conn.Open()
        Dim cmdcheck As New OleDbCommand("SELECT TravellerName FROM userprofiles WHERE (TravellerName = ?) ", conn)
        cmdcheck.Parameters.AddWithValue("@userName", Session("userName"))
        Dim profileCheckDr = cmdcheck.ExecuteReader()
        If IsDBNull(profileCheckDr("TravellerName")) Then
            ??
        End If
        If Not IsPostBack Then
            DisplayData()
            savec.Visible = False
        End If
    End Sub

这是我的 DisplayData() 函数,它将所有当前用户配置文件信息输入到页面上的文本框中:

Protected Sub DisplayData()
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        Dim sql = "SELECT * FROM userprofiles WHERE TravellerName=@f1"
        Dim cmd = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        conn.Open()
        Dim profileDr = cmd.ExecuteReader()
        profileDr.Read()
        Dim newEmailAddress = ""
        Dim newDescription = ""
        Dim newDOB = ""
        Dim newLocation = ""
        Dim newProfession = ""
        Dim newSmoker = ""
        Dim newDrinker = ""
        Dim newEducationLevel = ""
        Dim newMaritalStatus = ""
        If Not IsDBNull(profileDr("AvatarURL")) Then ProfilePic.ImageUrl = profileDr.Item("AvatarURL")
        If Not IsDBNull(profileDr("EmailAddress")) Then newEmailAddress = profileDr.Item("EmailAddress")
        If Not IsDBNull(profileDr("DOB")) Then newDOB = profileDr.Item("DOB")
        If Not IsDBNull(profileDr("Location")) Then newLocation = profileDr.Item("Location")
        If Not IsDBNull(profileDr("Description")) Then newDescription = profileDr.Item("Description")
        If Not IsDBNull(profileDr("Profession")) Then newProfession = profileDr.Item("Profession")
        If Not IsDBNull(profileDr("Smoker")) Then newSmoker = profileDr.Item("Smoker")
        If Not IsDBNull(profileDr("Drinker")) Then newDrinker = profileDr.Item("Drinker")
        If Not IsDBNull(profileDr("EducationLevel")) Then newEducationLevel = profileDr.Item("EducationLevel")
        If Not IsDBNull(profileDr("MaritalStatus")) Then newMaritalStatus = profileDr.Item("MaritalStatus")
        If Not IsDBNull(profileDr("AvatarURL")) Then ProfilePic.ImageUrl = profileDr.Item("AvatarURL")
        description.Text = newDescription
        email.Text = newEmailAddress
        smoker.SelectedValue = newSmoker
        drinker.SelectedValue = newDrinker
        dd_userlocation.SelectedValue = newLocation
        dob.Text = newDOB
        educationlevel.SelectedValue = newEducationLevel
        profession.SelectedValue = newProfession
        maritalstatus.SelectedValue = newMaritalStatus

        conn.Close()

    End Sub

如果页面加载子中的查询结果不返回任何内容,我如何突破页面加载子,以便 DisplayData() 子不会运行。我已经尝试过使用“Exit Sub”,但它似乎不起作用。

4

1 回答 1

1

一个快速的解决方法是简单地将您的 If 语句移动到调用周围DisplayData

If Not IsPostback Then
    If Not IsDBNull(profileCheckDr("TravellerName")) Then
        DisplayData()
    End If
End If

然而,真正的问题是——如果用户还没有注册你的网站,他们怎么能进入编辑个人资料页面? 未注册的用户应该无法访问您网站的用户特定部分。

于 2013-05-05T11:27:53.310 回答