2014-01-24 18 views
0

最近,我們的數據庫崩潰了,我們留下了用戶的電子表格記錄。我們想在成功登錄後向用戶展示一條消息,以更新他們的記錄。任何想法爲什麼代碼不起作用?

我們將數據從電子表格導入我們的用戶表。

由於有多個用戶數量超過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 [email protected]", 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

嗯。在同一個代碼文件中有「IsPostBack」和「SqlConnection」。你可能想讀這本老書,但好東西。 http://msdn.microsoft.com/en-us/library/Ee817644%28pandp.10%29.aspx – granadaCoder

+0

您可以給這個合理的標題,並在問題的正文中包含實際問題和信息。 –

+0

這與C#..待辦事項是什麼? – Dieterg

回答

2

If nameTB = "" or AddressTB = "" or phoneTB = "" or phoneTB is null or PrecinctTB = "" or PositionTB = "" Then 

線是使用nameTB nameTB已經潛在地被分配的值之前。您只在If dr2.Read() Then區塊內爲nameTB分配一個值。
如果代碼不在該塊內部,則您的字符串將不會被賦值。

+0

夥計們,感謝您的信息。我只會關注正面的反饋。我絕對不介意批評;只要你有一些有用的反饋,它就會與領土一起出現。爲此,我非常感謝Golden和GranadaCoder。 Golden在進行您所推薦的更改後,代碼將按照我們的預期工作。非常感謝 –

相關問題