Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
uploading image in database and wants to show that image in gridview bt when uploading image i m getting an issue..help me/remove erroer plzz..



C#
protected void Button1_Click(object sender, EventArgs e)
   {
       if (FileUpload1.HasFile)
       {
           String str = FileUpload1.FileName;
           FileUpload1.PostedFile.SaveAs(Server.MapPath(",") + "~//uploadedimages//" + str);
           string path = "~//Uploads//"+str.ToString();
           con.Open();
           SqlCommand cmd = new SqlCommand("insert into uploads values('" + TextBox1.Text + "','" + path + "')", con);
           cmd.ExecuteNonQuery();
           con.Close();
           Label1.Text = "Image upload Successfully";
       }
       else
       {
           Label1.Text = "Plz select ur image and upload";
       }

   }

error occoured when i use path as= "~//uploadedimages//"

Could not find a part of the path 'F:\Atverse\Web Example\Image\,~\uploadedimages\Chrysanthemum.jpg'.


error occoured when i use path as= ""~F:/Atverse/Web Example/Image/uploadedimages/"

The given path's format is not supported.


plzz tell me error..or right code for this.
Posted
Comments
Sibasisjena 8-Jan-14 1:54am    
Check my solution. If this will not work then give me your file structure.
amit1992 8-Jan-14 2:17am    
@sibasis05 sir ur code when i use on my machine is showing error in Cmd.ExecuteNonQuery();

unable to remove this error...many times before also..
Sibasisjena 8-Jan-14 2:33am    
Ok please check my next updated solution
amit1992 8-Jan-14 2:41am    
changes which u made in updated code i tried before and nw replaced my code with ur given code bt error is...



""Column name or number of supplied values does not match table definition.""

i m giving u the database table query of this..


CREATE TABLE [dbo].[uploads](
[uid] [int] NOT NULL,
[uname] [varbinary](max) NULL,
[uimg] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Sibasisjena 8-Jan-14 2:50am    
please wait, i will give you a updated sol.

C#
string
                filename = Path.GetFileName(fileupload1.FileName);
                fileupload1.SaveAs(Server.MapPath("~/")
                + filename);

Give it a try,Hope this will help.
 
Share this answer
 
Comments
amit1992 8-Jan-14 4:54am    
@Gitanjali Singh using your code image is uploading and going into database bt not going in folder(which is in Website->uploadimage) so gridview is not showing images
amit1992 8-Jan-14 4:58am    
Sry i was making a mistake its going in folder also nw.
Thnxx..
Run this script in sql server management studio
SQL
CREATE TABLE [dbo].[FileUploads](
	[uid] [int] IDENTITY(1,1) NOT NULL,
	[uname] [nvarchar](max) NULL,
	[uimg] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

change the following line in the method:
C#
SqlCommand cmd = new SqlCommand("insert into FileUploads values('" + TextBox1.Text + "','" + theFileName + "')", con);


Updated one:
C#
protected void Button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Put your connection string here";
            if (FileUpload1.HasFile)
            {
                string theFileName = Path.Combine(Server.MapPath("~/Uploads"), FileUpload1.FileName);

                using (var con = new SqlConnection(connectionString))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into uploads values('" + TextBox1.Text + "','" + theFileName + "')", con);
                    cmd.ExecuteNonQuery();
                }
                if (File.Exists(theFileName))
                {
                    File.Delete(theFileName);
                }
                FileUpload1.SaveAs(theFileName);
                Label1.Text = "Image upload Successfully";
            }
            else
            {
                Label1.Text = "Plz select ur image and upload";
            }

        }

Don't forget to use your own connection string to the server.
 
Share this answer
 
v4
Please have a look on the below link
save image in file and path in database asp.net[^]


C#
protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string theFileName = Path.Combine(Server.MapPath("~/Uploads"), FileUpload1.FileName);

                SqlCommand cmd = new SqlCommand("insert into FileUploads values('" + TextBox1.Text + "','" + theFileName + "')", con);
                cmd.ExecuteNonQuery();
                con.Close();
                if (File.Exists(theFileName))
                {
                    File.Delete(theFileName);
                }
                FileUpload1.SaveAs(theFileName);
                Label1.Text = "Image upload Successfully";
            }
            else
            {
                Label1.Text = "Plz select ur image and upload";
            }

        }

You can have a look upon :
How to get the path, a good article [^]
 
Share this answer
 
v5
C#
FileUpload1.SaveAs(Server.MapPath("~/uploadedimages/") + str);


I think it helps you...
let me know if any problem u got..
 
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