More details at ASP.Net site. At that site you also get some solutions, and I have read on the web that many people suggests changing web.config. That's a bad solution since it will affect all pages on your site.
Instead add a tag validateRequest="false" into the Page-directive on top of the page you want users to be able to input HTML and script tags.
Its very simple, free and SEO Friendly. Submit Now....
Tuesday, May 27, 2008
A potentially dangerous Request.Form value was detected from the client
Monday, May 19, 2008
ByRef vs ByVal
ByRef and ByVal
When getting the value of a variable from a procedure or function parameter you have two ways to request it. Either you can request for it to be passed ByRef (default), or ByVal.
Passing ByVal
Private Sub TestSub(ByVal strPath As String)
Passing ByRef
Private Sub TestSub(ByRef strPath As String) ' or just
Private Sub TestSub(strPath As String)
When you pass ByVal, Visual Basic passes a copy of the variable to the procedure. This way the procedure gets the value, but any changes it makes will not affect the original variable.
When you pass ByRef, Visual Basic passes a pointer to the procedure. This is a reference so that the procedure knows where to find the variable in the memory. Any changes the procedure makes to this variable will effect the original one, as they are the same thing, however the variable does not need to be declared as public if you were wanting the procedure to access the variabl any other way. The following example shows the differences:
Sub Form_Load()
Dim strTest As String
'// fill the variable
strTest = "Hello from Form_Load"
'// call the procedure
Call TestSub(strTest)
'// display a message box containing the value of strTest
Msgbox strTest
End Sub
'// TestSub procedure when passing ByVal
Sub TestSub(ByVal strString As String)
strString = "Hello from TestSub"
'// when control returns to Form_Load, no changes will have
'// been made to strTest
End Sub
'// TestSub procedure when passing ByRef
Sub TestSub(ByRef strString As String)
strString = "Hello from TestSub"
'// when control returns to Form_Load, the value of
'// strTest will have changed to Hello from TestSub
End Sub
Note that when you are passing a variable ByRef, it must be declared as a specific datatype (ie string). Otherwise, VB cannot pass a pointer to it. If you do not do this, you will get a compile error:
ByRef Argument Type Mismatch.
This does not occur when passing ByVal. For example, the code below will produce a compile error when you press Command1:
Private Function TestFunction(ByRef sString As String)
sString = sString & vbCrLf
End Function
Private Sub Command1_Click()
Dim sTestString
TestFunction sTestString
End Sub
while, the code below would not, as you have explicitly declared sTestString As String.
Private Function TestFunction(ByRef sString As String)
sString = sString & vbCrLf
End Function
Private Sub Command2_Click()
Dim sTestString As String
TestFunction sTestString
End Sub
Friday, May 16, 2008
Thursday, May 15, 2008
Bulk Insert Into SQL SERVER from CSV / text file
This is very common request recently - How to import CSV file into SQL Server? How to load CSV file into SQL Server Database Table? How to load comma delimited file into SQL Server? Let us see the solution in quick steps.
CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values.
Create TestTable
USE TestData GO CREATE TABLE CSVTest(ID INT,FirstName VARCHAR(40),LastName VARCHAR(40),BirthDate SMALLDATETIME) GO Create CSV file in drive C: with name csvtest.txt with following content. The location of the file is C:\csvtest.txt
1,James,Smith,19750101 2,Meggie,Smith,19790122 3,Robert,Smith,20071101 4,Alex,Smith,20040202

Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.
BULK INSERT CSVTest FROM ‘c:\csvtest.txt’ WITH(FIELDTERMINATOR = ‘,’,ROWTERMINATOR = ‘\n’) GO Check the content of the table.
SELECT * FROM CSVTest GO Drop the table to clean up database.
SELECT * FROM CSVTest GO 
ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
If you get this Exception
[HttpException (0x80004005): Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.]
and
- you know *for sure* that you aren't using a web farm
- it seems that it appears when using built-in databound controls such as GridView, DetailsView or FormView which utilize DataKeyNames.
- it appears if you have a large page which loads slowly for any reason
If following preconditions are true and you click a postbacking control/link while the Page hasn't loaded completely, you might get the "Validation of ViewState MAC failed" exception. In this case be sure to check following post on ASP.NET Forums where this has been discussed quite thoroughly : http://forums.asp.net/1173230/ShowPost.aspx
It appears because GridView using DataKeyNames requires ViewState to be encrypted. And when ViewState is encrypted, Page adds <input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" /> field just before closing of the <form> tag. But this hidden field might not bet yet rendered to the browser with long-running pages, and if you make a postback before it "gets down", browser initiates postback without this field (in form post collection)
End result is that if this field is omitted on postback, Page doesn't "know" that viewstate is encrypted and causes the prementioned Exception. E.g Page expects to be fully-loaded before you can make a postback. And by the way similar problem is with event validation since __EVENTVALIDATION field is also rendered on the end of the form.
A way to overcome the problem is to set in web.config
-
<pages enableEventValidation="false" viewStateEncryptionMode ="Never" />
Just note the security implications of these!
Wednesday, May 14, 2008
Useful META TAGS Table of Contents
Note the keywords "HTTP-EQUIV", "Name" and "Content" are case-insensitive. Their values are also case-insensitive.
| Tag Name | Example(s) | Description |
|---|---|---|
| Author | <META NAME="AUTHOR" CONTENT="Tex Texin"> | The author's name. |
| cache-control | <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"> | HTTP 1.1. Allowed values = PUBLIC | PRIVATE | NO-CACHE | NO-STORE. Public - may be cached in public shared caches Private - may only be cached in private cache no-Cache - may not be cached no-Store - may be cached but not archived The directive CACHE-CONTROL:NO-CACHE indicates cached information should not be used and instead requests should be forwarded to the origin server. This directive has the same semantics as the PRAGMA:NO-CACHE. |
| Content-Language | <META HTTP-EQUIV="CONTENT-LANGUAGE" CONTENT="en-US,fr"> | Declares the primary natural language(s) of the document. May be used by search engines to categorize by language. |
| CONTENT-TYPE | <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=UTF-8"> | The HTTP content type may be extended to give the character set. It is recommended to always use this tag and to specify the charset. |
| Copyright | <META NAME="COPYRIGHT" CONTENT="© 2004 Tex Texin"> | A copyright statement. |
| DESCRIPTION | <META NAME="DESCRIPTION" CONTENT="...summary of web page..."> | The text can be used when printing a summary of the document. The text should not contain any formatting information. Used by some search engines to describe your document. Particularly important if your document has very little text, is a frameset, or has extensive scripts at the top. |
| EXPIRES | <META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 22 Jul 2002 11:12:01 GMT"> | The date and time after which the document should be considered expired. An illegal EXPIRES date, e.g. "0", is interpreted as "now". Setting EXPIRES to 0 may thus be used to force a modification check at each visit. Web robots may delete expired documents from a search engine, or schedule a revisit. HTTP 1.1 (RFC 2068) specifies that all HTTP date/time stamps MUST be generated in Greenwich Mean Time (GMT) and in RFC 1123 format. |
| Keywords | <META NAME="KEYWORDS" CONTENT="sex, drugs, rock & roll"> | The keywords are used by some search engines to index your document in addition to words from the title and document body. Typically used for synonyms and alternates of title words. Consider adding frequent misspellings. e.g. heirarchy, hierarchy. |
| PRAGMA NO-CACHE | <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"> | This directive indicates cached information should not be used and instead requests should be forwarded to the origin server. This directive has the same semantics as the CACHE-CONTROL:NO-CACHE directive and is provided for backwards compatibility with HTTP/1.0. Clients SHOULD include both PRAGMA:NO-CACHE and CACHE-CONTROL:NO-CACHE when a no-cache request is sent to a server not known to be HTTP/1.1 compliant. HTTP/1.1 clients SHOULD NOT send the PRAGMA request-header. HTTP/1.1 caches SHOULD treat "PRAGMA:NO-CACHE" as if the client had sent "CACHE-CONTROL:NO-CACHE". Also see EXPIRES. |
| Refresh | <META HTTP-EQUIV="REFRESH" CONTENT="15;URL=http://www.I18nGuy.com/index.html"> | Specifies a delay in seconds before the browser automatically reloads the document. Optionally, specifies an alternative URL to load, making this command useful for redirecting browsers to other pages. |
| ROBOTS | <META NAME="ROBOTS" CONTENT="ALL"> <META NAME="ROBOTS" CONTENT="INDEX,NOFOLLOW"> <META NAME="ROBOTS" CONTENT="NOINDEX,FOLLOW"> <META NAME="ROBOTS" CONTENT="NONE"> | CONTENT="ALL | NONE | NOINDEX | INDEX| NOFOLLOW | FOLLOW | NOARCHIVE" default = empty = "ALL" "NONE" = "NOINDEX, NOFOLLOW" The CONTENT field is a comma separated list: |
| GOOGLEBOT | <META NAME="GOOGLEBOT" CONTENT="NOARCHIVE"> | In addition to the ROBOTS META Command above, Google supports a GOOGLEBOT command. With it, you can tell Google that you do not want the page archived, but allow other search engines to do so. If you specify this command, Google will not save the page and the page will be unavailable via its cache. See Google's FAQ. |
