Thursday, April 5, 2012

convert image to base64


For this u need to download Html agility


protected void Page_Load(object sender, EventArgs e)
{
HtmlWeb htmlWeb = new HtmlWeb();
HtmlAgilityPack.HtmlDocument document = htmlWeb.Load("http://www.fortunehotels.in/");
foreach (HtmlNode someNode in document.DocumentNode.SelectNodes("//img[@src]"))
{
if (someNode.Attributes.Contains("src"))
{
if (someNode.Attributes["src"].Value.EndsWith(".jpg"))
{
if (someNode.Attributes["src"].Value != "admin/UploadImage/summer-packages2010717138.jpg")
{
if (someNode.Attributes["src"].Value != "admin/UploadImage/101X108201243356.jpg")
{
WebClient wc = new WebClient();
byte[] data = wc.DownloadData(someNode.Attributes["src"].Value);
string base64ImageString = ConvertBytesToBase64(data);
someNode.Attributes["src"].Value = "data:image/jpg;base64," + base64ImageString;
myImage.Src = "data:image/jpg;base64,"+base64ImageString;
}
document.Save("C:/Users/sandeep_2/Desktop/fortune.htm");
}
}
}
}
}
public string ConvertBytesToBase64(byte[] imageBytes)
{
return Convert.ToBase64String(imageBytes);
}

Wednesday, April 4, 2012

Calculate age in sql

DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days int SELECT @date = '09/03/1985'
--date in mm/dd/yy
SELECT @tmpdate = @date SELECT @years = DATEDIFF(yy, @tmpdate, GETDATE()) - CASE WHEN MONTH(@date) > MONTH(GETDATE()) THEN 1 ELSE 0 END SELECT @tmpdate = DATEADD(yy, @years, @tmpdate) SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) - CASE WHEN DAY(@date) > DAY(GETDATE()) THEN 1 ELSE 0 END SELECT @tmpdate = DATEADD(m, @months, @tmpdate) SELECT @days = DATEDIFF(d, @tmpdate, GETDATE()) SELECT @years AS Years, @months AS Months, @days as Days

Tuesday, April 3, 2012

Read Excel

Define File name in the appsetting



<appSettings>
<add key="class" value="server=.;database=Hcl;uid=sa;pwd=sa123"/>
<add key="filename" value="order.xlsx"/>
<add key="filecity" value="City.xls"/>
</appSettings>

Call the method on button click



protected void btnsearch_Click(object sender, EventArgs e)
{
string location = Server.MapPath("~/Excel_file/") + ConfigurationManager.AppSettings["filename"].ToString().Trim();
GetExcelData(location);
}

Define Method



public DataTable GetExcelData(string location)
{
OledbConnectionString = string.Empty;
string ext = Path.GetExtension(ConfigurationManager.AppSettings["filename"].ToString().Trim());
if (ext == ".xls")
{
OledbConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + location + ";Extended Properties=Excel 8.0;";
}
else if (ext == ".xlsx")
{
OledbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + location + ";Extended Properties=Excel 12.0;";
}
else
{ }
OleDbConnection objConn = null;
objConn = new OleDbConnection(OledbConnectionString);
if (objConn.State == ConnectionState.Closed)
{
objConn.Open();
}

string query = "Select * From [Order Sheet$] Where rtrim(ltrim([Order No#]))=" + txtorderid.Text.ToString().Trim() + " and rtrim(ltrim([Email Id]))='" + txtemail.Text.Replace("'", " ").Replace("-", " ").ToString().Trim() + "' and rtrim(ltrim([Delivery Name]))='" + txtCname.Text.Replace("'", " ").Replace("-", " ").ToString().Trim() + "'";
OleDbCommand objCmdSelect = new OleDbCommand(query, objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
DataSet objDataset = new DataSet();
objAdapter.Fill(objDataset);
box.Visible = true;

try
{
if (objDataset.Tables[0] != null)
{
if (objDataset.Tables[0].Rows.Count == 0)
{
viewdetail.Visible = false;
GridView1.DataSource = objDataset.Tables[0];
GridView1.DataBind();
txtCname.Text = "";
txtemail.Text = "";
txtorderid.Text = "";
}
else
{
viewdetail.Visible = true;
GridView1.DataSource = objDataset.Tables[0];
GridView1.DataBind();
txtCname.Text = "";
txtemail.Text = "";
txtorderid.Text = "";
}

}
else
{
viewdetail.Visible = false;
}
}
catch (Exception ex)
{

}
finally
{
objCmdSelect.Dispose();
objConn.Close();
objConn.Dispose();

}
// objConn.Close();
return objDataset.Tables[0];

}

Friday, March 16, 2012

Encrypt and Decrypt Web.Config

Security is one of the major aspects in terms of a web application. Security can be implemented using different methods in a web application. One of the main components, which required high security, is the configuration files, where we will define the configurations. In this document, we will discuss about the simple way to protect the web.config entries.


Security is one of the major aspects in terms of a web application. Security can be implemented using different methods in a web application. One of the main components, which required high security, is the configuration files, where we will define the configurations. In this document, we will discuss about the simple way to protect the web.config entries.

Encrypt Web.Config Sections

We can encrypt the required sections using the Asp.Net built in utility aspnet_regiis. For example, we want to encrypt the following appSettings defined in the default website

Open the command prompt and run the command




%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pe "appSettings"


Rsa Key


g7ROtz+5zGkEXsfxBW1o7RmcGvekuEtbvHGC+EWQiqD0fJypbg56JgbO+7hLMYh7uq5J5AcNEa9XAxnsX2f7V5QJZLDguv+bJhdBHs27QrS5P0MNU4bqUipx0MYwuS+zPTkBwpyTuTzCBTJPZkkfd/4N0IvK9+JgQP9EUK9PlAA=



oSn8uYA9HUaii3ZcHET4oMATfVrFwpYpjipRgF9Uil+eSwno9r3HqNjS1VYwkEz8QFfhHnhSenwkKpIkuhv1hFvppX1CZtQyopVIDtsJgnzdP7uCk9xYt+7n6EX3OlgVuHCv8ckl7jpCAaTgtFLmScM6ey8N4SAkwK60e/Ou7zg4bqf0RnNYcghprdxuBR2z

Sometimes, we need to encrypt the sections of another website, which is not the default website, and then use the following options


%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pe "appSettings" -app "/" -site TestSite


This command specifies encrypt the appSettings section of the site TestSite. –app indicates the virtual directory under the site. For encrypting the web.config of the main web site use the virtual directory value as “/”.

In some cases, we may need to encrypt the web.config under a virtual directory or web application under our web site, then use the following command


%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pe "appSettings" -app "/MyApp" -site TestSite


Here, the command indicates encrypt the web.config of the TestSite and find the configuration under the MyApp web application configured under the website.

Instead of using the site name, we can use the metadata identifier or ID of the site, which can be obtained from the IIS Manager.


%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pe "appSettings" -app "/MyApp" -site 5

Decrypt Web.Config Sections
Same as encrypt, we can us ethe same utility to decrypt the section values for further updates. The decrypt command is


%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pd "appSettings" -app "/MyApp"
-site TestSite

SELECT + UPDATE IN ONE STATAEMENT

update visitor_count set COLUMN_NAME= (COLUMN_NAME + 1)