Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir.

I am using file upload contol to upload an image in folder.
The image is uploading to folder properly, but I want to save the image path in the database.
How can I save the file path in the database?
Can anybody help me please?

My code is given below:

C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        try
        {
            String picture;
            if (FileUpload1.PostedFile.FileName.EndsWith(".jpg") || FileUpload1.PostedFile.FileName.EndsWith(".gif") || FileUpload1.PostedFile.FileName.EndsWith(".png"))
            {
                string strfilename;

                //string filename = Path.GetFileName(FileUpload1.FileName);

                string File_Path_Text = FileUpload1.PostedFile.InputStream.ToString();
                //string filepath = FileUpload1.PostedFile.ContentLength();

                strfilename = FileUpload1.PostedFile.FileName;
                strfilename = strfilename.Substring(strfilename.LastIndexOf("\\") + 1);
                FileUpload1.SaveAs(Server .MapPath("\\Test\\" + strfilename));
                Server.MapPath("~/images/" + FileUpload1.FileName);
                picture = strfilename;

                SqlParameter [] oparam=new SqlParameter[3];
                oparam[0]=new SqlParameter("@LocationId", TxtLocationId .Text );
                oparam[1]=new SqlParameter("@LocationName", TxtLocationName.Text );
                oparam[2] = new SqlParameter("@Image", File_Path_Text);

                DataSet ds = new DataSet();
                ds = BusinessLogic.InsertImage(oparam);
                //LblShow.Text = "File Saved at:" + filepath;
            }

            //if (FileUpload1.PostedFile.ContentLength != 0)
            //{

            //    DataSet ds = new DataSet();

            //    FileUpload1.SaveAs(@"Test\\" + FileUpload1.FileName);

            //    Server.MapPath("images\\" + FileUpload1.FileName);

                //string path = FileUpload1.PostedFile.FileName;
                //FileUpload1.SaveAs("@\\Test\\" + FileUpload1.FileName);

                //Server.MapPath("\\images\\" + FileUpload1.FileName);
                //SqlParameter [] oparam=new SqlParameter[4];
                //oparam[0]=new SqlParameter("@LocationId", TxtLocationId .Text );
                //oparam[1]=new SqlParameter("@LocationName", TxtLocationName.Text );
                //oparam[2]=new SqlParameter ("@Image", FUImage .FileName );

                //DataSet ds = new DataSet();
            //}
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
Posted
Updated 2-Nov-10 0:57am
v2
Comments
Ankur\m/ 2-Nov-10 7:28am    
Use 'Add Comment' below a user's answer to discuss with him because that notifies the user about your response, an answer doesn't.

From the code you have posted, it would seem that BusinessLogic.InsertImage() is the method that does the work.

Beyond that it is impossible to help you without seing the code for that.
 
Share this answer
 
Comments
Ankur\m/ 2-Nov-10 7:25am    
[moved from answer]
OP wrote:

hai. thanks for ur reply.
actually i am storing image path in varchar format.
my stored procedure is
below
USE [Test]
GO
/****** Object: StoredProcedure [dbo].[SP_InsertImage] Script Date: 11/02/2010 16:33:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter proc [dbo].[SP_InsertImage]
(
@LocationID int,
@LocationName nvarchar(20),
@ImagePath nvarchar(20)
)
as begin
insert into ImageTable
(
LocationID ,
LocationName,
ImagePath
)
values
(
@LocationID ,
@LocationName,
@ImagePath
)
end
exec SP_InsertImage '1','hyd','sun.png'

it is workig well.

i am just storing the file path. from where we are selecting the file.

plz help me.
how can get the filepath using fileupload control.
SQL
INSERT INTO table(FilePath) VALUES (FileUpload.PostedFile.FileName)


AFAIU you can run this query inside your code and you will have your path in database.

Or I am missing something ? Kindly give your inputs for further solution.
 
Share this answer
 
Comments
Ankur\m/ 2-Nov-10 7:24am    
[moved from answer]
OP wrote:

hai. thanks for ur reply.
actually i am storing image path in varchar format.
my stored procedure is
below
USE [Test]
GO
/****** Object: StoredProcedure [dbo].[SP_InsertImage] Script Date: 11/02/2010 16:33:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter proc [dbo].[SP_InsertImage]
(
@LocationID int,
@LocationName nvarchar(20),
@ImagePath nvarchar(20)
)
as begin
insert into ImageTable
(
LocationID ,
LocationName,
ImagePath
)
values
(
@LocationID ,
@LocationName,
@ImagePath
)
end
exec SP_InsertImage '1','hyd','sun.png'

it is workig well.

i am just storing the file path. from where we are selecting the file.

plz help me.
how can get the filepath using fileupload control.
SqlParameter [] oparam=new SqlParameter[3];
                oparam[0]=new SqlParameter("@LocationId", TxtLocationId .Text );
                oparam[1]=new SqlParameter("@LocationName", TxtLocationName.Text );
//@Image is different from procedure @Imagepath
                oparam[2] = new SqlParameter("@ImagePath", FileUpload1.FileName);
//for complete path use
oparam[2] = new SqlParameter("@ImagePath", FileUpload1.PostedFile.FileName);
 
Share this answer
 

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