1

i have project in my pc..but when i am saving my uploaded files in a folder inside my project.now when i am transferring my project in another pc the server.mappath() is not working..why??

my problem upload function

protected void addproblem_Click(object sender, EventArgs e)
{
    string filepath;
    if (problemupload.HasFile)
        try
        {
            if(problemupload.PostedFile.ContentType=="application/pdf")
            {
               // problemupload.SaveAs("F:\\0\\My project website\\sgipc\\problems\\" + problemupload.FileName);
               // filepath = "F:\\0\\My project website\\sgipc\\problems\\" + problemupload.PostedFile.FileName;
                problemupload.SaveAs(Server.MapPath("\\sgipc\\problems\\" + problemupload.FileName));
                filepath = Server.MapPath(problemupload.PostedFile.FileName);
                string con = " ";
                con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
                SqlConnection objsqlconn = new SqlConnection(con);
                objsqlconn.Open();
                string userid = Convert.ToString(Session["userid"]);
                SqlCommand cmd = new SqlCommand("INSERT INTO problemtable(problemname,problempath,userid,status) Values('" + probbox.Text + "','" + filepath + "','" + userid + "','" + "pending" + "')", objsqlconn);
                cmd.ExecuteNonQuery();
                objsqlconn.Close();
            }
            else
            {
                Response.Write("<script>alert('" + "Only pdf format is allowed..." + "')</script>");
            }

        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.ToString() + "')</script>");
        }
    else
    {
        Response.Write("<script>alert('" + "you have not specified a file..." + "')</script>");
    }

 }

i am getting this error

"System.InvalidOperationException: Failed to map the path '/sgipc/tutorials/v6-1446-1449.pdf'."

while uploading a file from another pc..but from my pc it works fine..

Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
  • Your code is susceptible to a SQL Injection Attack. Please learn about this and take the appropriate steps to prevent it. Here's an article I wrote that will help you: http://colinmackay.co.uk/2005/04/23/sql-injection-attacks-and-some-tips-on-how-to-prevent-them/ And here are all the posts I've created on the subject: http://colinmackay.co.uk/tag/sql-injection-attack/ – Colin Mackay Mar 10 '14 at 10:51

1 Answers1

6

I think there is problem in Server.MapPath try with this syntax FileUploadControl.SaveAs(Server.MapPath("~/sgipc/problems/" + problemupload.FileName));

~ sign will automatic configure path from server and it is better then \\ sign for map path..

Shirish
  • 1,252
  • 11
  • 20
  • while using this command i am getting one error in line cmd.ExecuteNonQuery();..the error is binary data may be truncated.. –  Mar 10 '14 at 06:21
  • yes then just increase size of image path saving column --> varchar(MAX) – Shirish Mar 10 '14 at 06:22
  • @Shirish, varchar(MAX) is a bit excessive for a file path. 261, IIRC, is the longest path available in Windows. – Colin Mackay Mar 10 '14 at 10:48
  • @ColinMackay can you explain further? – Shirish Mar 10 '14 at 10:57
  • @ColinMackay What will be the problem in varchar(MAX)? http://stackoverflow.com/questions/4377740/database-design-preferred-field-length-for-file-paths. if u have better option then i really want to learn.. :) – Shirish Mar 10 '14 at 11:04
  • nvarchar(MAX) is slower because it has the potential to not store it in a contiguous block, unlike NVARCHAR(0..4000) so it uses a streaming interface (which you don't generally see). So, the best thing is to find our how long a path is and optimise for that. Although some Windows API functions allow paths upto 32767 chars, most will constrain it to 260 chars. So telling SQL Server to prepare for more is overkill which slows the database down. – Colin Mackay Mar 10 '14 at 14:06
  • Here's the documentation on max path: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath – Colin Mackay Mar 10 '14 at 14:09
  • @Shirish: Explain, how is it different? – Colin Mackay Mar 11 '14 at 11:14
  • @ColinMackay can you tell me in ms sql table what data type i should use to store image path? this thing only i want to know nothing else... – Shirish Mar 12 '14 at 04:52
  • I already told you it was up to 260 characters (or 255 chars depending on the document you read). That implies NVARCHAR(260). – Colin Mackay Mar 12 '14 at 10:15