mms says:
string sqlQuery = "SELECT * from [MostWanted] WHERE Photo = @FileName";
Here you are selecting all the column name so how can you specify which column value you are using to get by cmd.ExecuteScalar you just need to get the Column name Photo from the database whose file name you are comparing with the uploaded file.
Also you are selecting only image file name by FileUpload1.FileName and in database you have images like ~/MWPhoto/20_7_abc.jpg and ~/MWPhoto/4_pqr.jpg so it will not match with the folder name value from uploaded file may be 20_7_abc.jpg or 4_pqr.jpg. in this case you need to append ~/MWPhoto/ text with your parameter value.
Also if photo value matched as per the where condition then you need to first convert that file by its path to bytes and then you can check for the byte condition for comparison if its similar or not.
Refer the below sample code for your reference and implement it as per your code logic.
Here i am checking upload image by its name with the path from database if it exist with same name then just comparing both files by converting then in to byte and checking there length as well as each length value if its same or different.
SQL
CREATE TABLE FileImages
(ImageId INT IDENTITY(1,1),
[ImagePath] VARCHAR(50),
ImageName VARCHAR(20))
GO
Also inserted some images in folder as well as in Database.
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnCompareImages" Text="Compare Images" runat="server" OnClick="CompareImages" />
<hr />
C#
protected void CompareImages(object sender, EventArgs e)
{
if (this.FileUpload1.HasFile)
{
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
string sqlQuery = "SELECT ImagePath from FileImages WHERE ImagePath = @FileName";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
cmd.Parameters.AddWithValue("@FileName", "~/images/" + Path.GetFileName(this.FileUpload1.PostedFile.FileName));
conn.Open();
string imagepath = Convert.ToString(cmd.ExecuteScalar());
if (!string.IsNullOrEmpty(imagepath))
{
CompareEqualImage(bytes, Server.MapPath(imagepath));
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('File Not found in the table')", true);
}
conn.Close();
}
}
}
}
private void CompareEqualImage(Byte[] bytes, string imagepath)
{
if (File.Exists(imagepath))
{
byte[] bytes2 = System.IO.File.ReadAllBytes(imagepath);
bool equal = true;
if (bytes2 != null)
{
if (bytes.Length == bytes2.Length)
{
for (int i = 0; i < bytes.Length; i++)
{
if (bytes[i] != bytes2[i])
{
equal = false;
break;
}
}
}
else
{
equal = false;
}
if (equal)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Equal Images')", true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Images are not equal')", true);
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('File Not found in the table')", true);
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('File Not found in the table')", true);
}
}
VB.Net
Protected Sub CompareImages(ByVal sender As Object, ByVal e As EventArgs)
If Me.FileUpload1.HasFile Then
Dim fs As Stream = FileUpload1.PostedFile.InputStream
Dim br As BinaryReader = New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Int32))
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(constr)
Dim sqlQuery As String = "SELECT ImagePath from FileImages WHERE ImagePath = @FileName"
Using cmd As SqlCommand = New SqlCommand(sqlQuery, conn)
cmd.Parameters.AddWithValue("@FileName", "~/images/" & Path.GetFileName(Me.FileUpload1.PostedFile.FileName))
conn.Open()
Dim imagepath As String = Convert.ToString(cmd.ExecuteScalar())
If Not String.IsNullOrEmpty(imagepath) Then
CompareEqualImage(bytes, Server.MapPath(imagepath))
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('File Not found in the table')", True)
End If
conn.Close()
End Using
End Using
End If
End Sub
Private Sub CompareEqualImage(ByVal bytes As Byte(), ByVal imagepath As String)
If File.Exists(imagepath) Then
Dim bytes2 As Byte() = System.IO.File.ReadAllBytes(imagepath)
Dim equal As Boolean = True
If bytes2 IsNot Nothing Then
If bytes.Length = bytes2.Length Then
For i As Integer = 0 To bytes.Length - 1
If bytes(i) <> bytes2(i) Then
equal = False
Exit For
End If
Next
Else
equal = False
End If
If equal Then
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Equal Images')", True)
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('Images are not equal')", True)
End If
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('File Not found in the table')", True)
End If
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('File Not found in the table')", True)
End If
End Sub
Screenshot