Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have upload the photo i have select the local host (Example D:kannan\photo ) how toc hane in my code
namespace photoshops
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            onflbload(sender, e);
        }
           public void onflbload(object sender, EventArgs e)
        {
           
            
            if (flbload.HasFile)
            {
<pre lang="cs">HttpPostedFile file = UploadedFile.PostedFile;
            string fileExt = Path.GetExtension(file.FileName).ToLower();
            string fileName = Path.GetFileName(file.FileName);
           
                if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".jpeg" || fileExt == ".png")
                    {
                         file.SaveAs("D://" + fileName);
                    }
               }
              
            
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString =@"Data Source=DEVI\SQLEXPRESS; Initial Catalog =cat; Integrated Security=SSPI";
            try
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand("insert into tblphotosettings " + 
                                                "(BillNo,CustomerName,Address,StartDate,EndDate,Systemurl,Numberofcopies,Amount,Total ) values (@BillNo,@CustomerName,@Address,@StartDate,@EndDate,@Systemurl,@Numberofcopies,@Amount,@Total)", connection);
                cmd.Parameters.Add("@BillNo", SqlDbType.NVarChar).Value = TextBox1.Text;
                cmd.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = TextBox2.Text;
                cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = TextBox3.Text;
                cmd.Parameters.Add("@StartDate", SqlDbType.NVarChar).Value = Rdbsdate.SelectedDate;
                cmd.Parameters.Add("@EndDate", SqlDbType.NVarChar).Value = Rdbddate.SelectedDate;
                cmd.Parameters.Add("@Systemurl", SqlDbType.VarChar).Value = flbload ;
                cmd.Parameters.Add("@Numberofcopies", SqlDbType.NVarChar).Value = TextBox7.Text;
                cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = TextBox8.Text;
                cmd.Parameters.Add("@Total", SqlDbType.NVarChar).Value = TextBox9.Text;
                cmd.ExecuteNonQuery();
            }
            
            finally
            {
                connection.Close();
            }
        }
    }
}




the page is run but i have select a picture and click the save button
Error file path is not valid

[edit]Code block added to preserve formatting - OriginalGriff[/edit]
Posted
Updated 5-Apr-11 1:10am
v10
Comments
OriginalGriff 5-Apr-11 2:49am    
When you say "Image location not store" what do you mean? that the "filename" is not saved into the database? What is saved in your "systemurl" column? Have you looked at the actual database content?
vimal22 2 5-Apr-11 3:21am    
database content is varchar
OriginalGriff 5-Apr-11 3:30am    
No, what is saved in the database, not what is the type of the database field!
Ankur\m/ 5-Apr-11 2:53am    
Does it give any error? Or is it storing a value you don't want?

BTW you don't require "+flbload.PostedFile.FileName" part if you want to store all files in 'Photo' folder.
vimal22 2 5-Apr-11 3:14am    
yes 1 error Error 1 Unrecognized escape sequence


string filename = Server.MapPath("~/photo") +flbload.PostedFile.FileName;

I assume that your code is not quite like this:
string filename = Server.MapPath("~/photo") +flbload.PostedFile.FileName;
As This should not generate a "unrecognised escape sequence" error. It would if you had a "\" instead of the "/" character though...

Try putting an "@" in front of the string - that turns off escape character processing, and may make it work better for you:
string filename = Server.MapPath(@"~/photo") + flbload.PostedFile.FileName;

"hi
the page run but i have use local host
Ex string filename = Server.MapPath(@"~/D:\photo") + flbload.PostedFile.FileName;
but error path is in valid .i have use file upload control"


No, that won't work.
The whole idea of Server.MapPath is to convert an internal URL based path to a PC based file structure path. The '~' bit is the URL based bit for the root directory of your website.
So what you are asking for is the path:
"C:\websites\users\UID123456\root/D:\photo" which is seriously not going to work as it will complain about the colon in the middle of the path...
In addition, you need to separate the path from the filename with a further slash or backslash.

The way I would do it:
if (flbload.HasFile)
   {
   string filename = Server.MapPath(@"~/photo") + "/" + flbload.FileName;
   flbload.SaveAs(filename);
   }



"can you post edit in my code i have totally confused i am begineer"


I could, but then the context to other answers is lost. Instead, here is your code modified by mine:

public void onflbload(object sender, EventArgs e)
    {
    if (flbload.HasFile)
        {
        string filename = Server.MapPath(@"~/Resources/Images") + "/" + flbload.FileName;
        flbload.SaveAs(filename);
        using (SqlConnection connection = new SqlConnection())
            {
            connection.ConnectionString = @"Data Source=DEVI\SQLEXPRESS; Initial Catalog =cat; Integrated Security=SSPI";
            try
                {
                connection.Open();
                using (SqlCommand cmd = new SqlCommand("insert into tblphotosettings " +
                                                "(BillNo,CustomerName,Address,StartDate,EndDate,Systemurl,Numberofcopies,Amount,Total) " +
                                                "values (@BillNo,@CustomerName,@Address,@StartDate,@EndDate,@Systemurl,@Numberofcopies,@Amount,@Total)",
                                                connection))
                    {
                    cmd.Parameters.AddWithValue("@BillNo", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@CustomerName", TextBox2.Text);
                    cmd.Parameters.AddWithValue("@Address", TextBox3.Text);
                    cmd.Parameters.AddWithValue("@StartDate", Rdbsdate.SelectedDate);
                    cmd.Parameters.AddWithValue("@EndDate", Rdbddate.SelectedDate);
                    cmd.Parameters.AddWithValue("@Systemurl", filename);
                    cmd.Parameters.AddWithValue("@Numberofcopies", TextBox7.Text);
                    cmd.Parameters.AddWithValue("@Amount", TextBox8.Text);
                    cmd.Parameters.AddWithValue("@Total", TextBox9.Text);
                    cmd.ExecuteNonQuery();
                    }
                }
            catch (Exception ex)
                {
                LogError(ex.Message);
                }
            finally
                {
                connection.Close();
                }
            }
        }
    }
You will see I have made a number of changes!
1) There is the mod I discussed above.
2) I have used using blocks to ensure that the SqlConnection and SqlCommand are properly closed and disposed.
3) I have changed your Parameters.Add to Paramaters.AddWithValue: the former is depreciated and should be replaced with the latter - it is easier to read!
4) I have included a catch block: this way I will find out that an error occurred, even if the user doesn't! Please, try very hard not to omit catches, as they can help you find a problem early, rather than hidding it until it is a catastrophe later!

Two other things:
1) My LogEntry class does not exist in your code: replace the call with your appropriate action: add it to a text log file, or something so you can review it later and work out what error has occurred!
2) Please, please, please, change the names of your controls! TextBox8 may hold your "amount" but it is a lot more obvious if you write
cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = tbAmount.Text;
Rather than
cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = TextBox8.Text;
It just makes it easier to see that you are using the correct text box in the correct place - and you don't have to look back in your design to find out which text box holds the number of copies... :laugh:

"sorry i dont know text log file"

Try this:
using (StreamWriter sw = File.AppendText(Server.MapPath(@"~") + @"/MyLog.Txt"))
    {
    sw.WriteLine(ex.Message);
    }
It's not ideal (it puts it in a generally available location) but it'll do...
(I use a database for my logs, which is a bit more complex to set up than you want at the moment)
 
Share this answer
 
v4
Comments
vimal22 2 5-Apr-11 4:10am    
hi the page run but i have use local host
Ex string filename = Server.MapPath(@"~/D:\photo") + flbload.PostedFile.FileName;
but error path is in valid .i have use file upload control
OriginalGriff 5-Apr-11 4:50am    
Answer updated
vimal22 2 5-Apr-11 4:55am    
can you post edit in my code i have totally confused i am begineer
Ankur\m/ 5-Apr-11 5:23am    
You have already got enough help for being a beginner. A beginner works on the given guidelines. But you want us to write code for you. Have you read the links given by us? You don't want to learn the concept but want to get your work done. Sorry but we discourage this practice.
vimal22 2 5-Apr-11 5:32am    
than you i have check
hey vimal try this

file.SaveAs("D://kannan/photo/" + fileName);
 
Share this answer
 
Comments
vimal22 2 5-Apr-11 4:18am    
string filename = Server.MapPath(@"D://rajan/photo") + flbload.PostedFile.FileName;
file.SaveAs("D://rajan/photo/" + fileName);
flbload.PostedFile.SaveAs(filename);

but error is

Error 1 The name 'file' does not exist in the current context
Error 2 The name 'fileName' does not exist in the current context
vimal22 2 wrote:
yes 1 error Error 1 Unrecognized escape sequence string filename = Server.MapPath("~/photo") +flbload.PostedFile.FileName;
how do i store image path example (D:\kannan\photo) can u post the code


From the code I will assume that it's a web application (which you haven't mentioned or tagged).

You still don't understand how paths works in a web application.
Server.MapPath[^] method maps the specified relative or virtual path to the corresponding physical directory on the server. That means the path that you specified as a parameter will be mapped to the current executing file (depend on relative or virtual path that you have supplied).

Now the very first thing - If you want all the files under the directory 'photo', you don't need "+flbload.PostedFile.FileName;" path. So your file path will be
C#
string filepath = Server.MapPath("~/photo");


Now second thing, you said you want to store the file in location D:\kannan\photo.
You must understand that it doesn't work like this in a web architecture.You will store everything inside your web site's root directory. So there will be a folder inside your web site root directory called 'photo' and every photo will be saved there. There are permission issues as well. So it's always better to store things inside the web site root directory.

I also suggest you to read this - There is something about "Paths" for Asp.net beginners[^]

Hope that gives you some idea.
 
Share this answer
 
C#
public string fn_UploadProductImage(FileUpload UploadedFile)
    {

        HttpPostedFile file = UploadedFile.PostedFile;
        string fileExt = Path.GetExtension(file.FileName).ToLower();
        string fileName = Path.GetFileName(file.FileName);
        if (fileName != string.Empty)
        {
            try
            {
                if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".jpeg" || fileExt == ".png")
                {

                    
                   file.SaveAs("D://kannan/photo/" + fileName);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //str = fileName;
        return fileName;



    }
[Got That]-----------
 
Share this answer
 
Comments
vimal22 2 5-Apr-11 5:45am    
can you post edit in my code i have totally confused i am begineer
vimal22 2 5-Apr-11 5:52am    
an you post edit in my code i have totally confused i am begineer

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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