I have a handler that works fine in C# but I can't get it to work in VB. I don't know if maybe I just did something wrong in the translation? Can anyone look at it and see if anything jumps out? The site I'm working on currently, for various reasons needs
to be in VB so I'm hoping to be able to get this code to work properly.
The connection string in the web.config that the handler is referencing is exactly the same in both C and VB codes.
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string strConnection = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
SqlConnection conn = new SqlConnection(strConnection);
conn.Open();
string sql = "SELECT Image FROM [Gallery] " + "WHERE [ImageID]=@ImageID";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@ImageID", SqlDbType.Int).Value = context.Request.QueryString["id"];
cmd.Prepare();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
context.Response.ContentType = dr["Image"].ToString();
context.Response.BinaryWrite((byte[])dr["Image"]);
}
conn.Close();
}
public bool IsReusable {
get {
return false;
}
}
}
Imports System
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class Handler
Implements IHttpHandler
Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim strConnection As String = ConfigurationManager.ConnectionStrings("ConnectionString2").ToString()
Dim conn As New SqlConnection(strConnection)
conn.Open()
Dim sql As String = "SELECT Image FROM [Gallery] " + "WHERE [ImageID]=@ImageID"
Dim cmd As New SqlCommand(sql, conn)
cmd.Parameters.Add("@ImageID", SqlDbType.Int).Value = context.Request.QueryString("id")
cmd.Prepare()
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.Read() Then
context.Response.ContentType = dr("Image").ToString()
context.Response.BinaryWrite(DirectCast(dr("Image"), Byte()))
End If
conn.Close()
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Well, it's not really giving an error. In C it shows the image properly with no trouble. In VB is just simply shows an X. The handler connection string shows "connectionString" in one and "connectionString2" in the other, but the actual string is the same.
I just wonder if I messed something up in the VB code because it won't show the images and I don't understand why it does in C#.
Okay, update on this. I've got it to work using the While/End While statement. For some reason it only works if the page is in the root of the web. If I have the page in a subdirectory the handler apparently won't work or the page isn't communicating with
it. Any thoughts on that problem?
Guess I answered my own question. The handler has to be in the same directory as the page. I moved the handler to the folder I wanted the page to be in and it worked fine. Thanks for the advice on the While statement change, that fixed my issue!
AsPxFrK
Member
232 Points
93 Posts
Select Image works in C#, but not in VB
Feb 25, 2012 04:42 PM|LINK
I have a handler that works fine in C# but I can't get it to work in VB. I don't know if maybe I just did something wrong in the translation? Can anyone look at it and see if anything jumps out? The site I'm working on currently, for various reasons needs to be in VB so I'm hoping to be able to get this code to work properly.
The connection string in the web.config that the handler is referencing is exactly the same in both C and VB codes.
using System; using System.Web; using System.Data; using System.Data.SqlClient; using System.Configuration; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { string strConnection = ConfigurationManager.ConnectionStrings["connectionString"].ToString(); SqlConnection conn = new SqlConnection(strConnection); conn.Open(); string sql = "SELECT Image FROM [Gallery] " + "WHERE [ImageID]=@ImageID"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.Add("@ImageID", SqlDbType.Int).Value = context.Request.QueryString["id"]; cmd.Prepare(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { context.Response.ContentType = dr["Image"].ToString(); context.Response.BinaryWrite((byte[])dr["Image"]); } conn.Close(); } public bool IsReusable { get { return false; } } }Imports System Imports System.Web Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Public Class Handler Implements IHttpHandler Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest Dim strConnection As String = ConfigurationManager.ConnectionStrings("ConnectionString2").ToString() Dim conn As New SqlConnection(strConnection) conn.Open() Dim sql As String = "SELECT Image FROM [Gallery] " + "WHERE [ImageID]=@ImageID" Dim cmd As New SqlCommand(sql, conn) cmd.Parameters.Add("@ImageID", SqlDbType.Int).Value = context.Request.QueryString("id") cmd.Prepare() Dim dr As SqlDataReader = cmd.ExecuteReader() If dr.Read() Then context.Response.ContentType = dr("Image").ToString() context.Response.BinaryWrite(DirectCast(dr("Image"), Byte())) End If conn.Close() End Sub Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable Get Return False End Get End Property End Classpankajgoyal_...
Member
501 Points
166 Posts
Re: Select Image works in C#, but not in VB
Feb 25, 2012 05:25 PM|LINK
what error exactly you are getting?
AsPxFrK
Member
232 Points
93 Posts
Re: Select Image works in C#, but not in VB
Feb 25, 2012 05:34 PM|LINK
Well, it's not really giving an error. In C it shows the image properly with no trouble. In VB is just simply shows an X. The handler connection string shows "connectionString" in one and "connectionString2" in the other, but the actual string is the same. I just wonder if I messed something up in the VB code because it won't show the images and I don't understand why it does in C#.
arifcse12
Contributor
4600 Points
755 Posts
Re: Select Image works in C#, but not in VB
Feb 26, 2012 03:42 AM|LINK
What happens when you use
While dr.Read()
// Do the stuff..
End While
Regards
Please do not forget to click 'Mark as Answer' if this answer helps.
AsPxFrK
Member
232 Points
93 Posts
Re: Select Image works in C#, but not in VB
Feb 26, 2012 06:03 PM|LINK
It still produces the same result.
AsPxFrK
Member
232 Points
93 Posts
Re: Select Image works in C#, but not in VB
Feb 26, 2012 07:26 PM|LINK
Okay, update on this. I've got it to work using the While/End While statement. For some reason it only works if the page is in the root of the web. If I have the page in a subdirectory the handler apparently won't work or the page isn't communicating with it. Any thoughts on that problem?
The tag I'm using is
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageId", "Handler.ashx?ID={0}")%>' Height="225px" Width="225px" />AsPxFrK
Member
232 Points
93 Posts
Re: Select Image works in C#, but not in VB
Feb 26, 2012 07:29 PM|LINK
Guess I answered my own question. The handler has to be in the same directory as the page. I moved the handler to the folder I wanted the page to be in and it worked fine. Thanks for the advice on the While statement change, that fixed my issue!