Wednesday, February 3, 2010

Upload Image in Database and Display Image

upload images.. first i created a database table with the following columns...

Id --> int (identity column, and primary key)

firstname -->varchar(50)

lastname -->varchar(50)

image -->varchar(50)

and under my web site root directory i have created a folder with name..(images)

here is the code.. to upload the image....



using System.IO;
using System.Data.SqlClient;
using System.Web.Configuration;

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileExt = Path.GetExtension(FileUpload1.FileName).ToLower();
string fileName = Path.GetFileName(FileUpload1.FileName);
string dbfilePath = @"~/images/" + fileName;
if (fileName != string.Empty)
{
try
{
if (fileExt == ".jpg" || fileExt == ".gif")
{
FileUpload1.SaveAs(Server.MapPath(@"~/images/") + fileName);
}
else
{
Response.Write("You can upload only JPG or GIF files...");
}
}
catch (Exception ex)
{
throw ex;
}
}
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("insert into images values(@firstname,@lastname,@image)",con);
cmd.Parameters.AddWithValue("@firstname",TextBox1.Text);
cmd.Parameters.AddWithValue("@lastname",TextBox2.Text);
cmd.Parameters.AddWithValue("@image",dbfilePath);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
Response.Write("Uploaded");
}
}

}


Display Image in Repeater

his is how you can display the images in Repeater control....

Html...









CodeBehind...
using System.Data.SqlClient;
using System.Web.Configuration;

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from images",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Repeater1.DataSource = dt;
Repeater1.DataBind();
}

Regard
Prateek

Install Silver Light

Hi,

Mostly many person not know how to install silverlight so,follow this steps:
1 Microsoft visual studio 2008 (SP1)
2 Silverlight tool.exe
3 Microsoft Expression Blend 2.0
4 Microsoft Expression Blend 2.0 (SP1)
5 Silverlight tool kit 2 March

Regard
Prateek

Wednesday, December 30, 2009

Delete Type URL from AddressBar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Windows.Forms;
using System.IO;

[assembly: RegistryPermission(SecurityAction.RequestMinimum, ViewAndModify = "HKEY_CURRENT_USER\\SOFTWARE\\MICROSOFT\\INTERNET EXPLORER\\TYPEDURLS")]
[assembly: RegistryPermission(SecurityAction.RequestMinimum, ViewAndModify = "HKEY_CURRENT_USER\\SOFTWARE\\MICROSOFT\\INTERNET EXPLORER")]

then declare below static constant string varible:

public static SortedList historylinks = new SortedList();
public const string REG_URL = "SOFTWARE\\MICROSOFT\\INTERNET EXPLORER\\TYPEDURLS";
public const string REG_URL_SHORT = "SOFTWARE\\MICROSOFT\\INTERNET EXPLORER";

then here is the code for:

GetHistory();
int deletedhist = 0;
using (RegistryKey tempKey = Registry.CurrentUser.OpenSubKey(REG_URL, true))
{
if (tempKey == null)
{
System.Windows.Forms.MessageBox.Show("The registry entry for " + REG_URL + " does not exist!",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} // if
else
{
foreach (histlink hl in historylinks.Values)
{
tempKey.DeleteValue(hl.URL);
deletedhist++;
}
}
}
GetHistory();
lbl_message.Text = "Typed URL's cleared";

and here is the code for gethistory function:

public void GetHistory()
{
historylinks.Clear();
using (RegistryKey tempKey = Registry.CurrentUser.OpenSubKey(REG_URL))
{
if (tempKey == null)
{

using (RegistryKey tempKeyShort = Registry.CurrentUser.OpenSubKey(REG_URL_SHORT, true))
{
if (tempKeyShort == null)
{
MessageBox.Show("The registry entry for " + REG_URL_SHORT + " does not exist!",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
tempKeyShort.CreateSubKey("TypedURLs", RegistryKeyPermissionCheck.ReadWriteSubTree);
}
}
}
}

using (RegistryKey tempKey = Registry.CurrentUser.OpenSubKey(REG_URL))
{
if (tempKey == null)
{
System.Windows.Forms.MessageBox.Show("The registry entry for " + REG_URL + " does not exist!",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

string[] vals = tempKey.GetValueNames();
int itemcounter = 0;
foreach (string url in vals)
{
object keyValue = tempKey.GetValue(url);
if (keyValue != null)
{
histlink hl = new histlink();
string entry = keyValue.ToString();
if (entry.Length != 0)
{
itemcounter++;
hl.Entry = entry;
hl.URL = url;
historylinks.Add(itemcounter, hl);
}
}
}
}

Deleting historyfile

In this code the header files and assembly files are same as Delete Cache which i post early

and the code is:

//textBox is used for deleting particular history file

string enteredtext = txt_url.Text.Trim();
string lowertext = txt_url.Text.ToLower();
string uppertext = txt_url.Text.ToUpper();

string[] historyfile = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.History), "*", SearchOption.AllDirectories);
for (int i = 0; i < historyfile.Count(); i++)
{
try
{
File.Delete(historyfile.ElementAt(i));
lbl_message.Text = historyfile.ElementAt(i)+ " cleared";


}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}


//this is the function which I used :

public void GetHistory()
{
historylinks.Clear();
using (RegistryKey tempKey = Registry.CurrentUser.OpenSubKey(REG_URL))
{
if (tempKey == null)
{

using (RegistryKey tempKeyShort = Registry.CurrentUser.OpenSubKey(REG_URL_SHORT, true))
{
if (tempKeyShort == null)
{
MessageBox.Show("The registry entry for " + REG_URL_SHORT + " does not exist!",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
tempKeyShort.CreateSubKey("TypedURLs", RegistryKeyPermissionCheck.ReadWriteSubTree);
}
}
}
}

using (RegistryKey tempKey = Registry.CurrentUser.OpenSubKey(REG_URL))
{
if (tempKey == null)
{
System.Windows.Forms.MessageBox.Show("The registry entry for " + REG_URL + " does not exist!",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

string[] vals = tempKey.GetValueNames();
int itemcounter = 0;
foreach (string url in vals)
{
object keyValue = tempKey.GetValue(url);
if (keyValue != null)
{
histlink hl = new histlink();
string entry = keyValue.ToString();
if (entry.Length != 0)
{
itemcounter++;
hl.Entry = entry;
hl.URL = url;
historylinks.Add(itemcounter, hl);
}
}
}
}

Delete Temp File from Browser

In this code the header files and assembly files are same as Delete Cache which i post early

and the code is:

//textBox is used for deleting particular Temp Files

string enteredtext = txt_url.Text.Trim();
string lowertext = txt_url.Text.ToLower();
string uppertext = txt_url.Text.ToUpper();

string[] tempfiles = Directory.GetFiles(Environment.GetEnvironmentVariable("TEMP"), "*", SearchOption.AllDirectories);
for (int i = 0; i < tempfiles.Count(); i++)
{
try
{
if (tempfiles.ElementAt(i).Contains(enteredtext) || tempfiles.ElementAt(i).Contains(lowertext) || tempfiles.ElementAt(i).Contains(uppertext))
{
File.Delete(tempfiles.ElementAt(i));
lbl_message.Text = "TEMP file cleared";
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Delete Cookies

In this code the header files and assembly files are same as Delete Cache which i post early

and the code is:

//textBox is used for deleting particular Cookies

string enteredtext = txt_url.Text.Trim();
string lowertext = txt_url.Text.ToLower();
string uppertext = txt_url.Text.ToUpper();

string[] cookiefile = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies), "*", SearchOption.AllDirectories);
for (int i = 0; i < cookiefile.Count(); i++)
{
try
{
if (cookiefile.ElementAt(i).Contains(enteredtext) || cookiefile.ElementAt(i).Contains(lowertext) || cookiefile.ElementAt(i).Contains(uppertext))
{
File.Delete(cookiefile.ElementAt(i));
lbl_message.Text = "Cookie cleared";
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Delete Internet Cache

First Include Heder files and assembly files to your Web page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Windows.Forms;
using System.IO;

[assembly: RegistryPermission(SecurityAction.RequestMinimum, ViewAndModify = "HKEY_CURRENT_USER\\SOFTWARE\\MICROSOFT\\INTERNET EXPLORER\\TYPEDURLS")]
[assembly: RegistryPermission(SecurityAction.RequestMinimum, ViewAndModify = "HKEY_CURRENT_USER\\SOFTWARE\\MICROSOFT\\INTERNET EXPLORER")]

Then use this code:

//below is the textBox for deleting particular Cache from your Browser

string enteredtext = txt_url.Text.Trim();
string lowertext = txt_url.Text.ToLower();
string uppertext = txt_url.Text.ToUpper();

string[] internetcachefile = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), "*", SearchOption.AllDirectories);
for (int i = 0; i < internetcachefile.Count(); i++)
{
try
{
if (internetcachefile.ElementAt(i).Contains(enteredtext) || internetcachefile.ElementAt(i).Contains(lowertext) || internetcachefile.ElementAt(i).Contains(uppertext))
{
File.Delete(internetcachefile.ElementAt(i));
lbl_message.Text = "Cache cleared";
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}