How To Send Selected Images From Gridview To Database

In this tutorial, I’ll show you how to send the selected images from GridView to database. For that, I’ll first upload some of the images to my database table, and then load these images to GridView. Then, after selecting a few images from the GridView, we will save them to another database table.

Initial Chamber

Step 1

Open your Visual Studio 2010 and create an empty website.

Give it a suitable name [gridview_demo].

Step 2

In Solution Explorer, you get your empty website. Add a web form and SQL Database –

For Web Form

gridview_demo (Your Empty Website) -> Right Click -> Add New Item -> Web Form. Name it as -> gridview_demo.aspx.

For SQL Server Database

gridview_demo (Your Empty Website) -> Right Click -> Add New Item -> SQL Server Database. [Add Database inside the App_Data_folder].

Database Chamber

Step 3

Get to your Database [Database.mdf]. We will create a table -> tbl_image, and tbl_save.

Go to the database.mdf - -> Table - -> Add New table. Design your tables like this -

Tbl_image


Tbl_save


Design Chamber

Step 4

Open your grid_demo.aspx file to create our design. We will drag an HTML table, a FileUpload control, two buttons and a label, and a Textbox and GridView.


Code Chamber

Step 5

Open grid_demo.aspx.cs file to write code to save the image to database. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Data.SqlClient;  
  7. using System.Data.Sql;  
  8. using System.Data;  
  9. using System.Web.UI.WebControls;  
  10.   
  11. public partial class _Default : System.Web.UI.Page  
  12. {  
  13.   
  14.     SqlConnection con = new SqlConnection(@"Data Source=Nilesh;Initial Catalog=test_db;Integrated Security=True");  
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.     {  
  17.         if (!IsPostBack)  
  18.         {  
  19.             refreshdata();  
  20.               
  21.         }  
  22.   
  23.     }  
  24.   
  25.     protected void Button1_Click(object sender, EventArgs e)  
  26.     {  
  27.         if (FileUpload1.HasFile)  
  28.         {  
  29.             string str = FileUpload1.FileName;  
  30.             FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Upload/") + str);  
  31.             string image = "~/Upload/" + str.ToString();  
  32.             string imagename = TextBox3.Text;         
  33.             SqlCommand cmd = new SqlCommand("insert into tbl_image values(@imagename,@image)", con);  
  34.             cmd.Parameters.AddWithValue("@imagename", imagename);  
  35.             cmd.Parameters.AddWithValue("@image",image);  
  36.   
  37.             con.Open();  
  38.             int i = cmd.ExecuteNonQuery();  
  39.             con.Close();  
  40.   
  41.             refreshdata();  
  42.   
  43.             Label1.Text = "File Uploaded";  
  44.             Label1.ForeColor = System.Drawing.Color.Green;  
  45.   
  46.         }  
  47.   
  48.         else  
  49.         {  
  50.             Label1.Text = "Please Upload valid file";  
  51.             Label1.ForeColor = System.Drawing.Color.Red;  
  52.   
  53.         }  
  54.   
  55.          
  56.     }  
  57.   
  58.     public void refreshdata()  
  59.     {  
  60.   
  61.         SqlCommand cmd = new SqlCommand("select * from tbl_image",con);  
  62.         SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  63.         DataTable dt = new DataTable();  
  64.         sda.Fill(dt);  
  65.         GridView1.DataSource = dt;  
  66.         GridView1.DataBind();   
  67.          
  68.     }  
  69.   
  70.     protected void Button2_Click(object sender, EventArgs e)  
  71.     {  
  72.        
  73.         foreach (GridViewRow gvrows in GridView1.Rows)  
  74.         {  
  75.             var checkbox = gvrows.FindControl("CheckBox1") as CheckBox;  
  76.             if (checkbox.Checked)  
  77.             {  
  78.   
  79.                 var lblname = gvrows.FindControl("Label2") as Label;  
  80.                 SqlCommand cmd = new SqlCommand("insert into tbl_save values(@imagename)", con);  
  81.                 cmd.Parameters.AddWithValue("imagename", lblname.Text);  
  82.                 con.Open();  
  83.                 int i = cmd.ExecuteNonQuery();  
  84.                 con.Close();  
  85.                 Label3.Text = "Image Saved";  
  86.                 Label1.ForeColor = System.Drawing.Color.Green;  
  87.   
  88.                 refreshdata();  
  89.   
  90.             }  
  91.   
  92.             else  
  93.             {  
  94.   
  95.                 Label3.Text = "Please try it again";  
  96.                 Label1.ForeColor = System.Drawing.Color.Red;  
  97.             }  
  98.   
  99.   
  100.              
  101.   
  102.              
  103.         }  
  104.           
  105.     }  
  106. }   

OUTPUT


Let’s say, we upload the image name test5 in our database table (tbl_image) and it is also been loaded in the GridView dynamically.


Now, the user has chosen to save that test5 image to our database table (tbl_save). The user will select it and press on Save button. You can see your image in tbl_save now.


Hope you like it. Thank you for reading.

Have a good day!


Similar Articles