最近,我们的数据库崩溃了,我们留下了用户的电子表格记录。
我们将电子表格中的数据导入到Users表中。
由于有多个用户的数量远远超过3千,我们希望在主页上以粗体文本和黄色文字颜色显示一条消息,以便用户一登录,如果他/她有任何人记录是空白或无效的,他/她会看到这个建议她去他/她的个人资料页面并更新他/她的个人记录。
更新用户记录后,此消息将不再显示。
我确定不是我做错了,但下面的代码无效。
在分配值之前,我一直收到nameTB使用的错误。我相信其余部分都是一样的。
我需要做些什么才能让它发挥作用?
If Not IsPostBack Then
If Session("username") Is Nothing Then
' Redirect user to login before doing anything else
Response.Redirect("~/Login.aspx?redirect=default.aspx")
Else
Dim myConnectionString As [String] = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim myConnection As New SqlConnection(myConnectionString)
Dim nameTB As String
Dim AddressTB As String
Dim emailTB As String
Dim phoneTB As String
Dim PrecinctTB As String
Dim PositionTB As String
Try
myConnection.Open()
'*
' * Find personal info
'
Dim cmd2 As New SqlCommand("Select l.FullName,l.address,l.Phone_nbr,l.email,l.precinct,p.position_title from Users l inner join Posts p on l.positionId = p.positionId and l.username = @username and l.email=@email", myConnection)
cmd2.Parameters.AddWithValue("@username", Session("username"))
cmd2.Parameters.AddWithValue("@email", Session("UserId"))
Dim dr2 As SqlDataReader = cmd2.ExecuteReader()
If dr2.Read() Then
nameTB = dr2("FullName").ToString()
AddressTB = dr2("address").ToString()
phoneTB = dr2("phone_nbr").ToString()
emailTB = dr2("email").ToString()
PrecinctTB = dr2("precinct").ToString()
PositionTB = dr2("Position_title").ToString()
End If
If nameTB ="" or AddressTB ="" or phoneTB ="" or phoneTB is null or PrecinctTB = "" or PositionTB = "" Then
lblMessage.Text = " Please update your profile"
else
End If
'Check to see if any field is blank or null
dr2.Close()
Catch ex As SqlException
Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('" + ex.Message + "')</SCRIPT>")
Finally
myConnection.Close()
End Try
End If
End If
答案 0 :(得分:2)
你的
If nameTB = "" or AddressTB = "" or phoneTB = "" or phoneTB is null or PrecinctTB = "" or PositionTB = "" Then
行在nameTB可能被赋值之前使用nameTB。您只需在If dr2.Read() Then
块内为nameTB分配值
如果代码没有进入该块,则不会为您的字符串分配值。