Asp.net 如果对数据库的查询结果为DbNull,则退出sub
我有一个编辑配置文件页面,用户可以在其中编辑他们的配置文件信息。此时,新用户无法使用此页面,因为他们在“userprofiles”表中没有相应的记录。我使用的是aspnet_u成员系统,在同一数据库中有相应的aspnet_u用户表。“userprofiles”和aspnet表之间没有链接 我有一个名为“DisplayData()”的子项,它检查表中是否有用户的记录,并在文本框中显示用户的配置文件信息。不幸的是,对于新用户,表中没有记录,因此会抛出错误 这是我的页面加载子项:Asp.net 如果对数据库的查询结果为DbNull,则退出sub,asp.net,vb.net,ms-access,Asp.net,Vb.net,Ms Access,我有一个编辑配置文件页面,用户可以在其中编辑他们的配置文件信息。此时,新用户无法使用此页面,因为他们在“userprofiles”表中没有相应的记录。我使用的是aspnet_u成员系统,在同一数据库中有相应的aspnet_u用户表。“userprofiles”和aspnet表之间没有链接 我有一个名为“DisplayData()”的子项,它检查表中是否有用户的记录,并在文本框中显示用户的配置文件信息。不幸的是,对于新用户,表中没有记录,因此会抛出错误 这是我的页面加载子项: Protect
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
如果Page Load sub中的查询结果没有返回任何内容,如何中断Page Load sub,使DisplayData()sub不会运行。我已经尝试过使用“Exit Sub”,但它似乎不起作用。一个快速解决方法是简单地将If语句移动到调用
DisplayData
:
If Not IsPostback Then
If Not IsDBNull(profileCheckDr("TravellerName")) Then
DisplayData()
End If
End If
然而,真正的问题是-如果用户尚未注册您的站点,他们如何才能访问编辑配置文件页面?未注册的用户不应该能够访问您站点的用户特定部分。将对
DisplayData
的调用包装在if块中-如下所示:if not IsDBNull(profileCheckDr(“TravellerName”))然后显示数据()如果