By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

Convert VB to C# for FileStream Length

P: n/a
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\adminUser File.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;
}
}
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"dale zhang" <da*******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
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\adminUser File.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;
}
}

Nov 17 '05 #2

P: n/a
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"dale zhang" <da*******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
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\adminUser File.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;
}
}


Nov 17 '05 #3

P: n/a
dale zhang <da*******@discussions.microsoft.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

P: n/a
Jon, thank you for your additional help. It is very helpful. -Dale

"Jon Skeet [C# MVP]" wrote:
dale zhang <da*******@discussions.microsoft.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.