473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 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 3578
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.