How to display image from file path while retrieving from database in asp.net?












0














I need to display images from the file path and display the images on the image box. I am adding the images to a folder inside the project directory while adding them to the database. I need to display them back on the image box while retrieving from database. I have tried several ways referring the other links but I couldn't find where I am going wrong.



I have used tag and the to load the file. The images which I load using the FileUpload are getting displayed on the image box. I have added a GridView and binded the table values which did not work as expected.



following is my code which i use to read the data from sql.



            if (reader.Read())
{
if (reader.IsDBNull(0))
{

}
else
{
result = (reader["Answer_Type"].ToString()).Trim();
if (result.Equals("Image"))
{

string getPhysicalPath = (reader["IMG_Answer_A"].ToString());
string getvirtualPath = getPhysicalPath.Replace("D:\XXX\YYY\ZZZ\project.Web", "~");

Image1.ImageUrl = String.Format(Server.MapPath(getvirtualPath).ToString());
//GridView1.DataSource = dt;
//GridView1.DataBind();
}
}
}


following is the code segment which is used in the .aspx file



            <div id="imageB">
B. <asp:Image ID="Image2" runat="server" ImageUrl="..ProjectNameImagesblankImage.png" Width="150px"/>
&nbsp;
<div style="margin-left:15%">
<asp:FileUpload ID="FileUpload2" runat="server" Width="83px" onchange="ImagePreviewB(this)" />
</div>
</div>


When I debug my c# code I am getting the file path from the database. but it is not getting displayed on the image box. Following Image shows how it looks when the data is loaded.



enter image description here



Please let me know where I am going wrong!










share|improve this question






















  • What kind of string contained inside Image1.ImageUrl, getPhysicalPath and getvirtualPath? Probably you're supplied virtual path of the image in wrong way.
    – Tetsuya Yamamoto
    Dec 28 '18 at 8:37








  • 2




    Create an HttpHandler that will be responsible for rendering images. This link will help you - aspsnippets.com/Articles/…
    – Ankush Jain
    Dec 28 '18 at 8:38












  • @TetsuyaYamamoto getPhysicalPath returns the full path (with forward slash ) and I have used the replace function to trim it to the physycal path. Ex: "~xxyy.jpg".
    – Coder
    Dec 28 '18 at 9:05






  • 1




    You're using wrong path format. Try this instead: string getvirtualPath = getPhysicalPath.Replace(@"", "/").Replace("D:\XXX\YYY\ZZZ\project.Web", "~").
    – Tetsuya Yamamoto
    Dec 28 '18 at 9:08






  • 1




    if you debug step by step, you can find whats going wrong - also if you check what is render on the page, you also can find your error - its maybe be only the slash - its maybe a wrong path.
    – Aristos
    Dec 28 '18 at 9:11
















0














I need to display images from the file path and display the images on the image box. I am adding the images to a folder inside the project directory while adding them to the database. I need to display them back on the image box while retrieving from database. I have tried several ways referring the other links but I couldn't find where I am going wrong.



I have used tag and the to load the file. The images which I load using the FileUpload are getting displayed on the image box. I have added a GridView and binded the table values which did not work as expected.



following is my code which i use to read the data from sql.



            if (reader.Read())
{
if (reader.IsDBNull(0))
{

}
else
{
result = (reader["Answer_Type"].ToString()).Trim();
if (result.Equals("Image"))
{

string getPhysicalPath = (reader["IMG_Answer_A"].ToString());
string getvirtualPath = getPhysicalPath.Replace("D:\XXX\YYY\ZZZ\project.Web", "~");

Image1.ImageUrl = String.Format(Server.MapPath(getvirtualPath).ToString());
//GridView1.DataSource = dt;
//GridView1.DataBind();
}
}
}


following is the code segment which is used in the .aspx file



            <div id="imageB">
B. <asp:Image ID="Image2" runat="server" ImageUrl="..ProjectNameImagesblankImage.png" Width="150px"/>
&nbsp;
<div style="margin-left:15%">
<asp:FileUpload ID="FileUpload2" runat="server" Width="83px" onchange="ImagePreviewB(this)" />
</div>
</div>


When I debug my c# code I am getting the file path from the database. but it is not getting displayed on the image box. Following Image shows how it looks when the data is loaded.



enter image description here



Please let me know where I am going wrong!










share|improve this question






















  • What kind of string contained inside Image1.ImageUrl, getPhysicalPath and getvirtualPath? Probably you're supplied virtual path of the image in wrong way.
    – Tetsuya Yamamoto
    Dec 28 '18 at 8:37








  • 2




    Create an HttpHandler that will be responsible for rendering images. This link will help you - aspsnippets.com/Articles/…
    – Ankush Jain
    Dec 28 '18 at 8:38












  • @TetsuyaYamamoto getPhysicalPath returns the full path (with forward slash ) and I have used the replace function to trim it to the physycal path. Ex: "~xxyy.jpg".
    – Coder
    Dec 28 '18 at 9:05






  • 1




    You're using wrong path format. Try this instead: string getvirtualPath = getPhysicalPath.Replace(@"", "/").Replace("D:\XXX\YYY\ZZZ\project.Web", "~").
    – Tetsuya Yamamoto
    Dec 28 '18 at 9:08






  • 1




    if you debug step by step, you can find whats going wrong - also if you check what is render on the page, you also can find your error - its maybe be only the slash - its maybe a wrong path.
    – Aristos
    Dec 28 '18 at 9:11














0












0








0







I need to display images from the file path and display the images on the image box. I am adding the images to a folder inside the project directory while adding them to the database. I need to display them back on the image box while retrieving from database. I have tried several ways referring the other links but I couldn't find where I am going wrong.



I have used tag and the to load the file. The images which I load using the FileUpload are getting displayed on the image box. I have added a GridView and binded the table values which did not work as expected.



following is my code which i use to read the data from sql.



            if (reader.Read())
{
if (reader.IsDBNull(0))
{

}
else
{
result = (reader["Answer_Type"].ToString()).Trim();
if (result.Equals("Image"))
{

string getPhysicalPath = (reader["IMG_Answer_A"].ToString());
string getvirtualPath = getPhysicalPath.Replace("D:\XXX\YYY\ZZZ\project.Web", "~");

Image1.ImageUrl = String.Format(Server.MapPath(getvirtualPath).ToString());
//GridView1.DataSource = dt;
//GridView1.DataBind();
}
}
}


following is the code segment which is used in the .aspx file



            <div id="imageB">
B. <asp:Image ID="Image2" runat="server" ImageUrl="..ProjectNameImagesblankImage.png" Width="150px"/>
&nbsp;
<div style="margin-left:15%">
<asp:FileUpload ID="FileUpload2" runat="server" Width="83px" onchange="ImagePreviewB(this)" />
</div>
</div>


When I debug my c# code I am getting the file path from the database. but it is not getting displayed on the image box. Following Image shows how it looks when the data is loaded.



enter image description here



Please let me know where I am going wrong!










share|improve this question













I need to display images from the file path and display the images on the image box. I am adding the images to a folder inside the project directory while adding them to the database. I need to display them back on the image box while retrieving from database. I have tried several ways referring the other links but I couldn't find where I am going wrong.



I have used tag and the to load the file. The images which I load using the FileUpload are getting displayed on the image box. I have added a GridView and binded the table values which did not work as expected.



following is my code which i use to read the data from sql.



            if (reader.Read())
{
if (reader.IsDBNull(0))
{

}
else
{
result = (reader["Answer_Type"].ToString()).Trim();
if (result.Equals("Image"))
{

string getPhysicalPath = (reader["IMG_Answer_A"].ToString());
string getvirtualPath = getPhysicalPath.Replace("D:\XXX\YYY\ZZZ\project.Web", "~");

Image1.ImageUrl = String.Format(Server.MapPath(getvirtualPath).ToString());
//GridView1.DataSource = dt;
//GridView1.DataBind();
}
}
}


following is the code segment which is used in the .aspx file



            <div id="imageB">
B. <asp:Image ID="Image2" runat="server" ImageUrl="..ProjectNameImagesblankImage.png" Width="150px"/>
&nbsp;
<div style="margin-left:15%">
<asp:FileUpload ID="FileUpload2" runat="server" Width="83px" onchange="ImagePreviewB(this)" />
</div>
</div>


When I debug my c# code I am getting the file path from the database. but it is not getting displayed on the image box. Following Image shows how it looks when the data is loaded.



enter image description here



Please let me know where I am going wrong!







c# asp.net sql-server image imageurl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '18 at 8:31









CoderCoder

1810




1810












  • What kind of string contained inside Image1.ImageUrl, getPhysicalPath and getvirtualPath? Probably you're supplied virtual path of the image in wrong way.
    – Tetsuya Yamamoto
    Dec 28 '18 at 8:37








  • 2




    Create an HttpHandler that will be responsible for rendering images. This link will help you - aspsnippets.com/Articles/…
    – Ankush Jain
    Dec 28 '18 at 8:38












  • @TetsuyaYamamoto getPhysicalPath returns the full path (with forward slash ) and I have used the replace function to trim it to the physycal path. Ex: "~xxyy.jpg".
    – Coder
    Dec 28 '18 at 9:05






  • 1




    You're using wrong path format. Try this instead: string getvirtualPath = getPhysicalPath.Replace(@"", "/").Replace("D:\XXX\YYY\ZZZ\project.Web", "~").
    – Tetsuya Yamamoto
    Dec 28 '18 at 9:08






  • 1




    if you debug step by step, you can find whats going wrong - also if you check what is render on the page, you also can find your error - its maybe be only the slash - its maybe a wrong path.
    – Aristos
    Dec 28 '18 at 9:11


















  • What kind of string contained inside Image1.ImageUrl, getPhysicalPath and getvirtualPath? Probably you're supplied virtual path of the image in wrong way.
    – Tetsuya Yamamoto
    Dec 28 '18 at 8:37








  • 2




    Create an HttpHandler that will be responsible for rendering images. This link will help you - aspsnippets.com/Articles/…
    – Ankush Jain
    Dec 28 '18 at 8:38












  • @TetsuyaYamamoto getPhysicalPath returns the full path (with forward slash ) and I have used the replace function to trim it to the physycal path. Ex: "~xxyy.jpg".
    – Coder
    Dec 28 '18 at 9:05






  • 1




    You're using wrong path format. Try this instead: string getvirtualPath = getPhysicalPath.Replace(@"", "/").Replace("D:\XXX\YYY\ZZZ\project.Web", "~").
    – Tetsuya Yamamoto
    Dec 28 '18 at 9:08






  • 1




    if you debug step by step, you can find whats going wrong - also if you check what is render on the page, you also can find your error - its maybe be only the slash - its maybe a wrong path.
    – Aristos
    Dec 28 '18 at 9:11
















What kind of string contained inside Image1.ImageUrl, getPhysicalPath and getvirtualPath? Probably you're supplied virtual path of the image in wrong way.
– Tetsuya Yamamoto
Dec 28 '18 at 8:37






What kind of string contained inside Image1.ImageUrl, getPhysicalPath and getvirtualPath? Probably you're supplied virtual path of the image in wrong way.
– Tetsuya Yamamoto
Dec 28 '18 at 8:37






2




2




Create an HttpHandler that will be responsible for rendering images. This link will help you - aspsnippets.com/Articles/…
– Ankush Jain
Dec 28 '18 at 8:38






Create an HttpHandler that will be responsible for rendering images. This link will help you - aspsnippets.com/Articles/…
– Ankush Jain
Dec 28 '18 at 8:38














@TetsuyaYamamoto getPhysicalPath returns the full path (with forward slash ) and I have used the replace function to trim it to the physycal path. Ex: "~xxyy.jpg".
– Coder
Dec 28 '18 at 9:05




@TetsuyaYamamoto getPhysicalPath returns the full path (with forward slash ) and I have used the replace function to trim it to the physycal path. Ex: "~xxyy.jpg".
– Coder
Dec 28 '18 at 9:05




1




1




You're using wrong path format. Try this instead: string getvirtualPath = getPhysicalPath.Replace(@"", "/").Replace("D:\XXX\YYY\ZZZ\project.Web", "~").
– Tetsuya Yamamoto
Dec 28 '18 at 9:08




You're using wrong path format. Try this instead: string getvirtualPath = getPhysicalPath.Replace(@"", "/").Replace("D:\XXX\YYY\ZZZ\project.Web", "~").
– Tetsuya Yamamoto
Dec 28 '18 at 9:08




1




1




if you debug step by step, you can find whats going wrong - also if you check what is render on the page, you also can find your error - its maybe be only the slash - its maybe a wrong path.
– Aristos
Dec 28 '18 at 9:11




if you debug step by step, you can find whats going wrong - also if you check what is render on the page, you also can find your error - its maybe be only the slash - its maybe a wrong path.
– Aristos
Dec 28 '18 at 9:11












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53955643%2fhow-to-display-image-from-file-path-while-retrieving-from-database-in-asp-net%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53955643%2fhow-to-display-image-from-file-path-while-retrieving-from-database-in-asp-net%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Can't read property showImagePicker of undefined in react native iOS

Cannot access a disposed object : DataContext