Click here to Skip to main content
15,894,312 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
the company in which i am working have an IBM SERVER, i want to upload image to that server and display it in gridview i am using MS sql server management 2008 so i want to know how to upload image to that server and retrive it fom there bcz after publishing the website the image dosent get displayed in the gridview so help me on that!!

this is my code for inserting in a particular folder but not in server
C#
protected void btnSubmit_Click(object sender, EventArgs e)
  {

      int id = Convert.ToInt32(Session["AgentMasterID"]);
      string temp = "";
      if (fuProducImage.HasFile)
      {
          if (CheckFileType() == true)
          {
              string ext1 = Path.GetExtension(fuProducImage.FileName);
              var query = (from c in agb.AgentProducts orderby c.AgentProductID descending select c.AgentProductID).First();
              int lastid = Convert.ToInt32(query);
              int last = lastid + 1;
              string directoryPath = Server.MapPath("../AgentImages/") + id;
              string vitualp = directoryPath.Replace(@"C:\Users\NET DEVELOPMENT\Desktop\working\AgentWeb", "~").Replace(@"\", "/");
              string filepath = vitualp + "/";
              if (!Directory.Exists(filepath))
              {

                  Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filepath + "/"));
                  fuProducImage.SaveAs(HttpContext.Current.Server.MapPath(filepath + "/" + last + ext1));
                  temp = filepath  + last + ext1;
              }
              else
              {
                  fuProducImage.SaveAs(Server.MapPath(filepath + last + ext1));
                  temp = filepath + last + ext1;
              }
              //fuProducImage.SaveAs(MapPath("~/AgentImages/" + fuProducImage.FileName));
          }
      }
      dmp.Product = txtProduct.Text;
      dmp.AgentMasterID = Convert.ToInt32(Session["AgentMasterID"]);
      dmp.Description = txtDescription.Text;
      dmp.Features = txtFeatures.Text;
      dmp.ProductMRP = Convert.ToDecimal(txtMRP.Text);
      dmp.ProductProfit = Convert.ToDecimal(txtProfit.Text);
      dmp.ProductSellingPrice = Convert.ToDecimal(txtSellingprice.Text);
      dmp.ProductImage = temp;
      //if (!IsPostBack)
      //{
          dmp.InsertAgentproduct();
      //}
      lblPimage.Visible = true;
      lblPimage.Text = "Data Inserted Sucesfully";
      displayproduct();

      txtProduct.Text = "";
      txtProfit.Text = "";
      txtMRP.Text = "";
      txtFeatures.Text = "";
      txtSellingprice.Text = "";
      txtDescription.Text = "";
  }

  bool CheckFileType()
  {
      string ext1 = Path.GetExtension(fuProducImage.FileName);

      switch (ext1.ToLower())
      {
          case ".png":
              return true;
          case ".jpg":
              return true;
          case ".jpeg":
              return true;
          default:
              return false;
      }
  }


i want to insert into server so that after publishing the website it should display it in gridview!! pls help
Posted
Updated 15-Jan-15 21:46pm
v2
Comments
Sinisa Hajnal 16-Jan-15 3:53am    
In varchar 100 you cannot. It would have to be an image with less then 100 bytes.

Assuming you have bigger field, you should convert the image to base64 string and use that as the image. As for detailed code, you have to search that for your self, there are plenty of examples and this is not free code service. You have to do your own work.
V. 16-Jan-15 3:55am    
I think the OP wants to upload the image to a folder and store the path in the database.
V. 16-Jan-15 3:57am    
You can upload the image to the server in a folder and save the path/filename in the varchar(100) field. You can retrieve the correct file based on the saved path.
Member 14552976 27-Jun-20 14:02pm    
can you help me how to do that??
V. 29-Jun-20 2:34am    
try to google first, this is pretty basic stuff.
When stuck post a new question here on codeproject with what you tried and where it goes wrong, than people are glad to try to help you out.

Pretty much, you can't - 100 characters is very small for an image: even if it was 100 bytes, that would only be 5 by 5 pixels at 32bit colour!

What you can do (and probably what you are supposed to do) is put the path to the image file in the database (though 100 chars is a bit short for a "full path" it will be enough for most of them) and then access the image data via the path when you need to display it.
 
Share this answer
 
Comments
Parth Mashroo 16-Jan-15 5:49am    
sir iam not saving image i am saving image path you can see in my code but the problem is that the path that is stored is the path of my pc but i want to save it to a virtual path of my server i.e npay.in/agent/agentimages

i want to save it in that virtual path if i write it in this code it will create a folder with npay.inand sove it there but i want to save it in server!!
thanks for your reply pls help me on that!!!
OriginalGriff 16-Jan-15 6:32am    
If that code is from your website, then it runs on the server: and fuProducImage looks like a FileUpload control so that backs that up.
So the file will be saved on the server, not on a "local" drive on "your PC".
So just assign an absolute folder under your website root (I would use "Images/User" or similar) and use Server.MapPath to create a "writable" path to the file:

string path = Path.Combine(Server.MapPath("~/agent/agentimages/"), eventualNameOfFile);

Me, I use GUIDs as the file name, and let the DB row sort out the original name and who it "belongs to" rather than messing with ID's, particularly on a website.
Parth Mashroo 17-Jan-15 0:09am    
Yes sir thank you for your reply i did that and it worked thank you for your reply!!
OriginalGriff 17-Jan-15 3:58am    
You're welcome!
Parth Mashroo 17-Jan-15 5:03am    
Sir the image is getting displayed but i have one more doubt about displaying those images in another website using same table in another place or another pc then how to do it because only image path is saved and that image is somewhere else then how someone will be able to use that image from another pc. pls help!!
You will need a different type of column to store table data (BLOB type) - http://msdn.microsoft.com/en-us/library/bb895234.aspx[^].

Given that the length is only 100 characters, one solution here would be to store the path of the file instead of the file itself.
 
Share this answer
 
Comments
Parth Mashroo 16-Jan-15 5:02am    
sir i am saving only path of the file but the problem is that i am saving only path that exists in my pc but actually i want to save it to a virtual path in server so how to do that!!
pls help me on that!!
thanks for reply!

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