472,368 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,368 software developers and data experts.

Dealing with a null BLOB field

I could use some help dealing with null blobs. I'm
returning a transaction from an Image BLOB field in SQL
Server 2000 using C#. If the transaction exists the value
is returned with out trouble, but because the ID can exist
without having a value in the Image column the returned
value is NULL and the code can't handle it and I receive
this error when the Stored Procedure's value is returned
(the line of code is marked with "**HERE**":

Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and where
it originated in the code.

Exception Details:
System.Web.Services.Protocols.SoapException:
System.Web.Services.Protocols.SoapException: Server was
unable to process request. --->
System.InvalidCastException: Specified cast is not valid.
at
CareVu.WebServices.TransactionServicesm.Transactio nServices
..GetRejectedTransaction(Int32 txnId) in
c:\inetpub\wwwroot\web\transactionservices.asmx.cs :line
475 --- End of inner exception stack trace ---
Here is the code in the transactionservices.asmx.cs file:
/// <summary>
/// Retrieves an XML representation of a
transaction given its ID
/// </summary>
/// <param name="txnId">The transaction
ID</param>
/// <returns>String (xml)</returns>
[WebMethod]
public string GetRejectedTransaction(int
txnId)
{
SqlConnection conn = new
SqlConnection();

try
{
conn.ConnectionString =
System.Configuration.ConfigurationSettings.AppSett ings
["ConnectionString"];
SqlParameter[] parms = new
SqlParameter[1];
parms[0] = new SqlParameter
("@txnId", SqlDbType.Int);
parms[0].Value = txnId;
**HERE** byte[] weakBlob = (byte[])
SqlHelper.ExecuteScalar(conn,
CommandType.StoredProcedure, "GetRejectedTransaction",
parms);

if (weakBlob != null)
{

System.Text.StringBuilder contents = new
System.Text.StringBuilder(weakBlob.Length - 1);

for(int i = 0; i <
weakBlob.Length; i++)
{

contents.Append(System.Convert.ToChar(weakBlob
[i]));
}

return
contents.ToString();
}
else
{
return "";
}
}
catch(Exception exc)
{
throw exc;
}
finally
{
if (conn.State !=
ConnectionState.Closed)
{
conn.Close();
}
}
}
And here is the Stored Procedure:

CREATE proc dbo.GetRejectedTransaction
(
@txnId int
)
as
set nocount on
select
WeakBlob
from Transactions
where ID = @txnId
GO

Any help would be appreciated.

Thanks,
Matt

Nov 15 '05 #1
1 3384
I found a way to do it. Since the ID being sent in to the
SP will be in the table whether the BLOB is there or not,
I changed from using a byte[] to an object and then
converting to a byte[] after I knew if was not null:
[WebMethod]
public string GetRejectedTransaction(int
txnId)
{
SqlConnection conn = new
SqlConnection();

try
{
conn.ConnectionString =
System.Configuration.ConfigurationSettings.AppSett ings
["ConnectionString"];
SqlParameter[] parms = new
SqlParameter[1];
parms[0] = new SqlParameter
("@txnId", SqlDbType.Int);
parms[0].Value = txnId;
object wBlob = (object)
SqlHelper.ExecuteScalar(conn,
CommandType.StoredProcedure, "GetRejectedTransaction",
parms);

if (wBlob !=
System.DBNull.Value)
{
byte[] weakBlob =
(byte[])wBlob;

System.Text.StringBuilder contents = new
System.Text.StringBuilder(weakBlob.Length - 1);

for(int i = 0; i <
weakBlob.Length; i++)
{

contents.Append(System.Convert.ToChar(weakBlob
[i]));
}

return
contents.ToString();
}
else
{
return "";
}
}
catch(Exception exc)
{
throw exc;
}
finally
{
if (conn.State !=
ConnectionState.Closed)
{
conn.Close();
}
}
}

-----Original Message-----
I could use some help dealing with null blobs. I'm
returning a transaction from an Image BLOB field in SQL
Server 2000 using C#. If the transaction exists the valueis returned with out trouble, but because the ID can existwithout having a value in the Image column the returned
value is NULL and the code can't handle it and I receive
this error when the Stored Procedure's value is returned
(the line of code is marked with "**HERE**":

Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and whereit originated in the code.

Exception Details:
System.Web.Services.Protocols.SoapException:
System.Web.Services.Protocols.SoapException: Server was
unable to process request. --->
System.InvalidCastException: Specified cast is not valid.
at
CareVu.WebServices.TransactionServicesm.Transacti onService s..GetRejectedTransaction(Int32 txnId) in
c:\inetpub\wwwroot\web\transactionservices.asmx.c s:line
475 --- End of inner exception stack trace ---
Here is the code in the transactionservices.asmx.cs file:
/// <summary>
/// Retrieves an XML representation of a
transaction given its ID
/// </summary>
/// <param name="txnId">The transaction
ID</param>
/// <returns>String (xml)</returns>
[WebMethod]
public string GetRejectedTransaction(int
txnId)
{
SqlConnection conn = new
SqlConnection();

try
{
conn.ConnectionString =
System.Configuration.ConfigurationSettings.AppSet tings
["ConnectionString"];
SqlParameter[] parms = new
SqlParameter[1];
parms[0] = new SqlParameter
("@txnId", SqlDbType.Int);
parms[0].Value = txnId;
**HERE** byte[] weakBlob = (byte[])
SqlHelper.ExecuteScalar(conn,
CommandType.StoredProcedure, "GetRejectedTransaction",
parms);

if (weakBlob != null)
{

System.Text.StringBuilder contents = new
System.Text.StringBuilder(weakBlob.Length - 1);

for(int i = 0; i <
weakBlob.Length; i++)
{

contents.Append(System.Convert.ToChar(weakBlob
[i]));
}

return
contents.ToString();
}
else
{
return "";
}
}
catch(Exception exc)
{
throw exc;
}
finally
{
if (conn.State !=
ConnectionState.Closed)
{
conn.Close();
}
}
}
And here is the Stored Procedure:

CREATE proc dbo.GetRejectedTransaction
(
@txnId int
)
as
set nocount on
select
WeakBlob
from Transactions
where ID = @txnId
GO

Any help would be appreciated.

Thanks,
Matt

.

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and...
0
by: qhe | last post by:
Dear All: I'm using MyODBC 3.51.06 and MySQL 4.0.18. My table has a mediumblob field, but I can't update/insert data larger than 64K. The test application is created by VC6 appwizard, here is...
1
by: Greg.Harabedian | last post by:
I'll start off by saying I am using MySQL v4.0 and my question is...how do I get mysqldump to dump the actual binary values store in a blob? Here is an example: -- Create a test table create...
11
by: Chris Fink | last post by:
I have setup an Oracle table which contains a blob field. How do I insert data into this field using C# and ADO.net?
1
by: yoes | last post by:
Dear all, I am very new in MySQL, I am working on image database project for my research and I have problem how to convert blob field into float in MySQL so that I can calculate the blob field...
0
by: Big George | last post by:
Hello, I'm trying to save a jpg file of 300KB as a BLOB field in an Oracle 10g Database. If I try to call a Stored Procedure, it fails. If I use CommandText with SQL sentence, it success. I...
55
ADezii
by: ADezii | last post by:
Of all the questions asked here at TheScripts, the one which appears with the most frequency relates to the storing of Graphic Images within Access. There are basically three techniques available to...
1
by: rokenbok | last post by:
hi, I have a password field that I need to store in a db2 database. I created a blob field in my table, and I am trying to update it with the following java dal.getPrepared().setBytes(15,...
3
by: anandmms | last post by:
Hello friends, I am having a table Personal_Detail, and in that table i am having some fields like name, id, phone_number etc... and also with two blob fields photo1, photo2(allow null). my...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.