Display image from folder path saved in database in ASP.Net using jQuery Ajax

Jilsoft
 
on Mar 18, 2022 05:21 AM
1090 Views

Code to display Image Using path from database

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 18, 2022 05:50 AM
on Mar 18, 2022 07:05 AM
arjunv
 
on Mar 19, 2022 06:28 AM

Hi 

Please refer below sample.

HTML 

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<img id="imgData" alt="" width="100" height="100" />
<script type="text/javascript">
    $(function () {
        $.ajax({
            type: "POST",
            url: "CS.aspx/GetPath",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                $('#imgData').attr('src', response.d)
            },
            failure: function (response) {
                alert(response.responseText);
            },
            error: function (response) {
                alert(response.responseText);
            }
        });
    });
</script>

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;
using System.Web.Services;

VB.Net

Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Configuration

Code

C#

[WebMethod]
public static string GetPath()
{
    string path = string.Empty;
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "SELECT TOP 1 Filepath FROM tblfiles";
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            con.Open();
            path = Convert.ToString(cmd.ExecuteScalar());
            con.Close();
        }
    }

    return path;
}

VB.Net

<WebMethod>
Public Shared Function GetPath() As String
    Dim path As String = String.Empty
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim query As String = "SELECT TOP 1 Filepath FROM tblfiles"
    Using con As SqlConnection = New SqlConnection(conString)
        Using cmd As SqlCommand = New SqlCommand(query, con)
            con.Open()
            path = Convert.ToString(cmd.ExecuteScalar())
            con.Close()
        End Using
    End Using

    Return path
End Function

Screenshot