Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

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

ASP.NET C#

if (txtname.text == "")
  lblErr.Text = "Error! need to type something";

Why does a simple line like this throw an exception?
i used to validate this way in VB.NET
0
jedistar
Asked:
jedistar
  • 5
  • 4
  • 3
1 Solution
 
Elvio Lujan.Net Senior DeveloperCommented:
make a break point and tell us in what line is the error
0
 
Raju SrivatsavayeSoftware EngineerCommented:
where exactly are you trying to validate this..the code you have written is not wrong technically so just show a part of code where you have written this code
0
 
jedistarAuthor Commented:
if (txtname.text == "") <- red

its in a btnclick event {

if (txtname.text == "") {
  lblErr.Text = "Error! need to type something";
  return; //exit
}
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Elvio Lujan.Net Senior DeveloperCommented:
can post the declaration line of txtname
0
 
jedistarAuthor Commented:
protected System.Web.UI.WebControls.TextBox txtname;
0
 
Elvio Lujan.Net Senior DeveloperCommented:
ok and now make sure in the html code yo have the textBox with the same name "txtname" and it's have the runat="server" attribute
0
 
jedistarAuthor Commented:
yeah it is intact!

only thing is that: there is code behind to decide whether to display the textbox
as i may not display it when lbl says 0

<% if (lbl.Text == "0") { %>
..textbox..

..
0
 
Elvio Lujan.Net Senior DeveloperCommented:
someTimes the textBox in not visible?
0
 
Raju SrivatsavayeSoftware EngineerCommented:
It might be that txtname is invisible when the button is hit..Anyway make sure there is a txtbox on the page when the button is hit..
and it is visible
0
 
jedistarAuthor Commented:
yeah so how? i need the code behind there.
0
 
Elvio Lujan.Net Senior DeveloperCommented:
no in the codebehind yon can't access the invisible controls... the way is don't put it in visible=false... just set the width=0px and set it to readonly=true
0
 
Raju SrivatsavayeSoftware EngineerCommented:
Are you setting the text box invisible in your page load..can you give your code
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now