blob

H

Hrvoje Voda

I wrote this code to save a blob data into database.

What is wrong?

public ArrayList SetProfile(byte [] Profile)

{

ArrayList arrayProfile = new ArrayList ();


int bufferSize = 100; .

byte[] outbyte = new byte[bufferSize];



SqlConnection conn = null;

SqlDataReader rdr = null;


if (_UserName !=null)

{

try

{


conn = new

SqlConnection(sConnString);

conn.Open() ;

SqlCommand cmd = new SqlCommand("SetProfile", conn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add ("@UserName",_UserName);

cmd.Parameters.Add("@Profile", SqlDbType.Image, Profile.Length).Value =
Profile;

rdr = cmd.ExecuteReader ();



}

finally

{

if (conn != null)

{

conn.Close();

}

if (rdr != null)

{

rdr.Close();

}

}

}

return arrayProfile;

}

Hrcko
 
H

Hans Kesting

Hrvoje said:
I wrote this code to save a blob data into database.

What is wrong?

You might give some hints, as:
- what did you want to do / what did you expect?
- what did/didn't happen instead?
public ArrayList SetProfile(byte [] Profile)
{
ArrayList arrayProfile = new ArrayList ();

why do you need this? You never fill it!
int bufferSize = 100; .
byte[] outbyte = new byte[bufferSize];

why do you need these? never used!
SqlConnection conn = null;
SqlDataReader rdr = null;

if (_UserName !=null)
{
try
{
conn = new SqlConnection(sConnString);
conn.Open() ;
SqlCommand cmd = new SqlCommand("SetProfile", conn);

what does SetProfile do? just store something or do you expect a return?
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add ("@UserName",_UserName);
cmd.Parameters.Add("@Profile", SqlDbType.Image, Profile.Length).Value = Profile;
rdr = cmd.ExecuteReader ();

do you really need a reader here? (read: do you expect a return dataset?)
}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}

return arrayProfile;

will be an empty ArrayList.


Hans Kesting
 
H

Hrvoje Voda

I need to save a blob data into database with SetProfile store procedure.
I send to parameters Profile (witch is a blob variable) and a UserID to know
where to save that Blob.
When I run it I get an error on rdr : Overloaded.
This arrayProfile is a mistake.


Hans Kesting said:
Hrvoje said:
I wrote this code to save a blob data into database.

What is wrong?

You might give some hints, as:
- what did you want to do / what did you expect?
- what did/didn't happen instead?
public ArrayList SetProfile(byte [] Profile)
{
ArrayList arrayProfile = new ArrayList ();

why do you need this? You never fill it!
int bufferSize = 100; .
byte[] outbyte = new byte[bufferSize];

why do you need these? never used!
SqlConnection conn = null;
SqlDataReader rdr = null;

if (_UserName !=null)
{
try
{
conn = new SqlConnection(sConnString);
conn.Open() ;
SqlCommand cmd = new SqlCommand("SetProfile", conn);

what does SetProfile do? just store something or do you expect a return?
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add ("@UserName",_UserName);
cmd.Parameters.Add("@Profile", SqlDbType.Image, Profile.Length).Value = Profile;
rdr = cmd.ExecuteReader ();

do you really need a reader here? (read: do you expect a return dataset?)
}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}

return arrayProfile;

will be an empty ArrayList.


Hans Kesting
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

What is the error you are getting?

Why you use & return an empty arraylist?

or you did not post the entire code?

anyhow, this is the code I use for upload an Image to the DB

FileStream file = new FileStream( physicalname, FileMode.Open);
byte[] buff = new byte [ file.Length];
file.Read(buff, 0, Convert.ToInt32(file.Length));
file.Close();


SqlParameter param = com.Parameters.Add("@data", SqlDbType.Image);
param.Value = buff;


The SP is declared as :


PROCEDURE SaveDocument
(
@data IMAGE ,


and the datatype of the column is of course IMAGE


HTH,
 
H

Hans Kesting

Hrvoje said:
I need to save a blob data into database with SetProfile store
procedure. I send to parameters Profile (witch is a blob variable) and a UserID
to know where to save that Blob.
When I run it I get an error on rdr : Overloaded.
This arrayProfile is a mistake.

So, you can change the returntype of your method to "void".
Also, you should use cmd.ExecuteScalar instead of ExecuteReader.
Reader will expect to see a returned rowset, which you don't give.

Did something arrive in the database?

Hans Kesting

Hans Kesting said:
Hrvoje said:
I wrote this code to save a blob data into database.

What is wrong?

You might give some hints, as:
- what did you want to do / what did you expect?
- what did/didn't happen instead?
public ArrayList SetProfile(byte [] Profile)
{
ArrayList arrayProfile = new ArrayList ();

why do you need this? You never fill it!
int bufferSize = 100; .
byte[] outbyte = new byte[bufferSize];

why do you need these? never used!
SqlConnection conn = null;
SqlDataReader rdr = null;

if (_UserName !=null)
{
try
{
conn = new SqlConnection(sConnString);
conn.Open() ;
SqlCommand cmd = new SqlCommand("SetProfile", conn);

what does SetProfile do? just store something or do you expect a
return?
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add ("@UserName",_UserName);
cmd.Parameters.Add("@Profile", SqlDbType.Image,
Profile.Length).Value = Profile;
rdr = cmd.ExecuteReader ();

do you really need a reader here? (read: do you expect a return
dataset?)
}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}

return arrayProfile;

will be an empty ArrayList.


Hans Kesting
 
H

Hrvoje Voda

I get an error on rdr.ExecuteScalar...




Hans Kesting said:
Hrvoje said:
I need to save a blob data into database with SetProfile store
procedure. I send to parameters Profile (witch is a blob variable) and a
UserID
to know where to save that Blob.
When I run it I get an error on rdr : Overloaded.
This arrayProfile is a mistake.

So, you can change the returntype of your method to "void".
Also, you should use cmd.ExecuteScalar instead of ExecuteReader.
Reader will expect to see a returned rowset, which you don't give.

Did something arrive in the database?

Hans Kesting

Hans Kesting said:
Hrvoje Voda wrote:
I wrote this code to save a blob data into database.

What is wrong?

You might give some hints, as:
- what did you want to do / what did you expect?
- what did/didn't happen instead?


public ArrayList SetProfile(byte [] Profile)
{
ArrayList arrayProfile = new ArrayList ();

why do you need this? You never fill it!


int bufferSize = 100; .
byte[] outbyte = new byte[bufferSize];

why do you need these? never used!


SqlConnection conn = null;
SqlDataReader rdr = null;

if (_UserName !=null)
{
try
{
conn = new SqlConnection(sConnString);
conn.Open() ;
SqlCommand cmd = new SqlCommand("SetProfile", conn);

what does SetProfile do? just store something or do you expect a
return?
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add ("@UserName",_UserName);
cmd.Parameters.Add("@Profile", SqlDbType.Image,
Profile.Length).Value
= Profile;
rdr = cmd.ExecuteReader ();

do you really need a reader here? (read: do you expect a return
dataset?)

}
finally
{
if (conn != null)
{
conn.Close();
}
if (rdr != null)
{
rdr.Close();
}
}
}

return arrayProfile;

will be an empty ArrayList.


}

Hrcko


Hans Kesting
 
H

Hrvoje Voda

I manage to save data into database, but how to read this data for example
into listBox ?

Hrcko

Ignacio Machin ( .NET/ C# MVP ) said:
hi,

What is the error you are getting?

Why you use & return an empty arraylist?

or you did not post the entire code?

anyhow, this is the code I use for upload an Image to the DB

FileStream file = new FileStream( physicalname, FileMode.Open);
byte[] buff = new byte [ file.Length];
file.Read(buff, 0, Convert.ToInt32(file.Length));
file.Close();


SqlParameter param = com.Parameters.Add("@data", SqlDbType.Image);
param.Value = buff;


The SP is declared as :


PROCEDURE SaveDocument
(
@data IMAGE ,


and the datatype of the column is of course IMAGE


HTH,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Hrvoje Voda said:
I wrote this code to save a blob data into database.

What is wrong?

public ArrayList SetProfile(byte [] Profile)

{

ArrayList arrayProfile = new ArrayList ();


int bufferSize = 100; .

byte[] outbyte = new byte[bufferSize];



SqlConnection conn = null;

SqlDataReader rdr = null;


if (_UserName !=null)

{

try

{


conn = new

SqlConnection(sConnString);

conn.Open() ;

SqlCommand cmd = new SqlCommand("SetProfile", conn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add ("@UserName",_UserName);

cmd.Parameters.Add("@Profile", SqlDbType.Image, Profile.Length).Value =
Profile;

rdr = cmd.ExecuteReader ();



}

finally

{

if (conn != null)

{

conn.Close();

}

if (rdr != null)

{

rdr.Close();

}

}

}

return arrayProfile;

}

Hrcko
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

Hi,

This is the code to load an Image from a DB, it's up to you what to do with
the byte[], in the code I create a file with it.

look that I store different kind of files, ( this comes from a web app )
therefore after I extract them I have to generate a GUID as the name but I
keep the original extension !!!



SqlCommand cmd = new SqlCommand();
cmd.CommandText = "LoadDocument";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@dID", SqlDbType.Int).Value = id;
SqlDataReader reader = DataProvider.ExecuteReader( cmd);
while( reader.Read() )
{
this.comment = "";
this.origname = reader["OrigName"].ToString();
//Now we have to save the image

this.name = Guid.NewGuid().ToString() + "." + origname.Substring(
origname.LastIndexOf(".")+1);
physicalname = physicalname + @"\" + name;
FileStream file = new FileStream( physicalname, FileMode.Create);
file.Write( (byte[])reader["Data"], 0,
((byte[])reader["Data"]).GetUpperBound(0)+1 );
file.Close();

}
reader.Close();


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Hrvoje Voda said:
I manage to save data into database, but how to read this data for example
into listBox ?

Hrcko

Ignacio Machin ( .NET/ C# MVP ) said:
hi,

What is the error you are getting?

Why you use & return an empty arraylist?

or you did not post the entire code?

anyhow, this is the code I use for upload an Image to the DB

FileStream file = new FileStream( physicalname, FileMode.Open);
byte[] buff = new byte [ file.Length];
file.Read(buff, 0, Convert.ToInt32(file.Length));
file.Close();


SqlParameter param = com.Parameters.Add("@data", SqlDbType.Image);
param.Value = buff;


The SP is declared as :


PROCEDURE SaveDocument
(
@data IMAGE ,


and the datatype of the column is of course IMAGE


HTH,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Hrvoje Voda said:
I wrote this code to save a blob data into database.

What is wrong?

public ArrayList SetProfile(byte [] Profile)

{

ArrayList arrayProfile = new ArrayList ();


int bufferSize = 100; .

byte[] outbyte = new byte[bufferSize];



SqlConnection conn = null;

SqlDataReader rdr = null;


if (_UserName !=null)

{

try

{


conn = new

SqlConnection(sConnString);

conn.Open() ;

SqlCommand cmd = new SqlCommand("SetProfile", conn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add ("@UserName",_UserName);

cmd.Parameters.Add("@Profile", SqlDbType.Image, Profile.Length).Value =
Profile;

rdr = cmd.ExecuteReader ();



}

finally

{

if (conn != null)

{

conn.Close();

}

if (rdr != null)

{

rdr.Close();

}

}

}

return arrayProfile;

}

Hrcko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top