Friday, December 20, 2013

Review on Green Tea Nose Pack by Etude House~ & Cleansing Nose Pack by YADAH

So, this is my 1st beauty review on this blog!! And so, I tried the Green Tea Nose Pack by Etude House on my sister and Cleansing Nose Pack by YADAH on myself.


Steps to put a pore strip
Firstly, wash your face.
Secondly, wet your nose with enough water.
Thirdly, dry your hands and carefully place the pore strip on your nose.
Fourth, wet the pore strip so that it sticks well on your skin.
Lastly, wait for it to dry and peel it off.

Now, let's start with the Green Tea Nose Pack

Before
She's under the stage of growing up so Acne is not really avoidable, I can only give her things to reduce them such as mask etc

Using


After

To be honest, this is the results....


Two blackheads removed....LOL I didn't expect only two since my sis had quite a number of blackheads on her nose...So ya, to me, its not really useful. I would prefer Shiseido black mask

Shiseido Black Mask

Wait till next time, when I put on my Shiseido Black Mask and do a review on it, you will understand why I will prefer this :D

Okay, moving on
.
.
.
.
My review on Cleansing Nose Pack by YADAH

And THIS IS ME!! (a bit weird)


Before
So as you can see, I have much more pores than my sister...So of course, I should expect more blackheads to be removed!

Putting it on
This nose pack took a longer time to dry as compared to the green tea nose pack......I waited about 30 mins??

RESULTS
So my face had no difference, the before and after is exactly the same!!! And the results is even more shocking than the green tea nose pack....Nothing much got removed! Only one miserable blackhead that does not even look like blackhead came off!

Oh my god, I wasted reaching 45 mins just to remove 1 blackhead, I'm extremely disappointed that I threw the whole pack away!

Okay....so this is the end of my review. Comparing both of the nose packs, Green Tea nose pack is slightly better....Next time I'll do other reviews on different nose packs as well :D See you next time :)


Thursday, December 19, 2013

Check for duplicate record

How to check for duplicate records so that your database will not be filled with same content over and over again??


In this tutorial, I am checking for duplicate record for a book so that when adding a book record, duplicated book record will NOT be added.

NOTE: In this tutorial, I used a shortcut to connect the connection string. You can still use your normal connection string. However, if you wish to use the shortcut, remember to replace your connectionStrings line in your Web.config

 TO BE REPLACED IN WEB.CONFIG

 <connectionStrings>  
   <add name="ApplicationServices"  
      connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"  
      providerName="System.Data.SqlClient" />  
   
   <add name="connString" connectionString="data source=_______________;database=_________________________;Integrated Security=True" providerName="System.Data.SqlClient"/>  
  </connectionStrings>  
   


Firstly, in the .aspx file, add this code below to allow user to add a book name record.

 <b><asp:Label ID="lblbookname" Text="Enter book name: " runat="server"></asp:Label></b><br />  
   <asp:TextBox ID="bookname" runat="server" Width="300px"></asp:TextBox><br />  
   <div id="bookdivMessage" runat="server"></div><br />  
   
 <b><asp:Button ID="addbookbtn2" Text="Add Book" runat="server" Width="100px"   
       onclick="addbookbtn_Click"></asp:Button></b><br />   

Next, in the aspx.cs file (code behind file), add this part to allow access and usage of Sql and Class file which will be created later.

 using System.Data;  
 using System.Data.SqlClient;  
 using ProjectName.App_Code;  


 protected void addbookbtn_Click(object sender, EventArgs e)   
    {   
     int numOfRecordsAffected = 0;   
     CBookManager BooksManager = new CBookManager();   
     string collectedBookName = "";   
     collectedBookName = bookname.Text;   
    Boolean isThereDuplicateRecordByBookName = false;   
     isThereDuplicateRecordByBookName = BooksManager.checkDuplicateRecordByBookName(collectedBookName);   
     if (isThereDuplicateRecordByBookName == true)   
     {   
      bookdivMessage.InnerHtml = "There is already a book with this name";   
     }   
     if (isThereDuplicateRecordByBookName == false)   
     {   
      numOfRecordsAffected = BooksManager.addOneBook(collectedBookName);   
     divMessage.InnerHtml = "<u>" + numOfRecordsAffected + "</u> record has been added";  
     }   
     else   
     {   
      numOfRecordsAffected = 0;   
     divMessage.InnerHtml = "<u>" + numOfRecordsAffected + "</u> record has been added";  
     }   
    }   


After completing the code behind file, create a file named App_Code within your project and create a class file named CBookManager. After creating CBookManager, change the build action to COMPILE and add the following codes below.

 using System.Data;  
 using System.Data.SqlClient;  
 using System.Configuration;  -- To use Connection String

This code below is to check for duplicate record within the database. If there is duplicate record within database, record will not be added. However, if duplicate record == false, record will be added successfully.

 public Boolean checkDuplicateRecordByBookName(String inBookName)  
     {  
       int result = 0;  
       SqlCommand cmd = new SqlCommand();  
       SqlConnection conn = new SqlConnection();  
       string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;  
       conn.ConnectionString = connString;  
       cmd.Connection = conn;  
   
       string SqlText = "SELECT COUNT(*) FROM BOOK WHERE BOOKNAME = @inBookName";  
       cmd.CommandText = SqlText;  
       cmd.Parameters.Add("@inBookName", SqlDbType.VarChar, 100);  
       cmd.Parameters["@inBookName"].Value = inBookName;  
       conn.Open();  
       result = Convert.ToInt32(cmd.ExecuteScalar());  
       conn.Close();  
       if (result >= 1)  
       {  
         return true;  
       }  
       else  
       {  
         return false;  
       }  

Next, to activate the add book function, insert the following code:

 public int addOneBook(String inBookName)  
     {  
       int numOfRecordsAffected = 0;  
       SqlCommand cmd = new SqlCommand();  
       SqlConnection conn = new SqlConnection();  
       //Setup of the connection information   
   
       string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;  
       conn.ConnectionString = connString;  
   
       cmd.Connection = conn;//Tell the Command object use the Connection object  
   
       //Prepare a INSERT SQL template  
       string sqlText = "INSERT INTO Book (BookName) ";  
       sqlText += " VALUES (@inBookName)";  
       //setup the SQL in the cmd object  
   
       cmd.CommandText = sqlText;  
   
       cmd.Parameters.Add("@inBookName", SqlDbType.VarChar, 100);  
       cmd.Parameters["@inBookName"].Value = inBookName;  
         
       conn.Open();  
       numOfRecordsAffected = cmd.ExecuteNonQuery();  
       conn.Close();  
   
       return numOfRecordsAffected;  
     }  

Remember to create a Book database, which consists of BookName, type = varchar, 100 characters.

We have come to the end of this tutorial :D Thanks for your patience and have fun coding :)

Tuesday, December 10, 2013

Checkbox function for gridview and delete function

Creating checkbox for gridview together with delete function

In this tutorial, I'll be using a book management system as an example.



NOTE: In this tutorial, I used a shortcut to connect the connection string. You can still use your normal connection string. However, if you wish to use the shortcut, remember to replace your connectionStrings line in your Web.config

 TO BE REPLACED IN WEB.CONFIG

 <connectionStrings>  
   <add name="ApplicationServices"  
      connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"  
      providerName="System.Data.SqlClient" />  
   
   <add name="connString" connectionString="data source=_______________;database=_________________________;Integrated Security=True" providerName="System.Data.SqlClient"/>  
  </connectionStrings>  
   

Firstly, insert this javascript below which activates the checkbox.

 <script type="text/javascript">  
   var TotalChkBx;  
   var Counter;  
   window.onload = function () {  
     //Get total no. of CheckBoxes inside the GridView.  
     TotalChkBx = parseInt('<%= this.grdData.Rows.Count %>');  
     //Get total no. of checked CheckBoxes in side the GridView.  
     Counter = 0;  
   }  
   function HeaderClick(CheckBox) {  
     //Get target base & child control.  
     var TargetBaseControl =  
     document.getElementById('<%= this.grdData.ClientID %>');  
     var TargetChildControl = "selectbookchklist";  
     //Get all the control of the type INPUT in the base control.  
     var Inputs = TargetBaseControl.getElementsByTagName("input");  
     //Checked/Unchecked all the checkBoxes in side the GridView.  
     for (var n = 0; n < Inputs.length; ++n)  
       if (Inputs[n].type == 'checkbox' &&  
         Inputs[n].id.indexOf(TargetChildControl, 0) >= 0)  
         Inputs[n].checked = CheckBox.checked;  
     //Reset Counter  
     Counter = CheckBox.checked ? TotalChkBx : 0;  
   }  
   function ChildClick(CheckBox, HCheckBox) {  
     //get target control.  
     var HeaderCheckBox = document.getElementById(HCheckBox);  
     //Modifiy Counter;   
     if (CheckBox.checked && Counter < TotalChkBx)  
       Counter++;  
     else if (Counter > 0)  
       Counter--;  
     //Change state of the header CheckBox.  
     if (Counter < TotalChkBx)  
       HeaderCheckBox.checked = false;  
     else if (Counter == TotalChkBx)  
       HeaderCheckBox.checked = true;  
   }  
 </script>  

Next, within your gridview and before the 1st boundfield.

 <asp:TemplateField HeaderText="Select">  
   <HeaderTemplate>  
   <asp:CheckBox runat="server" ID="selectallbookchklist" onclick="javascript:HeaderClick(this);"/>  
   </HeaderTemplate>  
   <ItemTemplate>  
     <asp:CheckBox ID="selectbookchklist" runat="server" />  
   </ItemTemplate>  
   </asp:TemplateField>  

Lastly, insert the delete button.
(You can ignore this part if you are not implementing the delete function using checkbox)

 <asp:Button ID="delbtn" Text="Delete" Width="70px" runat="server"   
       onclick="delbtn_Click"/>  

Now, you are done for the basic presentation of checkbox and delete button. Next part will be implementing the code behind file.

In this tutorial, the delete function does not literally delete the record. It hides the records in database by creating an attribute named RecordDeleteDate, type=datetime, Allow null.

If a record is deleted, the delete button updates the RecordDeleteDate with the date. Hence, when the web browser calls out for all records, the records with RecordDeleteDate filled up will not be displayed. However, the record can be viewed if the web browser calls out for deleted records.

 protected void delbtn_Click(object sender, EventArgs e)  
     {  
       int numofRecordsDeleted = 0;  
       CheckBox deleteCheckBox;  
       CBookManager BooksManager = new CBookManager();  
       foreach (GridViewRow row in grdData.Rows)  
       {  
         deleteCheckBox = (CheckBox)row.FindControl("selectbookchklist");  
         if (deleteCheckBox.Checked == true)  
         {  
           numofRecordsDeleted += BooksManager.deleteOneBook(grdData.DataKeys[row.RowIndex].Value.ToString());  
         }  
       }  
       divMessage.InnerHtml = numofRecordsDeleted.ToString() + " book record(s) has been deleted";  
       DataTable BookDataTable = new DataTable();  
       BookDataTable = BooksManager.getAllBookData();  
       grdData.DataSource = BookDataTable;  
       grdData.DataBind();  
     }  

Moving on, the next part will be implementing the method to use the check-boxes.

 protected void gvCheckboxes_RowCreated(object sender, GridViewRowEventArgs e)  
     {  
       if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))  
       {  
         CheckBox selectbookchklist = (CheckBox)e.Row.Cells[1].FindControl("selectbookchklist");  
         CheckBox selectallbookchklist = (CheckBox)this.grdData.HeaderRow.FindControl("selectallbookchklist");  
         selectbookchklist.Attributes["onclick"] = string.Format("javascript:ChildClick(this,'{0}');", selectallbookchklist.ClientID);  
       }  
     }  

In here, I created a class file named CBookManager.cs within an App_Code folder. Remember to set it's build action to compile.

Also, if you are retrieving data records that are not deleted, remember to insert this in your SQL code:
RecordDeleteDate IS NULL


 public int deleteOneBook(String inBookRecordId)  
     {  
       int numOfRecordsAffected = 0;  
       SqlCommand cmd = new SqlCommand();  
       SqlConnection conn = new SqlConnection();  
       string sqlText = "UPDATE Book SET RecordDeleteDate=GETDATE()";  
       sqlText += " WHERE BookRecordId=@inBookRecordId";  
       string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;  
       conn.ConnectionString = connString;  
       cmd.Connection = conn;  
       //Prepare a valid DELETE SQL statement  
       cmd.CommandText = sqlText;//setup the SQL in the cmd object  
       cmd.Parameters.Add("@inBookRecordId", SqlDbType.Int);  
       cmd.Parameters["@inBookRecordId"].Value = inBookRecordId;  
       conn.Open();  
       numOfRecordsAffected = cmd.ExecuteNonQuery();  
       conn.Close();  
       return numOfRecordsAffected;  
     }  

Additional part

If you have a function that uses the recordId in the gridview such as an update function. You may choose to consider this part :D

 int intRowIndexWhichUserHasClicked = 0;  
       // If multiple buttons are used in a GridView control, use the  
       // CommandName property to determine which button was clicked.  
       if (e.CommandName == "UpdateOneBookCommand")  
       {  
         intRowIndexWhichUserHasClicked = Convert.ToInt32(e.CommandArgument);  
         Response.Redirect("ADMupdateBook.aspx?BookRecordId=" + grdData.DataKeys[intRowIndexWhichUserHasClicked].Value);  
       }  




Thursday, December 05, 2013

Simple search function asp.net C#

How to implement a basic search button in your website? 

Note: in this tutorial, this is a book search function with regards to a gridview

In your webform, insert these few lines where you want the search function to be.

 <asp:TextBox ID="txtSearch" runat="server" Width="200px" ></asp:TextBox>  
   <asp:Button ID="btnSearch" runat="server" Text="Search" Width="70px"   
       onclick="btnSearch_Click"/>  

Next, in your code behind file, implement this method

 using System.Data;  
 using System.Data.SqlClient;  
 using ProjectName.App_Code;  

protected void btnSearch_Click(object sender, EventArgs e)  
     {  
       DataTable dtBookData = new DataTable();  
       CBookManager BooksManager = new CBookManager();  
       string searchName = "";  
       searchName = txtSearch.Text;  
       dtBookData = BooksManager.searchBookByName(searchName);  
       grdData.DataSource = dtBookData;  
       grdData.DataBind();  
     }  

Next, in your class file, App_Code - CBookManager, insert this part.

 public DataTable searchBookByName(String inSearch)  
     {  
       SqlDataAdapter ad = new SqlDataAdapter();  
       SqlCommand cmd = new SqlCommand();  
       SqlConnection connection = new SqlConnection();  
       DataSet ds = new DataSet();  
       String sqlText = "SELECT Book.BookRecordId, Book.BookName FROM Book";  
       sqlText += " WHERE BookName LIKE @inBookName";  
       string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;  
       connection.ConnectionString = connString;  
       cmd.Connection = connection;  
       cmd.CommandText = sqlText;  
       cmd.Parameters.Add("@inBookName", SqlDbType.VarChar, 100);  
       cmd.Parameters["@inBookName"].Value = "%" + inSearch + "%";  
       ad.SelectCommand = cmd;  
       connection.Open();  
       ad.Fill(ds, "BookData");  
       connection.Close();  
       return ds.Tables["BookData"];//Return the data table to the web form  
     }  

After some editing, you should be able to search for the correct data. Good luck in implementing this search function. :)



Simple paging for gridview asp.net C#

How to create a simple paging for your grid-view. 

Assuming you already have everything in place, without the paging.

Firstly, insert this in your page that requires paging. In this case, it will be manageBook.aspx as it is a book management system.

 <asp:GridView ID="grdData" runat="server" DataKeyNames="BookRecordId" AutoGenerateColumns="false"   
   OnRowCommand="BookManage" AllowPaging="True" PageSize="4"   
   onpageindexchanging="grdData_PageIndexChanging" PagerSettings-Mode="NumericFirstLast"  
   PagerSettings-Visible="True">  

Next, in your code behind file, manageBook.aspx.cs, insert this method.

 protected void grdData_PageIndexChanging(object sender, GridViewPageEventArgs e)  
     {  
       DataTable dtBook = new DataTable();  
       CBookManager BooksManager = new CBookManager();  
       dtBook = BooksManager.getAllBookData();  
       grdData.DataSource = dtBook;  
       grdData.PageIndex = e.NewPageIndex;  
       grdData.DataBind();  
     }  

That's All :D Any mistakes in the tutorial, do comment below so that I can change it :) Thanks

Wednesday, December 04, 2013

Uploading, storing, update, retrieving and displaying images/pictures into database asp.net C#


Final Outcome

How to allow user to upload, store, update, retrieve and display images into your database and web sever?

This short tutorial by me shows you the simplified method of storing your images and retrieving the images efficiently.

Scenario :
You are creating a website that allows you to upload, store, update and display profile image.

What you should already have:

Database prepared for testing (Set your ProfileImage as image type)

- ProfileRecordId   int    primary key (Set identity specification to yes)
- ProfileImage        image

You should have these webforms/files done at the end of this tutorial.
1) App_Code
2) addprofilepic.aspx
3) displayprofilepic.aspx
4) manageprofilepics.aspx
5) profileImageHandler.ashx
6) updateProfile.aspx
7) Unknown.aspx - empty webpage(just for redirecting)





NOTE: In this tutorial, I used a shortcut to connect the connection string. You can still use your normal connection string. However, if you wish to use the shortcut, remember to replace your connectionStrings line in your Web.config

 TO BE REPLACED IN WEB.CONFIG

 <connectionStrings>  
   <add name="ApplicationServices"  
      connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"  
      providerName="System.Data.SqlClient" />  
   
   <add name="connString" connectionString="data source=_______________;database=_________________________;Integrated Security=True" providerName="System.Data.SqlClient"/>  
  </connectionStrings>  
   


Creating necessary files:
1) Create a file named App_Code
2) Create a class file named CProfileManager.cs
3) Set build action of CProfileManager.cs to compile

Add the following name spaces in every code behind file that uses Sql, App_Code.
 using System.Data;  
 using System.Data.SqlClient;  
 using ProjectName.App_Code; -->Note: this calls out your App_Code files which is very important.   


UPLOADING & STORING

1: addprofilepic.aspx

Firstly, add these 3 codes in your add profile pic.aspx

 <asp:Label ID="lbladdprofilepic" Text="Add profile image: " runat="server" Width="300px"></asp:Label><br />  
   
 <asp:FileUpload ID="profilepicupload" runat="server" /><br />  
   
 <asp:Button ID="addbtn" Text="Add Profile Image" runat="server" Width="100px" onclick="addimagebtn_Click"></asp:Button>  
   


2: addprofilepic.aspx.cs

Next, in your code behind file, addprofilepic.aspx.cs

This code will be command the web browser to execute the "add" action when the button is clicked


 using System.Data;  
 using System.Data.SqlClient;  
 using System.Configuration;   

protected void addimagebtn_Click(object sender, EventArgs e)  
     {  
       int numOfRecordsAffected = 0;  
       CProfileManager ProfileManager= new CProfileManager();  
         
       byte[] collectedProfilePic = new byte[profilepicupload.PostedFile.ContentLength];  
       HttpPostedFile Image = profilepicupload.PostedFile;  
       Image.InputStream.Read(collectedProfilePic , 0, (int)profilepicupload.PostedFile.ContentLength);  
   
       if (profilepicupload.HasFile)  
       {  
         numOfRecordsAffected = ProfileManager.addProfileImage(collectedProfilePic);  
         Response.Redirect("Unknown.aspx");  
       }  
       else  
       {  
         numOfRecordsAffected = 0;  
       }  
     }  

3: CProfileManager.cs

In the class file, the configuration method must be called so that the browser can connect to the database.

 using System.Data;  
 using System.Data.SqlClient;  
 using System.Configuration;  

 public int addProfileImage(Byte[] inProfileImage)  
     {  
       int numOfRecordsAffected = 0;  
       SqlCommand cmd = new SqlCommand();  
       SqlConnection conn = new SqlConnection();  
       //Setup of the connection information   
   
       string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;  
       conn.ConnectionString = connString;  
   
       cmd.Connection = conn;  
       //Tell the Command object use the Connection object  
   
       //Prepare a INSERT SQL template - You have a Profile Database already  
       string sqlText = "INSERT INTO Profile (ProfileImage) ";  
       sqlText += " VALUES (@inProfileImage)";  
       //setup the SQL in the cmd object  
       cmd.CommandText = sqlText;  
   
       cmd.Parameters.Add("@inProfileImage", SqlDbType.Image, inProfileImage.Length);  
       cmd.Parameters["@inProfileImage"].Value = inProfileImage;  
   
       conn.Open();  
       numOfRecordsAffected = cmd.ExecuteNonQuery();  
       conn.Close();  
   
       return numOfRecordsAffected;  
     }  

RETRIEVING & DISPLAYING

So far, we have done the uploading of image, now we will display it

4: profileImageHandler.ashx.cs  -  Generic Handler

In this step, we are implementing an image based handler. For more information, you can check out this link here


using System.Data;
using System.Data.SqlClient;
using System.Configuration;   


 public void ProcessRequest(HttpContext context)  
     {  
       SqlConnection connection = new SqlConnection();  
       connection.ConnectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;  
       connection.Open();  
       string sql = "SELECT ProfileImage";  
       sql += " FROM Profile WHERE ProfileRecordId=@ProfileRecordId";  
       SqlCommand cmd = new SqlCommand(sql, connection);  
       cmd.Parameters.Add("@ProfileRecordId", SqlDbType.Int).Value = context.Request.QueryString["ProfileRecordId"];  
       cmd.Prepare();  
       SqlDataReader dr = cmd.ExecuteReader();  
       dr.Read();  
       context.Response.BinaryWrite((byte[])dr["ProfileImage"]);  
       dr.Close();  
       connection.Close();  
     }  

5: displayprofilepic.aspx

Simply place this line of code in the displayprofilepic.aspx to display the image.

 <asp:Image ID="profilepic" runat="server" width="100" Height="150"/>  

6: displayprofilepic.aspx.cs

In this step, do remember to insert the namespaces.

 protected void Page_Load(object sender, EventArgs e)  
     {  
       CProfileManager ProfileManager = new CProfileManager();  
       DataRow ProfileRow;  
       DataTable ProfileDataTable = new DataTable();  
       String ProfileRecordId = "";  
   
       if (Page.IsPostBack == false)  
       {  
         ProfileRecordId = (String)Request.QueryString["ProfileRecordId"];  
         ProfileDataTable = ProfileManager.displayOneProfileData(ProfileRecordId);  
         ProfileRow = ProfileDataTable.Rows[0];  
         profilepic.ImageUrl = "profileImageHandler.ashx?ProfileRecordId=" + ProfileRecordId;  
       }  
       }  

7: CProfileManager.cs - retrieve specific profile image only

 public DataTable displayOneProfileData(String inProfileRecordId)   
 {  
   SqlDataAdapter ad = new SqlDataAdapter();  
   SqlCommand cmd = new SqlCommand();  
   SqlConnection conn = new SqlConnection();  
   DataSet ds = new DataSet();  
   string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;  
   conn.ConnectionString = connString;  
   cmd.Connection = conn;  
   ad.SelectCommand = cmd;  
   conn.Open();  
   cmd.CommandText = "SELECT ProfileImage FROM Profile WHERE ProfileRecordId=@inProfileRecordId";  
   cmd.Parameters.AddWithValue("@inProfileRecordId", inProfileRecordId);  
   ad.Fill(ds, "ProfileData");  
   conn.Close();  
   return ds.Tables["ProfileData"];//Return the data table to the web form  
 }  


UPDATING

8: CProfileManager.cs - Update profile image

 public int updateOneProfile(String inProfileRecordId, Byte[] inProfileImage)  
 {  
   int numOfRecordsAffected = 0;  
   SqlCommand cmd = new SqlCommand();  
   SqlConnection conn = new SqlConnection();  
   string sqlText = "UPDATE Profile SET ProfileImage=@inProfileImage";  
   sqlText += " WHERE ProfileRecordId=@inProfileRecordId";  
   
   string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;  
   conn.ConnectionString = connString;  
   //setup the SQL in the cmd object  
   cmd.CommandText = sqlText;  
   cmd.Connection = conn;  
   
   cmd.Parameters.Add("@inProfileRecordId", SqlDbType.Int);  
   cmd.Parameters["@inProfileRecordId"].Value = inProfileRecordId;  
   
   cmd.Parameters.Add("@inProfileImage", SqlDbType.Image, inProfileImage.Length);  
   cmd.Parameters["@inProfileImage"].Value = inProfileImage;  
   
   conn.Open();  
   numOfRecordsAffected = cmd.ExecuteNonQuery();  
   conn.Close();  
   return numOfRecordsAffected;  
 } //for all fields updated  
   
   

9: updateProfile.aspx


 <b><asp:Label ID="lblupdateprofileimage" Text="Update profile image: " runat="server" Width="300px"></asp:Label></b>  
   <asp:Image ID="profileimagedisplay" runat="server" width="100" Height="150"/>  
   <asp:FileUpload ID="profilepicupload" runat="server" /><br />  
   
 <b><asp:Button ID="updateprofilebtn" Text="Update Profile" runat="server" Width="100px" onclick="updateprofilebtn_Click"></asp:Button></b><br />  
   


10: updateProfile.aspx.cs

 protected void Page_Load(object sender, EventArgs e)  
     {  
       CProfileManager ProfileManager = new CProfileManager();  
       DataRow ProfileRow;  
       DataTable ProfileDataTable = new DataTable();  
       String ProfileRecordId = "";  
   
       if (Page.IsPostBack == false)  
       {  
         ProfileRecordId = (String)Request.QueryString["ProfileRecordId"];  
         ProfileDataTable = ProfileManager.displayOneProfileData(ProfileRecordId);  
         ProfileRow = ProfileDataTable.Rows[0];  
         profileimagedisplay.ImageUrl = "profileImageHandler.ashx?ProfileRecordId=" + ProfileRecordId;  
       }  
     }  
   
     protected void updateprofilebtn_Click(object sender, EventArgs e)  
     {  
   
       int numOfRecordsAffected = 0;  
       CProfileManager ProfileManager = new CProfileManager();  
   
       string collectedProfileRecordId = (String)Request.QueryString["ProfileRecordId"];  
   
       if (profilepicupload.HasFile == true)  
       {  
         byte[] collectedProfilePic = new byte[profilepicupload.PostedFile.ContentLength];  
         HttpPostedFile Image = profilepicupload.PostedFile;  
         Image.InputStream.Read(collectedProfilePic, 0, (int)profilepicupload.PostedFile.ContentLength);  
         numOfRecordsAffected = ProfileManager.updateOneProfile(collectedProfileRecordId, collectedProfilePic);  
         Response.Redirect("Unknown.aspx");  
       }  
       else  
       {  
         numOfRecordsAffected = 0;  
       }  


MANAGING ALL IMAGES

11: manageprofilepics.aspx


 <div>  
   <table border="1">  
   <tr>  
    <td>Manage Profile Images</td>  
   </tr>  
   <tr>  
   <td><div id="div1" runat="server"></div></td>  
   </tr>  
   <tr>  
   <td>  
   <asp:GridView ID="grdData" runat="server" DataKeyNames="ProfileRecordId" AutoGenerateColumns="false" OnRowCommand="ProfileManage">  
    <Columns>  
   
   <asp:BoundField DataField="ProfileRecordId" HeaderText="Profile Id">  
   <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"/>  
      <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />  
      </asp:BoundField>  
   
 <asp:TemplateField>  
  <ItemTemplate>  
   <asp:Image ID="profileimage" runat="server" ImageUrl='<%#"profileImageHandler.ashx?profileRecordId=" + Eval("profileRecordId")%>' width="100" Height="150"/>  
   
   
  </ItemTemplate>  
 </asp:TemplateField>  
   
   <asp:ButtonField ButtonType="Button" Text="Update" CommandName="UpdateOneProfileCommand"/>  
   </Columns>  
   </asp:GridView>  
   <div id="divMessage" runat="server"></div>  
   <asp:Button ID="addbtn" Text="Add" Width="70px" runat="server"   
       onclick="addbtn_Click"/>  
   </td>  
   </tr>  
   <tr>  
   <td style="text-align:right;">&nbsp;</td>  
   </tr>  
   </table>  
    
   </div>  

12: manageprofilepics.aspx.cs


 protected void Page_Load(object sender, EventArgs e)  
     {  
       if (Page.IsPostBack == false)  
       {  
         CProfileManager ProfileManager = new CProfileManager();  
         DataTable dtProfile = new DataTable();  
         dtProfile = ProfileManager.getAllProfileData();  
         grdData.DataSource = dtProfile;  
         grdData.DataBind();  
       }  
     }  
   
   
     public void ProfileManage(Object sender, GridViewCommandEventArgs e)  
     {  
   
       int intRowIndexWhichUserHasClicked = 0;  
       // If multiple buttons are used in a GridView control, use the  
       // CommandName property to determine which button was clicked.  
       if (e.CommandName == "UpdateOneProfileCommand")  
       {  
         intRowIndexWhichUserHasClicked = Convert.ToInt32(e.CommandArgument);  
         Response.Redirect("updateProfile.aspx?ProfileRecordId=" + grdData.DataKeys[intRowIndexWhichUserHasClicked].Value);  
       }  
     }  
   
   
   
     protected void addbtn_Click(object sender, EventArgs e)  
     {  
       Response.Redirect("addprofilepic.aspx");  
     }  

13: CProfileManager.cs - Retrieve all images


 public DataTable getAllProfileData()    
     {  
       SqlConnection conn = new SqlConnection();  
       SqlCommand cmd = new SqlCommand();  
       SqlDataAdapter ad = new SqlDataAdapter();  
       DataSet ds = new DataSet();  
   
       String sqlText = "SELECT ProfileRecordId, ProfileImage FROM Profile";  
       string connString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;  
       conn.ConnectionString = connString;  
       cmd.Connection = conn;  
       ad.SelectCommand = cmd;  
       cmd.CommandText = sqlText;  
       conn.Open();//open an active connection  
       ad.Fill(ds, "ProfileData");  
       conn.Close();//close the active connection   
       return ds.Tables["ProfileData"];  
     }  


ADDITIONAL:

To validate the type of image uploaded:


 <script type="text/javascript">  
   
   $(document).ready(function () {  
     $('#form1').validate({}); -->This is your form name (<form id="form1" runat="server">)  
   
     $("#profilepicupload").rules('add', {  
       required: true,  
       extension: "jpg|jpeg",  
       messages: {  
         required: "Please Upload Profile Image",  
         extension: "Please Upload only .jpg and .jpeg"  
       }  
     });  
   
   });  
   
 </script>  
   

We have come to the end to our tutorial :D Hope you can understand it :) If you do see any mistakes around, please tell me so that I can change it :)