Redirecting user in the catch block:
Redirection is easy in Asp.net. All you need to do is to supply the Response.Redirect with the url where you wish to redirect. Redirection takes an interesting turn when you try to do this in the catch block. Catch blocks in C# are mainly used to catch the Exception and then take the appropriate action to handle the exception. If you wish to redirect a user to a different page whenever an exception is thrown you can follow a simple code snippet as shown below:
try {
// write some code that creates an exception
}
catch(Exception ex)
{
// Redirect to other page
Response.Redirect("NewPage.aspx");
}
Unfortunatly, if you try to run this code and your code throws exception instead of redirecting the user to the specified page it will throw an exception "System.ThreadingException" . You can easily overcome this exception by adding a "false" parameter in the Response.Redirect method. The "false" parameter means that the old thread will not be discarded and will be removed later by the garbage collector.
Response.Redirect("NewPage.aspx",false);
Sorting ArrayList in Ascending and Descending order :
I personally like ArrayList. The reason is simple it can take object type as a parameter. You can add any datatype to an arraylist. Sometimes we need to sort some data and than bind it to page. The easiest way to sort the data is to sort it in arraylist.
Ascending Order:
Before sorting any arraylist we need to populate the list with some data. This can be easily done by the "Add" method of the arraylist.
ArrayList aList = new ArrayList();
aList.add("one");
aList.add("two");
aList.add("three");
We just added three items to the arrayList. Now we need to sort the arraylist. Sorting in ascending order can be done easily using the Sort method of the arrayList.
aList.Sort(); // this sorts the arraylist in ascending order
Descending Order:
Lets now sort the arrayList in Descending order:
aList.Sort();
aList.Reverse(); // This sorts it in Descending order
You can also use the IComparable Interface to implement your own sorting.
Displaying Images from database using Asp.net:
Inserting images is always one of the hard jobs. In this code snippet I am going to show you that how you can insert an image to an Access Database. Always remember that Access is a weak database and cannot take the load of the large images. So in order to save images you can always use the Server's Folder and save all the images there. In the code snippet below I used server's folder named as "C:\ServerFolder\".
File1 is a HTMLFileInput control which lets you browse any file from your computer harddrive.
string strFileName = File1.PostedFile.FileName; // gets the name of the file which is to be uploaded this also returns the whole path of the file
string c = System.IO.Path.GetFileName(strFileName); //Extract the file name from the whole file path
try
{
File1.PostedFile.SaveAs(@"C:\ServerFolder\"+c); //Saves the file on the Server's Folder
Response.Write(@"C:\ServerFolder\"+c);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
FileStream o = null;
StreamReader r = null;
string jpgFile = @"C:\ServerFolder\"+c; // jpgFile is the name of the uploaded file along with the Server's path
o = new FileStream(jpgFile,FileMode.Open,FileAccess.Read,FileShare.Read); //Read the file and assigns to the filestream object
r = new StreamReader(o);
// The following code reads the file as a series of bytes and put them in the byte array. Later the byte array is used as a parameter to be inserted into the database. The database field type that holds the image is known as the blob object
try
{
byte[] FileByteArray = new byte[o.Length-1];
OleDbCommand CmdObj = new OleDbCommand("InsertImage",myConnection);
CmdObj.CommandType = CommandType.StoredProcedure;
CmdObj.Parameters.Add("@ImageUrl",OleDbType.VarChar,50);
CmdObj.Parameters["@ImageUrl"].Value = jpgFile;
CmdObj.Parameters.Add("@Image",OleDbType.Binary);
CmdObj.Parameters["@Image"].Value = FileByteArray;
myConnection.Open();
CmdObj.ExecuteNonQuery();
myConnection.Close();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
Setting focus on HTML TextArea control:
When you click on the HTML TEXTAREA control you can write in it starting from the middle. If you want to write starting from the left you can easily configure it.
Here is a simple java script function that is called on the onfocus event of the HTML TextArea controls.
function WriteFromLeft()
{
// The name of the text area is txtArea1 and the name of the form is Form1
document.Form1.txtArea1.value = "";
}
yup thats it just remember that this function is called on the "onfocus" event of the TextArea control.
Removing and item from the ListBox:
This code is executed on a button click and used to remove the selected item from the ListBox.
private void _delFromListBox(object sender, EventArgs e)
{
for(int i=_rightListBox.Items.Count-1;i>=0;i--)
{
if(_rightListBox.Items[i].Selected)
{
_rightListBox.Items.Remove(_rightListBox.Items[i]);
}
}