Monday, August 31, 2009

jQuery - Center a div or any control

Here is a short code to centralize your div or any html control using Jquery..
Dependency could be jquery-1.3.2.js

$('#divid').css('top',$(window).height()/2 - $('#divid').height()/2 + $(window).scrollTop());
$('#divid').css('left',$(window).width()/2 - $('#divid').width()/2 + $(window).scrollLeft());

You can use animate to get some effect to the moving operation.

$('#divid').animate({top: $(window).height()/2 - $('#divid').height()/2 + $(window).scrollTop()},500);
$('#divid').
animate({left: $(window).width()/2 - $('#divid').width()/2 + $(window).scrollLeft()},500);

Friday, August 28, 2009

Read - Write SMTP settings Web.Config with Asp.net runtime

//Read SMTP Settings
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
txtSmtpHostAddr.Text = settings.Smtp.Network.Host;
txtSmtpUserName.Text = settings.Smtp.Network.UserName;
txtSmtpPortNo.Text = settings.Smtp.Network.Port.ToString();
txtSmtpEmail.Text = settings.Smtp.From;

//Write SMTP Settings
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
settings.Smtp.Network.Host = txtSmtpHostAddr.Text.Trim();
settings.Smtp.Network.UserName = txtSmtpUserName.Text.Trim();
settings.Smtp.Network.Password = txtSmtpPassword.Text.Trim();
settings.Smtp.Network.Port = txtSmtpPortNo.Text == "" ? 25 : Convert.ToInt32(txtSmtpPortNo.Text.Trim());
settings.Smtp.From = txtSmtpEmail.Text.Trim();
config.Save();

Thursday, August 27, 2009

Language Translation API ASP.NET

private string translateme(string stringToTranslate, string fromLanguage, string toLanguage)

{

const int bufSizeMax = 65536;

const int bufSizeMin = 8192;

try

{

// by default format? is text.

// so we don't need to send a format? key

string requestUri = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + stringToTranslate + "&langpair=" + fromLanguage + "%7C" + toLanguage;

// execute the request and get the response stream

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();

// get the length of the content returned by the request

int length = (int)response.ContentLength;

int bufSize = bufSizeMin;

if (length > bufSize)

bufSize = length > bufSizeMax ? bufSizeMax : length;

// allocate buffer and StringBuilder for reading response

byte[] buf = new byte[bufSize];

StringBuilder sb = new StringBuilder(bufSize);

// read the whole response

while ((length = responseStream.Read(buf, 0, buf.Length)) != 0)

{

sb.Append(Encoding.UTF8.GetString(buf, 0, length));

}

// the format of the response is like this

// {"responseData": {"translatedText":"¿Cómo estás?"}, "responseDetails": null, "responseStatus": 200}

// so now let's clean up the response by manipulating the string

string translatedText = sb.Remove(0, 36).ToString();

return translatedText.Substring(0, translatedText.IndexOf("\"},"));

}

catch

{

return "";

}

}

Wednesday, August 26, 2009

Read,Write,Edit Resource File Asp.Net C#

//READ RESOURCE

ResXResourceReader reader = new ResXResourceReader(Server.MapPath("~/App_GlobalResources/test.resx"));

IDictionaryEnumerator ide = reader.GetEnumerator();

int i = 0;

//COUNTING FOR THE RESOURCE KEYS

while (ide.MoveNext())

{

i++;

}

//ASSIGN THE DICTIONARY ENTRY ARRAY FOR STORING KEYS

DictionaryEntry[] de = new DictionaryEntry[i];

i = 0;

//RESET DICTIONARY TO INITIAL POSITION

ide.Reset();

while (ide.MoveNext())

{

de[i] = ide.Entry;

Response.Write(ide.Key + " - " + ide.Value);

}

reader.Close();

//WRITE RESOURCE

ResXResourceWriter writer = new ResXResourceWriter(Server.MapPath("~/App_GlobalResources/test.resx"));

foreach (DictionaryEntry del in de)

{

//YOUR CODE HERE IF YOU WANT TO CHANGE VALUES

writer.AddResource(del.Key.ToString(), del.Value.ToString() + "*");

}

//SAVE CHANGES

writer.Generate();

//CLOSE RESOURCE FILE

writer.Close();

//DESPOSE RESOURCE

writer.Dispose();

Tuesday, August 11, 2009

Drop Everything in SQL Server 2005 Database

/* Drop all non-system stored procs */

DECLARE @name VARCHAR(128)

DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 ORDER BY [name])

WHILE @name is not null

BEGIN

SELECT @SQL = 'DROP PROCEDURE [' + RTRIM(@name) +']'

EXEC (@SQL)

PRINT 'Dropped Procedure: ' + @name

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 AND [name] > @name ORDER BY [name])

END

GO

/* Drop all views */

DECLARE @name VARCHAR(128)

DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL

BEGIN

SELECT @SQL = 'DROP VIEW [' + RTRIM(@name) +']'

EXEC (@SQL)

PRINT 'Dropped View: ' + @name

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 AND [name] > @name ORDER BY [name])

END

GO

/* Drop all functions */

DECLARE @name VARCHAR(128)

DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL

BEGIN

SELECT @SQL = 'DROP FUNCTION [' + RTRIM(@name) +']'

EXEC (@SQL)

PRINT 'Dropped Function: ' + @name

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 AND [name] > @name ORDER BY [name])

END

GO

/* Drop all Foreign Key constraints */

DECLARE @name VARCHAR(128)

DECLARE @constraint VARCHAR(254)

DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)

WHILE @name is not null

BEGIN

SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)

WHILE @constraint IS NOT NULL

BEGIN

SELECT @SQL = 'ALTER TABLE [' + RTRIM(@name) +'] DROP CONSTRAINT ' + RTRIM(@constraint)

EXEC (@SQL)

PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name

SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)

END

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)

END

GO

/* Drop all Primary Key constraints */

DECLARE @name VARCHAR(128)

DECLARE @constraint VARCHAR(254)

DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)

WHILE @name IS NOT NULL

BEGIN

SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)

WHILE @constraint is not null

BEGIN

SELECT @SQL = 'ALTER TABLE [' + RTRIM(@name) +'] DROP CONSTRAINT ' + RTRIM(@constraint)

EXEC (@SQL)

PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name

SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)

END

SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)

END

GO

/* Drop all tables */

DECLARE @name VARCHAR(128)

DECLARE @SQL VARCHAR(254)

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 ORDER BY [name])

WHILE @name IS NOT NULL

BEGIN

SELECT @SQL = 'DROP TABLE [' + RTRIM(@name) +']'

EXEC (@SQL)

PRINT 'Dropped Table: ' + @name

SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name])

END

GO