开发者

Check request.form["field"] string length not working in ASP.net

I am currently trying to validate my ASP.NET form. I need to be to sure that the user has entered a password of at least 5 characters.

I have done a check to make sure that something is valid using the following code:

else if (Request.Form["txtPassword"] == "")
{}

I am then开发者_如何学Go checking that the characters is not less than 5 by using the following code:

if (Request.Form["txtPassword"].Length < 5)
{}

However, when I run the form and submit it, instead of it displaying the error to the user about the password length, I keep on getting an error from Visual Studio. Before I have tried to submit the form it displays:

Object reference not set to an instance of an object.

This error is only displayed when I am checking the length not if I am checking the String is empty.

Thanks for any help you can provide.


You have a null reference exception. Request.Form["txtPassword"] returns null. This is essentiall what is happening:

string myString = null;

// This is valid comparison because myString is a string object.
// However, the string is not *empty*, it is null, thus it has *no* value.
if(myString == "")
{
    DoSomething();
}

// This is not acceptable, as null.Length has no meaning whatsoever.
// The expression compiles because the variable is of type string,
// However, the string is null, so the "Length" property cannot be called.
if(myString.Length < 5)
{
    DoSomethingElse();
}

You cannot access the length of a null string. Try using this instead:

var myString = Request.Form["txtPassword"];

if(!String.IsNullOrEmpty(myString) && myString.Length < 5)
{
    DoSomething();
}

The next question, however, is WHY is it null? Perhaps you have inaccurately named the associated form input to something other than "txtPassword", or perhaps the data is not being sent via POST.


This probably means that Request.Form["txtPassword"] is null. I would first check that it exists.


Youy receive that error because you are calling the Length property on a null object, in this case Request.Form["txtPassword"] is null, Length cannot be called.

You might want to make sure your textbox has ID "txtPassword" remember that asp.net before .net 4 generates client IDs like "ctl00_txtPassword" and that becomes the Form field and you might need to enter Request.Form["ctl00_txtPassword"].


I had a similar problem. I was trying to post from one project to another. Because of requirements I was stuck using a regular HTML form. The values were always null. I beat my head against the wall with this one until I stumbled on the solution.

1st: NO runat="server" tag in your form header

 bad:  <form id="form1" runat="server"  action="http://yoursite.com/yourpage.aspx" method="post" >
 good: <form id="form1"   action="http://yoursite.com/yourpage.aspx" method="post" >

2nd:

.Net's html input box is .Net HTML Tag: <input id="myinput" type="text" />

The closing tag /> is the problem. If you remove the / it will work.

 bad: <input id="myinput" type="text" />
 good: <input name="myinput" type="text" >

3rd: be sure to use the "name" attribute instead of "id" attribute

 bad: <input id="myinput" type="text" />
 good: <input name="myinput" type="text" >


If the form tag is placed in Master Page eg:Site.Master, and the form you are submitting contains field

< asp:TextBox ID="amount" name="amount" runat="server" />

and is actually is inside the ContentPlaceHolder (ID="MainContent"), then if you use Request.Form["amount"] it does not work and always shows null value in it...because, when the page is rendered, the id actually becomes "ctl00$MainContent$amount". Hence, Request.Form["ctl00$MainContent$amount"] will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜