0

I want to store textbox's text into string variable but an error occurs:

System.NullReferenceException: Object reference not set to an instance of an object

Code:

 Dim Name As String = txtName.Text
Dim Email As String = txtEmail.Text
Dim Password As String = txtPassword.Text
Dim Repassword As String = txtRepassword.Text
Dim Country As String = txtCountry.Text
Dim College As String = txtCollege.Text
Dim Phone As String = txtPhone.Text

It shows that txtName.text is NULL; how can I fix it?

My control code is here:

'''<summary>
'''txtName control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents txtName As Global.System.Web.UI.WebControls.TextBox
'''<summary>
'''txtEmail control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents txtEmail As Global.System.Web.UI.WebControls.TextBox
'''<summary>
'''txtPassword control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents txtPassword As Global.System.Web.UI.WebControls.TextBox
'''<summary>
'''txtRepasword control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents txtRepassword As Global.System.Web.UI.WebControls.TextBox
'''<summary>
'''txtCountry control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents txtCountry As Global.System.Web.UI.WebControls.TextBox
'''<summary>
'''txtCollege control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents txtCollege As Global.System.Web.UI.WebControls.TextBox
'''<summary>
'''txtPhone control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents txtPhone As Global.System.Web.UI.WebControls.TextBox
halfer
  • 18,701
  • 13
  • 79
  • 158
Ashutosh
  • 25
  • 1
  • 2
  • 8
  • Double check the name and type of your control. It seems like txtName is either not the name of a control or, if it is, it isn't a control type with the Text property. –  Jun 15 '16 at 20:40
  • You can't get txtName value at the time of declaration; you need to try to capture at button click or some other event where you needed those values – techspider Jun 15 '16 at 20:40
  • @techspider i also tried capture textbox value at button click but same error occurs – Ashutosh Jun 15 '16 at 20:44
  • @Kalmino I checked no any problem in Contol – Ashutosh Jun 15 '16 at 20:48
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Tony Chiboucas Jun 16 '16 at 00:15

1 Answers1

0

Some error checking will help you avoid stack traces

If String.IsNullOrEmpty(txtName.Text) Then
Dim name As String = txtName.Text
End If
JKerny
  • 438
  • 3
  • 18