Sayan Chatterjee

Sayan Chatterjee

  • NA
  • 22
  • 2.9k

I want to store images in database tables

Aug 1 2017 2:59 PM
I have written a code as follows -
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Web;   
  5. using System.Web.UI;   
  6. using System.Web.UI.WebControls;   
  7. using System.Data.SqlClient;   
  8. using System.IO;     
  9. namespace Shop   
  10. {            
  11. public partial class addimage : System.Web.UI.Page       
  12. {         SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Sayan\\Documents\\Saanvi.mdf;Integrated Security=True;Connect Timeout=30");           
  13.           protected void Page_Load(object sender, EventArgs e)  
  14.  {          
  15.  }         
  16.           protected void Buttonuploadimage_Click(object sender, EventArgs e)  
  17.  {               
  18. FileUpload f = new FileUpload();  
  19.   
  20. if (f.HasFile)               
  21. {                   
  22. string str = f.FileName;  
  23. f.PostedFile.SaveAs(Server.MapPath(".") + "//Images//" + str);    
  24. string path = "~//Images//" + str.ToString();  
  25. con.Open();  
  26. SqlCommand cmd=new SqlCommand("Insert into Images values('" + path + "')", con);   
  27. cmd.ExecuteNonQuery();  
  28. con.Close();   
  29. Labeluploadimage.Text = "Image uploaded successfully";  
  30. }    
  31.              
  32. else    
  33. {       
  34. Labeluploadimage.Text = "Pls. upload an image";  
  35. }  
However, I see that the if statement is not executed and always the statement under the else statement is executed. Is this something to do with the Button click event handler? I have a "upload image" button and a "upload" control used from the toolbox. I have an "Images" folder under solution explorer, where I have already kept few pics.

Answers (2)