Another work-around to set maximum character length allowable to a textbox.



JavaScript Code:

function TextboxMultilineMaxNumber(txt, maxLen) {
            try {
                if (txt.value.length > (maxLen - 1)) {
                    alert("Maximum allowable characters reached.");
                    return false;
                }
            }
            catch (e) {
            }
        }

HTML(DOM) Code:

 <asp:TextBox ID="CourseName" runat="server" Width="615px" onkeypress="return TextboxMultilineMaxNumber(this,110)"></asp:TextBox>


L.
Another trick or work-around (i think) to make jQuery work right after UpdatePanel server request.

On my example code below, I have a textbox that has class name "datefield" that shows jQuery date picker (date calendar) when you click the textbox.


Javascript:

function load() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

function EndRequestHandler() {
 AsyncDone();
}


function AsyncDone() {
 $(".datefield").datepicker();
}


jQuery(document).ready(function ($) {
                        AsyncDone();
                    });




C# codebehind
ScriptManager.RegisterStartupScript(UpdateThis, typeof(UpdatePanel), "EndRequest", "load();", true);


L.
A SQL query statement you can use to delete all stored procedure in your database. This will permanently delete all the stored procedure scripts, so make you have the correct database and you back-up.

Use with caution.



SQL Query Statement:

USE DBNAME
GO

declare @procName sysname

declare someCursor cursor FOR
    SELECT name FROM sysobjects WHERE type = 'P' AND objectproperty(id, 'IsMSShipped') = 0

open someCursor
fetch next FROM someCursor INTO @procName
while @@FETCH_STATUS = 0
begin
    exec('drop proc ' + @procName)
    fetch next FROM someCursor INTO @procName
end

close someCursor
deallocate someCursor
go



L.
Below is a query statement you can use to delete all the tables of the database you selected/using. This will permanently delete all tables together with the schema and records of each table. 

Double check and make sure you are deleting from the correct database.
Use with caution. 


SQL Query Statement:

USE TESTDB
GO

DECLARE tables_cursor CURSOR

FOR SELECT name FROM sysobjects WHERE type = 'U'

OPEN tables_cursor
DECLARE @tablename sysname

FETCH NEXT FROM tables_cursor INTO @tablename
WHILE (@@FETCH_STATUS <> -1)
BEGIN
    EXEC ('DROP TABLE ' + @tablename)
    FETCH NEXT FROM tables_cursor INTO @tablename
END


DEALLOCATE tables_cursor

GO


L.
Another irritating (funny though) error message developers encountered while developing an ASP.NET application.

I've been caught by this error the first time and it haunted me or irritated me for few hours. Not sure what I did wrong it just happened. This usually happened to freshly deployed application to LIVE Environment if I remember it correctly.


ERROR MESSAGE:

Server Error in '/' Application.
--------------------------------------------------------------------------------

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.


SOLUTION:

In web.config, inside the , add the following line:

< pages enableEventValidation="false" viewStateEncryptionMode="Never" / >

L.
I am migrating a database from SQL Server 2005 to MS SQL Server 2012 and I came across this error message while running the application. (I am also upgrading the .NET Framework from 2.0 to 4.5 i know.. kind'a big jump ^_^.)

ERROR MESSAGE:
SQLException: Incorrect syntax near '*='.

I checked the stored procedure and found the culprit operator '*='. 
Obviously, I am not the initial developer of this application because I don't know what operator is that. After a minute of research, I found out that this is an operator for the LEFT JOIN and the RIGHT JOIN. But '*=' and '=*' operators syntax has been obsolete since 1992. (No wonder I haven't heard of it. I started coding around 2000.) Luckily it is still work in SQL Server 2005. 

Operator '*=' and '=*' is the LEFT JOIN and RIGHT JOIN.

SOLUTION:
So to fix this issue.. just replace to LEFT JOIN for '*=' and RIGHT JOIN for '=*'

EXAMPLE QUERY - BEFORE:

SELECT *
FROM tblPatient as A, tblInvoices as B
WHERE A.InvoiceId *= B.InvoiceId

EXAMPLE QUERY - AFTER:

SELECT *
FROM tblPatient as A LEFT JOIN tblInvoices as B 
ON A.InvoiceId = B.InvoiceId


L.


A simple way to convert or export data to a CSV format. Columns are separated by comma (a delimiter) and rows by each line. 

In my example below, I used StringBuilder for the content of my file, Environment.NewLine to move my cursor to next line. In the programming world, records can be pulled from any collections such as Dataset, LINQ and the likes. 

After the page has been executed, a .csv file will be created and a message will pop-up to ask if you want to save or open the file.


C# Code:

public void ExportCSV()
        {

            HttpContext context = HttpContext.Current;

            context.Response.Clear();
            context.Response.ContentType = "text/csv";
            context.Response.AddHeader("Content-Disposition", "attachment; filename=Result.csv");

            StringBuilder sbt = new StringBuilder();
            sbt.Append("Name,Age");
            sbt.Append(Environment.NewLine);
            sbt.Append("Anna,15");
            sbt.Append(Environment.NewLine);
            sbt.Append("Joe,12");

            context.Response.Write(csvresult);
            context.Response.End();

        }

protected void btnExport_Click(object sender, EventArgs e)
        {
           ExportCSV();
        }


L.