lunes, 10 de diciembre de 2012

Redirecting LinkButton with blank tarjet



<asp:LinkButton ID="bPDF" runat="server" CausesValidation="False" CssClass="reset" Text="Ver Documento" OnClick="bPDF_Click" OnClientClick="aspnetForm.target ='_blank';"/>

protected void bPDF_Click(object senderEventArgs e)
{
    Response.Redirect("documento.pdf");
}

Validating values between TextBoxes using Javascript

<head runat="server">
    <title></title>
 
    <script type="text/javascript" language="javascript">
        function leave()
        {
            try
            {
                var val = document.getElementById('TextBox2').value;
                var compara = document.getElementById('TextBox1').value;
 
                if ((val > 0) && (compara > 0) && (compara > (val * 0.5)))
                {
                    document.getElementById('Label1').innerHTML = 'ERROR';
                    document.getElementById('Label2').innerHTML = 'ERROR';
                }
                else
                {
                    document.getElementById('Label1').innerHTML = '';
                    document.getElementById('Label2').innerHTML = '';
                }
            }
            catch (err)
            {
                //
            }
        }
    </script>
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"  OnBlur="leave();" />
        <asp:Label ID="Label2" runat="server" Text="Label" />
        <br />
        <asp:TextBox ID="TextBox2" runat="server" OnBlur="leave();" />
        <asp:Label ID="Label1" runat="server" Text="Label" />
    </div>
    </form>
</body>
</html>

Setting value TextBox using Javascript

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 
    <script type="text/javascript" lang="javascript">
        function setValue(TextBox)
        {
            TextBox.value = '0';
        }
    </script>
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"  OnBlur="setValue(this);" />
    </div>
    </form>
</body>
</html>

martes, 23 de octubre de 2012

Send mail in SQL

--send mail using sql agent

exec sp_configure 'show advanced option', '1'
reconfigure
exec sp_configure 'SQL Mail XPs', 1
reconfigure

exec sys.xp_startmail
exec master.sys.xp_sendmail @recipients=N'tucorreo@gmail.com', @message=N'xp_sendmail test.'
exec sys.xp_stopmail

--run exe

exec sp_configure 'show advanced option', '1'
reconfigure
exec sp_configure 'xp_cmdshell', 1
reconfigure
exec sp_configure 'show advanced option', '0'
reconfigure

EXEC xp_cmdshell 'c:\MailEngine.exe'

martes, 11 de septiembre de 2012

Send excel file to save locally using ASP.NET

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=reporte.csv");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/excel";
Response.TransmitFile(Server.MapPath("~/exportado/reporte.csv"));
Response.End();

Creating MessageBox using ASP.NET

private void ShowPopUpMsg(string msg)
{
StringBuilder sb = new StringBuilder();
sb.Append("alert('");
sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
sb.Append("');");
ClientScript.RegisterStartupScript(this.GetType(), "Aviso", sb.ToString(), true);
}

jueves, 7 de junio de 2012

Cursor example in SQL

DECLARE @vNoSerie AS VARCHAR(15) DECLARE @vPrecio AS DECIMAL(10,2) DECLARE vCursor CURSOR FOR SELECT NoSerie, Precio FROM Ventas OPEN vCursor FETCH NEXT FROM vCursor INTO @vNoSerie, @vPrecio WHILE @@fetch_status = 0 BEGIN DECLARE @v VARCHAR(25) SET @v = (SELECT Estado FROM Movimientos WHERE NoSerie = @vNoSerie AND Importe = @vPrecio) --Visual Trace PRINT @vNoSerie + ' - ' + CONVERT(VARCHAR,@vPrecio) + ' - ' + @v IF (@v = 'DISPONIBLE') UPDATE Ventas SET Vendido = 1 WHERE NoSerie = @vNoSerie AND Precio = @vPrecio FETCH NEXT FROM vCursor INTO @vNoSerie, @vPrecio END CLOSE vCursor DEALLOCATE vCursor

viernes, 18 de mayo de 2012

Dates calculation in SQL

SELECT GETDATE() AS NOW, GETDATE()-(1) AS YESTERDAY, GETDATE()-(0.04166666) AS PREVIOUS_HOUR, GETDATE()-(0.0006944444) AS PREVIOUS_MINUTE, GETDATE()-(0.000011574) AS PREVIOUS_SECOND

miércoles, 1 de febrero de 2012

Convert message to morce code

Dictionary lst = new Dictionary();

private void Form1_Load(object sender, EventArgs e)
{
lst.Add("A", "11");
lst.Add("B", "12");
lst.Add("C", "13");
lst.Add("D", "14");
lst.Add("E", "15");
lst.Add("F", "21");
lst.Add("G", "22");
lst.Add("H", "23");
lst.Add("I", "24");
lst.Add("J", "25");
lst.Add("K", "31");
lst.Add("L", "32");
lst.Add("M", "33");
lst.Add("N", "34");
lst.Add("O", "35");
lst.Add("P", "41");
lst.Add("Q", "42");
lst.Add("R", "43");
lst.Add("S", "44");
lst.Add("T", "45");
lst.Add("U", "51");
lst.Add("V", "52");
lst.Add("W", "53");
lst.Add("X", "54");
lst.Add("Y", "55");
lst.Add("Z", "61");
lst.Add("1", "62");
lst.Add("2", "63");
lst.Add("3", "64");
lst.Add("4", "65");
lst.Add("5", "66");
lst.Add("6", "71");
lst.Add("7", "72");
lst.Add("8", "73");
lst.Add("9", "74");
lst.Add("0", "75");
lst.Add(".", "77");
lst.Add(",", "85");
lst.Add("-", "86");
lst.Add("?", "87");
lst.Add("!", "88");
lst.Add("_", "95");
lst.Add("(", "96");
lst.Add(")", "97");
lst.Add("Ñ", "98");
lst.Add(" ", "99");
////codigo similar al morce
//lst.Add("A", "-----·");
//lst.Add("B", "----·-");
//lst.Add("C", "----··");
//lst.Add("D", "---·--");
//lst.Add("E", "---·-·");
//lst.Add("F", "---··-");
//lst.Add("G", "---···");
//lst.Add("H", "--·---");
//lst.Add("I", "--·--·");
//lst.Add("J", "--·-·-");
//lst.Add("K", "--·-··");
//lst.Add("L", "--··--");
//lst.Add("M", "--··-·");
//lst.Add("N", "--···-");
//lst.Add("O", "--····");
//lst.Add("P", "-·----");
//lst.Add("Q", "-·---·");
//lst.Add("R", "-·--·-");
//lst.Add("S", "-·--··");
//lst.Add("T", "-·-·--");
//lst.Add("U", "-·-·-·");
//lst.Add("V", "-·-··-");
//lst.Add("W", "-·-···");
//lst.Add("X", "-··---");
//lst.Add("Y", "-··--·");
//lst.Add("Z", "-··-·-");
//lst.Add("1", "-··-··");
//lst.Add("2", "-···--");
//lst.Add("3", "-···-·");
//lst.Add("4", "-····-");
//lst.Add("5", "-·····");
//lst.Add("6", "·-----");
//lst.Add("7", "·----·");
//lst.Add("8", "·---·-");
//lst.Add("9", "·---··");
//lst.Add("0", "·--·--");
//lst.Add(".", "·--·-·");
//lst.Add(",", "·--··-");
//lst.Add("-", "·--···");
//lst.Add("?", "·-·--·");
//lst.Add("!", "·-·--·");
//lst.Add("_", "·-·-·-");
//lst.Add("(", "·-·-··");
//lst.Add(")", "·-··--");
//lst.Add("Ñ", "·-··-·");
//lst.Add(" ", "------");
}


private void btConvert_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.ToUpper();
string s = textBox1.Text;
string t = string.Empty;

for (int i = 0; i <= s.Length - 1; i++)
{
if (lst.ContainsKey(s[i].ToString()))
{
t += lst.First(p => p.Key == s[i].ToString()).Value.ToString();
}
}

textBox1.Text = t;
}

private void btUnConvert_Click(object sender, EventArgs e)
{
string s = textBox1.Text;
string t = string.Empty;
int wordlen = lst.FirstOrDefault().Value.Length;

for (int i = 0; i <= (s.Length/wordlen) - 1; i++)
{
if (lst.ContainsValue(s.Substring(i * wordlen, wordlen)))
{
t += lst.First(p => p.Value == s.Substring(i * wordlen, wordlen)).Key.ToString();
}
}

textBox1.Text = t;
}

lunes, 30 de enero de 2012

Export database to excel using MS Excel

//Add Reference (COM) Microsoft Office List 12.0
//Add Reference (.NET) Microsoft.Office.Interop.Excel 12.0
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflexion;
using System.IO;
using System.Diagnostics;
using System.Data.OleDb;

try
{
Excel.Application xlsApp = new Excel.Application(); //Create XLS
xlsApp.DisplayAlerts = false; //Block Alerts
Excel.Worksheet xlsSheet; //Declare Sheet
Excel.Workbook xlsBook; //Declare Workbook
xlsApp.Visible = false; //Turn off the aplicación
xlsBook = xlsApp.Workbooks.Add(true); //Add Workbook
xlsSheet = (Excel.Worksheet)xlsBook.ActiveSheet; //Add Sheet

//TABLE HEADER
for (irow = 0; irow < 1; irow++)
{
for (icol = 1; icol < Lista.Tables[0].Columns.Count; icol++)
{
xlsSheet.Cells[1, icol] = Lista.Tables[0].Columns[icol].ColumnName;
MessageBox.Show(Lista.Tables[0].Rows[irow].ItemArray[icol].ToString());
}
}


//TABLE ROWS
for (irow = 1; irow < Lista.Tables[0].Rows.Count; irow++)
{
for (icol = 1; icol < Lista.Tables[0].Columns.Count; icol++)
{
xlsSheet.Cells[irow + 1, icol] = Lista.Tables[0].Rows[irow - 1].ItemArray[icol - 1].ToString();
MessageBox.Show(Lista.Tables[0].Rows[irow].ItemArray[icol].ToString());
}
}

xlsSheet.Columns.AutoFit(); //Resize columns
xlsApp.Visible = true;
try
{
xlsApp.SaveWorkspace(); //Save XLS
}
catch
{ }
xlsApp.Quit();

MessageBox.Show("Insert Terminado");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

Export database to excel without MS Excel

try
{
if (File.Exists(@"c:\exportado.xls"))
File.Delete(@"c:\exportado.xls");

using(System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\exportado.xls';Extended Properties='Excel 8.0;HDR=Yes'"))
using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("", con))
{
cmd.Connection = con;
con.Open();

string sql = "CREATE TABLE [Precios] ( columna1 VARCHAR(100) )";
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();

//sql = "UPDATE [Precios$A1:A1] SET columna1 = '12345678'";
sql = "INSERT INTO [Precios] (columna1) VALUES('12345678')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

con.Close();
}

MessageBox.Show("Insert Terminado");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

lunes, 9 de enero de 2012

Creating temporal table, inserting and pivoting

--CREATING TEMPORAL TABLE
DECLARE @Tabla TABLE (
FName VARCHAR(25),
School VARCHAR(15),
Team VARCHAR(15),
Points INT
)

--INSERTING VALUES
INSERT INTO @Tabla VALUES ('Pablo','OLOF','MEN',5)
INSERT INTO @Tabla VALUES ('Pedro','OLOF','MEN',5)
INSERT INTO @Tabla VALUES ('Ruby','OLOF','WOMEN',5)
INSERT INTO @Tabla VALUES ('Ruby','OLOF','WOMEN',5)
INSERT INTO @Tabla VALUES ('Paco','AMAT','WOMEN',5)

--VIEW
SELECT * FROM @Tabla

--PIVOTING VALUES
SELECT School,[MEN],[WOMEN]
FROM (SELECT School,Team,Points FROM @Tabla) As T
PIVOT (SUM(Points)
FOR Team IN ([MEN],[WOMEN])) As PivotT

SELECT FName As FullName,School,[MEN],[WOMEN]
FROM (SELECT Fname,School,Team,Points FROM @Tabla) As T
PIVOT (SUM(Points)
FOR Team IN ([MEN],[WOMEN])) AS PivotT