Archive for the ".NET" Category

8
Jan

Often times in code you want to assign one variable to another, but only if the variable is not null. If it is null, you want to populate the target variable with another ( perhaps default ) value. The code normally may look like this using the C# Conditional Operator:

string fileName = tempFileName != null ? tempFileName : “Untitled”;

If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.

This can now be abbreviated as follows using the Null-Coalescing Operator:

string fileName = tempFileName ?? “Untitled”;

The logic is the same. If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.

The Null-Coalescing Operator comes up a lot with nullable types, particular when converting from a nullable type to its value type:

int? count = null;

int amount = count ?? default(int);

Since count is null, amount will now be the default value of an integer type ( zero ).

These Conditional and Null-Coalescing Operators aren’t the most self-describing operators :) , but I do love programming in C#!

Reference : http://source.witssquare.com

VN:F [1.7.4_987]
Rating: 5.0/5 (1 vote cast)
VN:F [1.7.4_987]
Rating: +1 (from 1 vote)

11
Dec

1. As per functionality both GET and POST methods were same.Difference is GET method will be showing the information information to the users.But in the case of POST method information will not be shown to the user.

2. The data passed using the GET method would be visible to the user of the website in the browser address bar but when we pass the information using the POST method the data is not visible to the user directly.

3. Also in GET method characters were restricted only to 256 characters.But in the case of POST method characters were not restricted.
Get method will be visible to the user as it sended appended to the UML, put Post will not be visible as it is sent encapsulated within the HTTP request body.

4. About the data type that can be send, with Get method you can only use text as it sent as a string appended with the URL, but with post is can text or binary.

5. About form default, Get is the defualt method for any form, if you need to use the post method, you have to change the value of the attribute “method” to be Post.

Get method has maximum length restricted to 256 characters as it is sent appended with the URL, but the Post method hasn’t.

Reference : http://source.witssquare.com

VN:F [1.7.4_987]
Rating: 5.0/5 (1 vote cast)
VN:F [1.7.4_987]
Rating: +1 (from 1 vote)

17
Nov

This simple program will guide how to do client side validation of Form in JavaScript.

In this just make a form as follows:

  1. Name : <asp:TextBox ID=”txtName” />
  2. Email : <asp:TextBox ID=”txtEmail” />
  3. Web URL : <asp:TextBox ID=”txtWebUrl” />
  4. Zip : <asp:TextBox ID=”txtZip” />
  5. <asp:Button ID=”btnSubmit” OnClientClick=” return validate()” runat=”server” Text=”Submit” />

Now on the source code of this form in script tag write the following code:

<script language=”javascript” type=”text/javascript”>
function
validate()
{
if (document.getElementById(“<%=txtName.ClientID%>”).value==“”
)
{
alert(“Name Feild can not be blank”
);
document.getElementById(
“<%=txtName.ClientID%>”
).focus();
return false
;
}
if(document.getElementById(“<%=txtEmail.ClientID %>”).value==“”
)
{
alert(
“Email id can not be blank”
);
document.getElementById(“<%=txtEmail.ClientID %>”
).focus();
return false
;
}
var
emailPat = /^(\”.*\”|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid=document.getElementById(“<%=txtEmail.ClientID %>”
).value;
var
matchArray = emailid.match(emailPat);
if (matchArray == null
)
{
alert(
“Your email address seems incorrect. Please try again.”
);
document.getElementById(
“<%=txtEmail.ClientID %>”
).focus();
return false
;
}
if(document.getElementById(“<%=txtWebURL.ClientID %>”).value==“”
)
{
alert(
“Web URL can not be blank”
);
document.getElementById(
“<%=txtWebURL.ClientID %>”).value=
“http://”
document.getElementById(“<%=txtWebURL.ClientID %>”
).focus();
return false
;
}
var Url=
“^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$”
var tempURL=document.getElementById(“<%=txtWebURL.ClientID%>”
).value;
var
matchURL=tempURL.match(Url);
if(matchURL==null
)
{
alert(
“Web URL does not look valid”
);
document.getElementById(
“<%=txtWebURL.ClientID %>”
).focus();
return false
;
}
if (document.getElementById(“<%=txtZIP.ClientID%>”).value==“”
)
{
alert(
“Zip Code is not valid”
);
document.getElementById(
“<%=txtZIP.ClientID%>”
).focus();
return false
;
}
var digits=“0123456789″
;
var
temp;
for (var i=0;i<document.getElementById(“<%=txtZIP.ClientID %>”
).value.length;i++)
{
temp=document.getElementById(
“<%=txtZIP.ClientID%>”
).value.substring(i,i+1);
if
(digits.indexOf(temp)==-1)
{
alert(
“Please enter correct zip code”
);
document.getElementById(
“<%=txtZIP.ClientID%>”
).focus();
return false
;
}
}
return true
;
}
</script>

And in code behind file just write the below code.

In C#,

protected void Page_Load(object sender, System.EventArgs e)
{

btnSubmit.Attributes.Add(“onclick”, “return validate()”);

}

In VB,

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
btnSubmit.Attributes.Add(
“onclick”, “return validate()”
)
End Sub

Reference : http://source.witssquare.com

VN:F [1.7.4_987]
Rating: 5.0/5 (1 vote cast)
VN:F [1.7.4_987]
Rating: +1 (from 1 vote)