Tuesday, November 1, 2011

Read Stream from a file in chuncks

Problem
I had a programming requirement where I need to read from a file but only 32kb could be transferred at a time. So how do we do that.

Solution
Solution to that problem is that we use binary reader to read only the chunk of data from the file then we use filewriter to write the file.


int chunkSize = 1024;
byte[] chunk = new byte[chunkSize];
         
string destinationPath = @"C:\Users\faraz\MyFileDestination\"+_filename;
string filepath = @"C:\Users\"+targetFile;
using (FileStream fileReader = new FileStream(filepath, FileMode.Open, FileAccess.Read) )
{
      FileStream filewriter = new FileStream(destinationPath, FileMode.Create, FileAccess.ReadWrite);
      BinaryReader binaryReader = new BinaryReader(fileRead);

      int bytesToRead = (int)fileReader.Length;

      do
      {
              chunk = binaryReader.ReadBytes(chunkSize);
              filewriter.Write(chunk, 0, chunk.Length);
              bytesToRead -= chunkSize;
      } while (bytesToRead > 0);

      filewriter.Close();
      binaryReader.Close();    
}
            
            ResultLlabel.Text = @"File Was written successfully.";

Wednesday, April 13, 2011

ASHX Generic Handler in ASP.Net

Problem:
I was working on WCF project where I was returning the list of objects, each object contained a field that holds Image, since it was a WCF project so I had to send images as byte[] array, now problem was that the picture was in byte[] array format, ASP.Net page does't convert it automatically into Image and when binding ListView to the collection, it was converting my byte[] array to a text field instead of Image.

Impact:
Web page does't display Images.

Solution:
Solution to that problem is to use ashx page. When creating a web page that dynamically returns an Image, XML file, PDF file or other non html web pages from the query string we use ashx page to perform those actions.

This solution will show how to use ahsx page to dynamically convert byte[] array to an image.

Step 1: Add new Generic Handler by right clicking on web project->Add New Item then select Generic Handler. Give it any name you want, I named mine HttpHandler.We need to pass the query string to the handler and handler will convert the byte[] array to an image and return a Url of that image, to do that go back to your aspx web page, in the ListView Item templet look for the column that is bound to the Image. It should look something like this.

ImageUrl='<%# Eval("Image") %>' //where Image is the name of the column.

Since ListView is bound to a collection of record and each record has an ID column that is associated with the Image, we will pass that Id as a query string instead of Image, and since we already have a collection we could create a session object that stores that collection so that we could access it in our handler, so do all that first replace your Eval("Image") with the following.

ImageUrl='<%# "HttpHandler.ashx?id=" + Eval("Id") %>'


Now we are passing the Id of that record to the HttpHandler.

Step 2: Create a Http Session object.
In the code behind file of you page, go to Page load method and create a session and pass your collection to that session.

private MovieServiceClient _movieService;
private List<Movie> _movies;

protected void Page_Load(object sender, EventArgs e)
{
        try
        {
            _movieService = new MovieServiceClient("MovieService_WsHttp");
            _movies = _movieService.GetAllMovies();
            Session["session"] = _movies;
        }
        catch (FaultException ex)
        {
            ErrorLabel.Text = ex.Message;
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = ex.Message;
        }
}
        
Step 3: Code to convert byte[] into an image using HttpHandler.

In HttpHandler page we need to call session object and by default we sessions are not enabled in ashx pages, we use IRequiresSessionState Interface to enable it. Write the following code in your Process request method().


public class HttpHandler : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest (HttpContext context)
    {
        try
        {
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.BufferOutput = false;
            //get the id from the query string.
            int id = Int32.Parse(context.Request.QueryString["id"]);
            //set the type to Image/jpeg, or png or gif, any type that you are returning.
            context.Response.ContentType = "Image/jpeg";
            //get the session object.
            context.Session["session"] = HttpContext.Current.Session["session"];
            if (context.Session["session"] != null)
            {
                //pass the session object to a List<>
                var list = context.Session["session"];

                foreach (var movie in (List<Service.Movie>)list)
                {
                    //check if the id of that record matches to that record.
                    if (id == movie.Id)
                    {
                        //set the response that convert byte[] to an image.
                        context.Response.BinaryWrite(movie.Image);
                        break;
                    }
                }

            }
        }
        catch (Exception ex)
        {
            context.Response.Write(ex.Message);
        }
      
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

this will return one image at a time as your ListView loop through all the records, your web page should now show that Image.

Sunday, April 10, 2011

Hiding buttons in Gridview based on user.

Problem
I created a web project where I am binding the list of students to the GridView. I also enabled selection, edition and deletion. My application also involved security features where I had admin, power user and guest roles. Admin could add, edit and delete, power user could add, but can't delete, and guest user can only view the record, but the problem is I can't seem to find a way to hide the buttons depending on the user.

Impact
Can either completely hide or show the buttons.

Solution
when we bind the list to the grid view, grid view creates column and bind each field to each column, it also creates columns for each field we create for Edit, Delete, Insert or Select button. In the Item templet we have a property called Visible where we could hide or show columns. Since we don't know who the user will be until run time we have to create a method that returns the true or false depending on the user.


<ItemTemplate>
                        <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False"
                            CommandName="Edit" ImageUrl="~/Images/database_edit.png" Text="Edit"                    Visible='<%# GetShowEditButton() %>' />
                        &nbsp;<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False"
                            CommandName="Select" ImageUrl="~/Images/arrow_left.png" Text="Select" />
                        &nbsp;<asp:ImageButton ID="ImageButton3" runat="server" CausesValidation="False"
                            CommandName="Delete" ImageUrl="~/Images/delete.png" Text="Delete" Visible='<%# GetShowDeleteButton() %>' />
                    </ItemTemplate>

Code behind file where I created the highlighted methods.


public partial class StudentView : System.Web.UI.Page
{
    private StudentRepository _studentRepository = new StudentRepository();
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }

    protected bool GetShowDeleteButton()
    {
        String[] rolesArray = Roles.GetRolesForUser();
        if (rolesArray != null)
        {
            if (rolesArray.Contains("Admin"))
                return true;
        }
        return false;
    }

    protected bool GetShowEditButton()
    {
        String[] rolesArray = Roles.GetRolesForUser();
        if (rolesArray != null)
        {
            if (rolesArray.Contains("Admin"))
                return true;
            if (rolesArray.Contains("PowerUser"))
                return true;
        }
        return false;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.DataSource = _studentRepository.GetAllStudents();
        GridView1.PageIndex = e.NewPageIndex;

        GridView1.DataBind();
    }
}

Overriding Tostring method

Problem
I created a list of person class but when run it through foreach loop and print it to console I was getting namespace.Person instead of actual person name.

Impact
Can't print the name of the person in the class.

Solution
I is a very common mistake that people make when they are new to OOP languages, well at least I did. I did't understand how we use console.writeln() method to print out integer or double values even though it accept only string value.

Ever wondered why when we create a custom class, we get few methods and properties even thought we did't even created them. Every class in C# is ultimately driven from object class. object class defines all these methods, one of them is ToString() method. To string is the method that gets called automatically when we use and print the value of an object. Following program shows how we could override and use to string method to help us out solving that small problem.


namespace ToStingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            Animal animal = new Animal("Dog");
            Vertex3D vertex3d = new Vertex3D(1,2,3);
            Console.WriteLine("Without overriding Tostring = {0}",person.ToString());
            Console.WriteLine("\nAfter Overridding Tostring method = {0}",animal.ToString());
            Console.WriteLine("\nAfter overridding and using Format method = {0}",vertex3d.ToString());
            Console.ReadLine();
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    public class Animal
    {
        public string Name { get; set; }

        public Animal(string name)
        {
            Name = name;
        }

        public override string ToString()
        {
            return Name;
        }
    }

    public class Vertex3D
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int Z { get; set; }

        public Vertex3D(int x,int y, int z)
        {
            X = x;
            Y = y;
            Z = z;
        }

        public override string ToString()
        {
            return string.Format("{0},{1},{2}", X, Y, Z);
        }
    }

}


And here is the output that we get after running our little program.

Thursday, April 7, 2011

Hashing In C#

Problem
It is a common practice to save the user and the password associated with the user as a plain text in the database, but there is one problem with this approach, your passwords are not secure, anyone one who has an access to the database can steal your data. How to make sure that your password is secure.

Impact
Data is not secure.

Solution
 To make sure that password or any kind of data you passing is secure we use a process called Hashing. In C# we could used MD5 or SHA1 Hashing. Following is an example of how to create a hash for a password that user entered in the textbox.




namespace PasswordEncription
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string password = PassWordTextBox.Text;
            byte[] buffer = GetCharArray(password);
            ResultTextBox.Text = EncriptPassword(buffer);
        }

        private byte[] GetCharArray(string password)
        {
            byte[] buffer = new byte[password.Length];
            int i = 0;
            foreach (byte c in password)
            {
                buffer[i] = c;
                i++;
            }
            return buffer;
        }

        private string EncriptPassword(byte[] password)
        {
            MD5CryptoServiceProvider encriptor = new MD5CryptoServiceProvider();
            byte[] encriptedPassword = encriptor.ComputeHash(password);
            StringBuilder stringBuilder = new StringBuilder();
            foreach (sbyte b in encriptedPassword)
            {
                stringBuilder.Append(b.ToString("x2"));
            }
            return stringBuilder.ToString();
        }
    }
}

Monday, March 28, 2011

Transaction object

Problem
I had a situation where I had to add a record in the table, and at the same time I needed to add record in another table but I needed an Id of the first table to enter in the second second table. What must I do to achieve that.

Impact
Program will not do what it is suppose to do.

Solution
Solution to this problem is to use transaction object. Think of it this way, if we go to bank and transfer the money from one account to another, first bank has to take money out of the account and then transfer it in the other account, what happens if first transaction succeeds but the second one does't.  C# uses transaction object to solve this issue. When we use transaction object both of these transactions must succeed before we enter the record in the database, if one fails all other transactions will rollback. Here is the code that I wrote where I am entering record in the data base and retrieving the id of the record just been entered and then committing to the action.


public int Save(Customer customer)
        {
            int roweffected = 0;
            try
            {
                //first open the connection to the database
                _connection.Open();
                //begin the transaction.
                using(SqlTransaction transaction = _connection.BeginTransaction())
                {
                    //create a command object to enter a record in the database.
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.CommandText = "Insert into Customer (FirstName,LastName,DateOfBirth,HouseNumber," +
                                              "Street,City,Country,PostCode,Phone,Email) Values(@firstname,@lastname," +
                                              "@dateofbirth,@housenumber,@street,@city,@country," +
                                              "@postcode,@phone,@email)";
                        command.Connection = _connection;
                        command.Transaction = transaction;
                        command.Parameters.AddWithValue("@firstname", customer.FirstName);
                        command.Parameters.AddWithValue("@lastname", customer.LastName);
                        command.Parameters.AddWithValue("@dateofbirth", customer.DateOfBirth);
                        command.Parameters.AddWithValue("@housenumber", customer.HouseNumber);
                        command.Parameters.AddWithValue("@street", customer.StreetName);
                        command.Parameters.AddWithValue("@city", customer.City);
                        command.Parameters.AddWithValue("@country", customer.Country);
                        command.Parameters.AddWithValue("@postcode", customer.PostCode);
                        command.Parameters.AddWithValue("@phone", customer.Phone);
                        command.Parameters.AddWithValue("@email", customer.Email);
                        roweffected = command.ExecuteNonQuery();
                    }
                    //Create another command object to retrieve the record id you just entered.
                    using (SqlCommand command = new SqlCommand())
                    {
                        //this query returns the id of the last record entered in the customer table.
                        command.CommandText = "Select @@Identity from Customer";
                        command.Connection = _connection;
                        command.Transaction = transaction;
                        var value = command.ExecuteScalar();
                    }
                    transaction.Commit();
                }
              
                return roweffected;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

Saturday, March 26, 2011

How To Save Image To SqlServer

Problem: I am currently working on my college project where I was required to save images to the database and I had no idea how to do that.

Impact:
Unable to save images to the database.

Solution:
After doing some research I found some material on this topic, so I wrote a small program for this post to share my knowledge with you guys. First create a windows form application, add four button, load, browse, save and clear, Add two text boxes, A picture box and from Dialog Tool Box select openFileDialog. Here is the screen shot of my form.


First we need to up load the image to the picture box using open file dialog. When user click on browse open file dialog will pop up and the select the image. Following code shows how to get image in picture box.


private void BrowseButton_Click(object sender, EventArgs e)
{
            openFileDialog1.ShowDialog();
            //_file is a string declared as a private field at class level.
            _file = openFileDialog1.FileName;
            pictureBox1.Image = Image.FromFile(_file);
            FileBrowserTextBox.Text = _file; 
}


Now this is the hard part. Once the image is loaded in the picture box and you are sure that this is the image you want to save, then click on Save button. But before we save the image, we need to convert the image into  byte of array, and then pass it to the database. I used separate layer (Project) for database and created a class called ImageRepository. Image Repository does two things save and load, before I show you how to do that we need to convert the image into byte[]. Following code shows how to convert image into byte[] and then pass it to the database.


private void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                //use ReadFile method to get byteArray
                byte[] data = ReadFile();
                ImageRepository repository = new ImageRepository();
                int result = repository.SaveImage(data);
                if(result==1)
                {
                    MessageBox.Show(@"Image Saved Successfully");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


private byte[] ReadFile()
        {
            byte[] data;
            //Use FileInfo object to get file size.
            FileInfo fileInfo = new FileInfo(_file);
            long fileLength = fileInfo.Length;
            //Open FileStream to read file
            FileStream fileStream = new FileStream(_file,FileMode.Open,FileAccess.Read,FileShare.Read);
            //Use BinaryReader to read file stream into byte array.
            BinaryReader binaryReader = new BinaryReader(fileStream);

             //When you use BinaryReader, you need to supply number of bytes
//to read from file.
      //In this case we want to read entire file. 
      //So supplying total number of bytes.
            data = binaryReader.ReadBytes((int)fileLength);
            //Close the file stream
            fileStream.Close();
            return data;
        }


Now we will use Repository to save the image.

public int SaveImage(byte[] imagedata)
        {
            try
            {
                byte[] imageData = imagedata;
                //get the connectin string.
                string connectionstring = @"Data Source=FARAZ-PC\FARAZ_K1;Initial Catalog=RentAMovieDB;Integrated Security=True";
                SqlConnection connection = new SqlConnection(connectionstring);

        //Open the connection to the database.

                connection.Open();
                SqlCommand command = new SqlCommand();
                //Insert image to the database. I am updating my record since I had a table with a column name Image. you could use Insert command instead of update.
                command.CommandText = "UPDATE Movie SET Image = @image where Name = 'Your                  Highness'";
                command.Connection = connection;
                //Adding the parameter to the command object.
                command.Parameters.AddWithValue("@image", imageData);
                //if the result is 1 that means that image was saved.
                int result = command.ExecuteNonQuery();
                connection.Close();
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

Now we need to load the image from the database. Since I have my Image stored in Movie table, so I will use Movie name to search for the image I am looking for. Getting the image from the database is other way round, before we were converting the image into byte[] array and then saving it, now we are getting the image from that database as byte[] array and we need to convert it back into image and load it to picture box. First I am providing the name of the movie to look for the image. Here is what happens when user clicks on LoadImage button.

private void LoadButton_Click(object sender, EventArgs e)
        {
          
            try
            {
                ImageRepository repository = new ImageRepository();
                //get the byte[] data from database using repository class.
                byte[] image = repository.LoadImage(LoadTextBox.Text);
                Image newImage;

        //Read image data into a memory stream

                using(MemoryStream memoryStream = new MemoryStream(image,0,image.Length))
                {
              
                    memoryStream.Write(image,0,image.Length);
                    //Set image variable value using memory stream.
                     newImage = Image.FromStream(memoryStream, true);
                }
                //Set picturebox image.
                pictureBox1.Image = newImage;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


//get image using the repository class loadImage method.

public byte[] LoadImage(string movieName)
        {
            byte[] image = null;
            string connectionstring = @"Data Source=FARAZ-PC\FARAZ_K1;Initial Catalog=RentAMovieDB;Integrated Security=True";
            SqlConnection connection = new SqlConnection(connectionstring);
            connection.Open();
            SqlCommand command = new SqlCommand();
            command.CommandText = "Select image from Movie where Name = '" + movieName+"'";
            command.Connection = connection;
            SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
            DataSet dataset = new DataSet();
            dataAdapter.Fill(dataset);
            foreach (DataRow row in dataset.Tables[0].Rows)
            {
                image = (byte[])row["Image"];
            }
            return image;
        }

Here is the form after loading the image from the database.