jueves, 24 de febrero de 2011

Almacenando objetos complejos usando localStorage

En principio solo es posible almacenar parejas clave valor, pero se puede serializar el objeto javascript en un texto JSON y almacenarlo como value.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Prueba LocalStorage</title>
        <script src="javascripts/jquery-1.5.min.js" type="text/javascript"></script>
  <script>
  
  var items = {'18': 4 , '17' : 44 , '16' : 84};
  
  $(document).ready(function() {
   $("#main").append("<div>Original items:" + JSON.stringify(items) + "</div");
   localStorage.setItem('varItems', JSON.stringify(items));
   var savedItems = localStorage.getItem('varItems');
   $("#main").append("<div>Saved items:" + savedItems + "</div>");
  });
  </script>
</head>
<body>
  <div id="main"></div>
</body>
</html>

viernes, 11 de febrero de 2011

Evento al cierre de browser en Visual WebGui

En Visual WebGui existe el evento ThreadSuspend que se ejecuta cuando cerras la ventana del browser, apretas F5 o navegas otra url.

public void RegisterThreadSuspendHandler()
{
  Application.ThreadSuspend += new EventHandler(Application_ThreadSuspend);
}

void Application_ThreadSuspend(object sender, EventArgs e)
{
  //Codigo para cierre de form
}

lunes, 7 de febrero de 2011

Clases para validación de superposición de rangos

Método que las utiliza:  
static bool RangosSuperpuestos(DataRow dr, string pHoraInicio, string pHoraFin)
{
     DateTime horaIniciodr = DateTime.Parse(dr["Inicio"].ToString());       
     DateTime horaFindr = DateTime.Parse(dr["Fin"].ToString());
     DateTime horaInicio = DateTime.Parse(pHoraInicio);
     DateTime horaFin = DateTime.Parse(pHoraFin);
     return Range.Overlap(new Range(horaIniciodr, horaFindr), new Range(horaInicio, horaFin));
}
Clases Auxiliares:
class Range
{
   public readonly T Start;
   public readonly T End;

   public Range(T start, T end)
   {
        Start = start;
        End = end;
   }
}

static class Range
{
   public static bool Overlap(Range left, Range right) where T : IComparable            
   {
      if (left.Start.CompareTo(right.Start) == 0)
      {
         return true;
      }
      else if (left.Start.CompareTo(right.Start) > 0)
      {
         return left.Start.CompareTo(right.End) <= 0;
      }
      else
      {
         return right.Start.CompareTo(left.End) <= 0;
      }
  }
}

miércoles, 2 de febrero de 2011

Metodo para grabar texto en un archivo usando C#

public void LogText(string text,bool logDate)
{
 string path = @"C:\texto.log";
 FileStream fs;
 if (!File.Exists(path))
   fs=File.Open(path,FileMode.Create,FileAccess.Write);
 else
   fs = File.Open(path, FileMode.Append, FileAccess.Write);
 using (var outfile =new StreamWriter(fs,Encoding.UTF8))
 {
   string logLine = string.Empty;
   if (logDate)
    logLine = String.Format("{0:G} - {1}.", System.DateTime.Now, text);
   else
    logLine = text;
   outfile.WriteLine(logLine);
 }
 fs.Close();
}

viernes, 14 de enero de 2011

Quitar texto entre < > en una cadena

public static string RemoveTags (string str)
{
   return Regex.Replace (str, "<.*?>", string.Empty);
}

sábado, 20 de noviembre de 2010

Obtener el tamaño de todas las tablas de una base de datos en SQL Server

DECLARE @TableName VARCHAR(100)    --For storing values in the cursor

--Cursor to get the name of all user tables from the sysobjects listing
DECLARE tableCursor CURSOR
FOR
select [name]
from dbo.sysobjects
where  OBJECTPROPERTY(id, N'IsUserTable') = 1
FOR READ ONLY

--A procedure level temp table to store the results
CREATE TABLE #TempTable
(
    tableName varchar(100),
    numberofRows varchar(100),
    reservedSize varchar(50),
    dataSize varchar(50),
    indexSize varchar(50),
    unusedSize varchar(50)
)

--Open the cursor
OPEN tableCursor

--Get the first table name from the cursor
FETCH NEXT FROM tableCursor INTO @TableName

--Loop until the cursor was not able to fetch
WHILE (@@Fetch_Status >= 0)
BEGIN
    --Dump the results of the sp_spaceused query to the temp table
    INSERT  #TempTable
        EXEC sp_spaceused @TableName

    --Get the next table name
    FETCH NEXT FROM tableCursor INTO @TableName
END

--Get rid of the cursor
CLOSE tableCursor
DEALLOCATE tableCursor

--Select all records so we can use the reults
SELECT *
FROM #TempTable
ORDER BY tableName ASC
--Final cleanup!
DROP TABLE #TempTable

GO

viernes, 19 de noviembre de 2010

Buscar en el contenido de un stored procedure

SELECT Name,OBJECT_DEFINITION(OBJECT_ID) AS Code
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%cadena_de_busqueda%'