Asp.net Vb Best and Fastest Way to Upload and Display Images
Background
Subsequently my article Uploading Images to Database Using ASP.Internet C# I accept often been asked how to upload images to a database and displaying them from the database using ASP.Internet C#. And so to satisfy those the requirements I decided to write this article especially focusing on beginners and those who want to learn how to upload images to a database and display those images.
Now before creating the application, permit us create a tabular array named "ImageToDB" or whatever name you wish in a database to store the uploaded image files in a database table having the following fields (shown in the following epitome),
And so later that don't forget to ready the identity specification of the id column to yes, considering in this awarding Id is an important role for retrieving the images, if you don't know how to gear up it then see the following example.
When you select the id column of the above table and then it shows the column backdrop as,
Now in the prototype above, yous run across the "Is Identity" property, set it to "yep".
In the preceding table, the ImageName column name is used to store the name of an image file and the image is used to store the image file .
I hope you accept created the same type of tabular array.
Now permit us starting time to create an application to upload and display image files step-by-footstep.
At present create the project every bit,
- "Beginning" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Project" (to avert adding a master page).
- Give the projection a name, such every bit "DisplayingImages" or some other equally you lot wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" - "Default.aspx" page.
- I File upload command, one Button, ane TextBox, one label and one Image control to display images.
Then the <form> section of the Default.aspx page looks as in the following,
- <form id= "form1" runat= "server" >
- <tr> Proper noun
- <td>
- <asp:TextBox ID="TextBox1" runat= "server" /> </td>
- <td>
- <td> Select Image </td>
- <td>
- <asp:FileUpload ID="FileUpload1" runat= "server" /> </td>
- </td>
- </tr>
- </table>
- <tabular array>
- <tr>
- <td>
- <p>
- <asp:Label ID="Label2" runat= "server" Text= "label" ></asp:Label>
- </p>
- </td>
- </tr>
- </table>
- </div>
- </course>
Now we demand to add together a Handler file to our project to display images from the database because we are saving images in binary format to get the binary formatted information we are using the Handler class. To know more than about the handler class delight refer to the MSDN forum.
Adding Handler Course
1. Right-click on your project add a new Handler1.ashx file as,
In the preceding image, nosotros tin can encounter the .ashx extension file, now keep the name as it is or alter it every bit yous wish, I accept kept information technology as information technology is because the next time you can easily remember it and click on the add button, at present later on that your Solution Explorer will look as in the following,
Now switch to design mode to code behind and create the following method to upload the image files as,
- individual void Imageupload()
- {
- if (FileUpload1.HasFile)
- {
- int imagefilelenth = FileUpload1.PostedFile.ContentLength;
- byte [] imgarray = new byte [imagefilelenth];
- HttpPostedFile image = FileUpload1.PostedFile;
- image.InputStream.Read(imgarray, 0, imagefilelenth);
- connection();
- query ="Insert into ImageToDB (ImageName, Prototype) values (@Proper name, @Image)" ;
- SqlCommand com =new SqlCommand(query, con);
- com.Parameters.AddWithValue("@Proper name" , SqlDbType.VarChar).Value = TextBox1.Text;
- com.Parameters.AddWithValue("@Prototype" , SqlDbType.Image) Value = imgarray;
- com.ExecuteNonQuery();
- Label1.Visible =truthful ;
- Label1.Text ="Epitome Is Uploaded successfully" ;
- imagebindGrid();
- }
- }
Now double-click on the "Imageupload" push button and call the preceding method on information technology as,
- protected void upload( object sender, EventArgs e)
- {
- Imageupload();
- }
At present, create the method to bind to the GridView as,
- public void imagebindGrid()
- {
- connection();
- query ="Select id, ImageName, Image from ImageToDB" ;
- SqlCommand com =new SqlCommand(query, con);
- SqlDataReader dr = com.ExecuteReader();
- Gridview1.DataSource = dr;
- Gridview1.DataBind();
- }
And call preceding "Imagebindgirdmethod"() on page load as,
- protected void Page_Load( object sender, EventArgs due east)
- {
- Label1.Visible =false ;
- if (!IsPostBack)
- imagebindGrid();
- }
Now, open the "Handler1.ashx.cs" and write the following lawmaking,
- public class Handler1 : IHttpHandler
- {
- Default cls =new Default();
- public void ProcessRequest(HttpContext context)
- {
- string displayimgid = context.Asking.QueryString[ "id_Image" ].ToString();
- cls.connexion();
- cls.query ="select Image from ImageToDB where id=" + displayimgid;
- SqlCommand com =new SqlCommand(cls.query, cls.con);
- SqlDataReader dr = com.ExecuteReade();
- dr.Read();
- context.Response.BinaryWrite((Byte[])dr[0]);
- context.Response.End();
- }
- }
In the preceding Handler1.ashx.cs file, outset I have created the object of the default.aspx.cs form to use the connection method and variable to avoid the repetition of lawmaking because in the handler.ashx.cs file we too need the aforementioned connexion method and string variables that are in the default.aspx.cs class.
At present, the most important thing is to specify the image URL property of the epitome command that display the images from the database, and so let united states of america encounter how to do it.
ImageUrl='<%# "Handler1.ashx?id_Image="+ Eval("id") %>'
Now run, the application that will look similar as in the following,
At present upload the images that will display the output as follows,
In preceding output, y'all can clearly see that my uploaded image is displayed in the GridView with file name and prototype.
Note
- For detailed lawmaking please download the zero file attached higher up.
- Don't forget to update the "Web.config" file for your server location.
Summary
Now nosotros have learned how to upload images to a database and display those images in GridView. I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.
Source: https://www.c-sharpcorner.com/UploadFile/0c1bb2/uploading-and-displaying-images-from-database-using-Asp-Net/