Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi to all,


I am working on a School Management Project.
I need to add Faculties and Students photos in there profile.

I want to add image files in file system and there paths in SQL Database as nvarchar(255).

Please describe me, how to achieve this?
Posted

If you want to save the path of the photos in the database, I suggest you use the FileUpload object.
In this way you get the File and may save some table in this way.
Example (In. Net):
VB
FileUpload.PostedFile.FileName.ToString.Trim()
 
Share this answer
 
Comments
nitindongre 14-Dec-12 10:34am    
thanks to answer,


can you please elaborate
nitindongre 17-Dec-12 1:32am    
Hi, please describe what you want to say.
FehPorto 17-Dec-12 6:12am    
you call an object "fileupload" on your page. In this object you can climb the photos and save your way.
When you save, after rising the way you will see it in the following way: FileUpload.PostedFile.FileName.ToString.Trim ()

That done you will have saved the path of your photo in the database.
Note: The column,where u want to store ur images in database,should be declared type-Image

byte[] Attachmnt;
 private void btnBrowse_Click(object sender, EventArgs e) //Button by which u will select image from ur system
{   
            
OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Select image Files Only";

openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;

openFileDialog1.Filter = "Jpeg files (*.jpg)|*.jpg|All Files(*.*)|*.*"; 
//Specify format above as u want
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;

openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
               
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
tbUpload.Text = openFileDialog1.SafeFileName;//Here u get Only fileName
FilePath = openFileDialog1.FileName;//Here u will get FilePath
if (openFileDialog1.FileName != null)  
{//this block will convert ur image file to byte array format, which will be accept by ur database column's where type is "image" u must declare
System.IO.FileStream fs = new System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
long byteLength = new System.IO.FileInfo(FilePath).Length;
Attachmnt = binaryReader.ReadBytes((Int32)byteLength);
//Above in Attachmnt u will get ur image file to byte format now u can save the value of "Attachmnt" to that column,either by stored procedure or by query
//Now at next step,is u want to get it from ur database to ur system or anywhere, u have to again convert into byte array format.
fs.Close();
fs.Dispose();
binaryReader.Close();
                    }                    
                }
               
             }      
 
Share this answer
 
v2
Comments
nitindongre 14-Dec-12 10:35am    
I don't want to save photos in database. Photos will go to a folder in server and the paths of photos will be saved in database
StackQ 14-Dec-12 10:40am    
OK,how u uploading ur image?
incobilgisayar 7-Sep-18 14:35pm    
is it written on webservices ? or genereric handler ?.. client side is written by html -jquery (ajax) ..other is complated by webservices

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900