Convert VB to C# for FileStream Length

G

Guest

Hi,

I am trying to save an image to MS Access DB based on the following article:

http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp

For save button, I converted his VB to the following C#. But the compiler
complains:

C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(333): Cannot
implicitly convert type 'long' to 'byte'

for " byte[] rawData = new byte[] {fs.Length};"

My code is attached below. Any suggestion? Thanks. -Dale

private void btnSaveToDB_Click(object sender, System.EventArgs e)
{
curFileName = myFile.PostedFile.FileName;
// only the attched file name not its path
string c = System.IO.Path.GetFileName(curFileName);
// Read a bitmap contents in a stream
FileStream fs = new FileStream(curFileName, FileMode.OpenOrCreate,
FileAccess.Read);
byte[] rawData = new byte[] {fs.Length};
fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
// Construct a SQL string and a connection object
OleDbConnection dbConn;
OleDbCommand dbCmd;
string applicationState = ((string)(Application["DBType"])).ToLower();
string sConn = dbClass.Connect(applicationState);
string sSQL;

sSQL = ("INSERT INTO UserFiles (UserName,UserFile) "
+ ("VALUES ("
+ (dbClass.DelimString(dListUsers.SelectedValue) + (","
+ (rawData )))));
try
{
// write the visit log entry
dbConn = new OleDbConnection(sConn);
dbConn.Open();
dbCmd = new OleDbCommand(sSQL, dbConn);
dbCmd.ExecuteNonQuery();
dbConn.Close();
return;
}
catch (Exception excep)
{
Debug.WriteLine(excep.Message);
return;
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Dale,

I think your conversion is wrong. What you really want to do is:

byte[] rawData = new byte[fs.Length];

Also, the call to read the bytes should be:

fs.Read(rawData, 0, rs.Length);

Finally, you should use the filestream in a using clause.

Hope this helps.
 
G

Guest

Nicholas,

Your help is excellent. Only the compiler does not take
"fs.Read(rawData, 0, fs.Length);"

but "fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));" is right.

I am done with saving. Thanks a lot. -dale

Nicholas Paldino said:
Dale,

I think your conversion is wrong. What you really want to do is:

byte[] rawData = new byte[fs.Length];

Also, the call to read the bytes should be:

fs.Read(rawData, 0, rs.Length);

Finally, you should use the filestream in a using clause.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

dale zhang said:
Hi,

I am trying to save an image to MS Access DB based on the following
article:

http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp

For save button, I converted his VB to the following C#. But the compiler
complains:

C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(333):
Cannot
implicitly convert type 'long' to 'byte'

for " byte[] rawData = new byte[] {fs.Length};"

My code is attached below. Any suggestion? Thanks. -Dale

private void btnSaveToDB_Click(object sender, System.EventArgs e)
{
curFileName = myFile.PostedFile.FileName;
// only the attched file name not its path
string c = System.IO.Path.GetFileName(curFileName);
// Read a bitmap contents in a stream
FileStream fs = new FileStream(curFileName, FileMode.OpenOrCreate,
FileAccess.Read);
byte[] rawData = new byte[] {fs.Length};
fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
// Construct a SQL string and a connection object
OleDbConnection dbConn;
OleDbCommand dbCmd;
string applicationState = ((string)(Application["DBType"])).ToLower();
string sConn = dbClass.Connect(applicationState);
string sSQL;

sSQL = ("INSERT INTO UserFiles (UserName,UserFile) "
+ ("VALUES ("
+ (dbClass.DelimString(dListUsers.SelectedValue) + (","
+ (rawData )))));
try
{
// write the visit log entry
dbConn = new OleDbConnection(sConn);
dbConn.Open();
dbCmd = new OleDbCommand(sSQL, dbConn);
dbCmd.ExecuteNonQuery();
dbConn.Close();
return;
}
catch (Exception excep)
{
Debug.WriteLine(excep.Message);
return;
}
}
 
J

Jon Skeet [C# MVP]

dale zhang said:
Your help is excellent. Only the compiler does not take
"fs.Read(rawData, 0, fs.Length);"

but "fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length));" is right.

Rather than having an unsightly conversion call, I'd just use:

fs.Read (rawData, 0, (int) fs.Length);

However, you shouldn't rely on all the data being read in one call
anyway. See
http://www.pobox.com/~skeet/csharp/readbinary.html
 

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