Pages

Dec 8, 2011

How to set html link visible false depending on Eval function in grid view

Below is the html link in the grid view item template that sets visibility depending on the IsOnline values.

<a href = "www.google.com" style='<%# Eval("IsOnline").ToString() == "True" ? "display:block" : "display:none" %>' >link</a>

Dec 7, 2011

How to convert DateTime to Date in Linq to Entity + C#

Use EntityFunction to truncate the time part from a DateTime String.

grid.DataSource = (from user in context.Users where ISActive = True select new
                                          {
                                              user.UserId,
                                              user.UserName,
                                              FirstName = (user.FirstName + " " + user.LastName),                                           
                                              CreatedDate = System.Data.Objects.EntityFunctions.TruncateTime(user.CreatedDate)                                           
                                          }).ToList();)

Jun 19, 2011

Clipboard operations in javascript


Copy TO and Copy FROM CLIPBOARD :


<body>
    <textarea id='clipText'>
Enter Text And Click Button To Copy Text To ClipBoard</textarea><br />
    <input type="button" id='bt' onclick="clipboardData.setData('Text',document.getElementById('clipText').value);"
        value="Copy" />
    <input type="button" onclick="clipboardData.clearData('Text');" value="Clear" />
    <input type="button" onclick="alert(clipboardData.getData('Text'));" value="Paste" />
</body>

May 26, 2011

How to insert a Guid or UniqueIdentifier and return it as a OUTPUT parameter + StoredProcedure. + SQL


CREATE PROCEDURE [test]
    @name as varchar(50),
    @id as uniqueidentifier OUTPUTAS
BEGIN
    declare @returnid table (id uniqueidentifier)

    INSERT INTO test(
        name
    )
    output inserted.id into @returnid
    VALUES(
        @name
    )

    select @id = r.id from @returnid rEND
Above SP returns the newly inserted GUID as the OUT parameter.

May 20, 2011

How to use double coute in string + C#

This usage of double coutes in a string can be easily done using @.
For eg:
To show the below text 
 "This is a "Test"


Code:
TextBox1.text = @"""This is a " + @"""Test""";


The logic is that if you use @ in front of the sting variable then using e double coute(""") 
throughout the string will give you one double coute(").

How to create a text file and save it on client machine using Save File Dialog + Asp.Net + C#

Method to Create text file:
Using System.IO;
Using SystemsText;

private void CreatetxtFile()
{
StringBuilder sb = new StringBuilder();

sb.AppendLine("test");
sb.AppendLine("= = = = = =");
sb.AppendLine();
sb.Append("test2");
sb.AppendLine();
sb.AppendLine();

using (StreamWriter outfile = new StreamWriter("c:" + @"\AllTxtFiles.txt"))
{
outfile.Write(sb.ToString());
}

}
Code to Save the file as dialog :
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=AllTxtFiles.txt");
// specify the path of the file that has to be saved in client machine.
Response.TransmitFile("c://AllTxtFiles.txt");
Response.End();
The above code will show a save file dialog to save the file.

Apr 25, 2011

How to update data from excel sheet to the sql data base. + How to compare two date time columns and find the later values

// Copies the excel sheet values to the table sTemp.
SELECT * INTO dbo.sTemp
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\test.xls;IMEX=1',
'SELECT * FROM [Sheet_Name2$]')

GO

// Compares two columns of the excel and updates the table with the later values.
UPDATE INV
SET
INV.EndDate = TMP.
RecentDate 
FROM
dbo.Inventory as INV
INNER JOIN
(
SELECT
Inventoryid,
CASE
WHEN ISNULL([WEDate],'') >= ISNULL([MEDate],'')
THEN
[WEDate]
ELSE
[MEDate]
END
AS MostRecentDate,
[WEDate],
[MEDate]
FROM sTemp
) TMP
ON
TMP.TestID = INV.TestID

GO

DROP TAble stemp

GO