473,385 Members | 1,769 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,385 software developers and data experts.

Convert VB to C# for FileStream Length

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
4 2372
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Toby Mathews | last post by:
Hi, In an ASP.Net application I want to convert open create a FileStream object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method,...
1
by: bob | last post by:
byte salt = new byte {0xA8, 0x9B, 0xc8, 0x32, 0x57, 0x35, 0xe3, 0x03}; PasswordDeriveBytes p = new PasswordDeriveBytes("thepass", salt, "MD5", 21); EncryptData(args, args, p.GetBytes(8),...
2
by: Joey Lee | last post by:
Hi, Does anyone know how I am able to write a utf-8 encoded binary string into binary file? Currently I am given a UTF-8 string which was read from a gif image. Here are my functions... ...
3
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
12
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
3
by: Dennis | last post by:
I am trying to convert a bitmap to a JPEG MemoryStream and return a Byte array containing the resulting JPEG Image as follows: Public Function BmpToJPEG(ByVal BitMapIn As Bitmap, ByVal Quality As...
3
by: ArtOfSpeech | last post by:
how to convert this statement to vb.net??? byte buffer = new Byte;
5
by: bbb | last post by:
Hi, I need to convert XML files from Japanese encoding to UTF-8. I was using the following code: using ( FileStream fs = File.OpenRead(fromFile) ) { int fileSize = (int)fs.Length; int buffer...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.