Convert VB to C# for reading an image from Access DB

G

Guest

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 application, while I am
doing for web. I can only find Image from web forms control and HTML control.
This may be the root cause of my problem. For read button, I converted his VB
to the C#. But the compiler complains:

1. For Image,
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(421): 'Image'
is an ambiguous reference

2. For imgBox.Image,
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(422):
'System.Web.UI.WebControls.Image' does not contain a definition for 'Image'

3. For imgBox.Invalidate(),
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(423):
'System.Web.UI.WebControls.Image' does not contain a definition for
'Invalidate'

The related codes are attached below. Any suggestion? Thanks. -Dale

VB:
Private Sub UseReaderBtn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles UseReaderBtn.Click

' Construct a SQL string and a connection object
Dim sql As String = "SELECT UserPhoto FROM Users"
Dim conn As OleDbConnection = New OleDbConnection()
conn.ConnectionString = connectionString
' Open connection
If conn.State <> ConnectionState.Open Then
conn.Open()
End If

Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
Dim fs As FileStream
Dim bw As BinaryWriter
Dim bufferSize As Integer = 300000
Dim outbyte(300000 - 1) As Byte
Dim retval As Long
Dim startIndex As Long = 0
Dim pub_id As String = ""
Dim reader As OleDbDataReader = _
cmd.ExecuteReader(CommandBehavior.SequentialAccess)
' Read first record
reader.Read()
fs = New FileStream(savedImageName, _
FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
startIndex = 0
retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
bw.Write(outbyte)
bw.Flush()
' Close the output file.
bw.Close()
fs.Close()
reader.Close()
' Display image
curImage = Image.FromFile(savedImageName)
PictureBox1.Image = curImage
PictureBox1.Invalidate()
' Clean up connection
If conn.State = ConnectionState.Open Then
conn.Close()
' Dispose connection
conn.Dispose()
End If
End Sub

C#:
private void btnReadFmDB_Click(object sender, System.EventArgs e)
{
OleDbConnection dbConn;
OleDbCommand dbCmd;
OleDbDataReader dbDR;
string applicationState = ((string)(Application["DBType"])).ToLower();
string sConn = dbClass.Connect(applicationState);
// Construct a SQL string and a connection object
string sql = ("SELECT * FROM UserFiles WHERE Username =\'" +
(dbClass.DelimString(dListUsers.SelectedValue) + "\'"));
FileStream fs;
BinaryWriter bw;
int bufferSize = 300000;
byte[] outbyte;
long retval;
long startIndex = 0;
string pub_id = "";

try
{
dbConn = new OleDbConnection(sConn);
dbConn.Open();
dbCmd = new OleDbCommand(sql, dbConn);
dbDR = dbCmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dbDR.Read())
{
fs = new FileStream(savedImageName, FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);
startIndex = 0;
retval = dbDR.GetBytes(0, 0, outbyte, 0, bufferSize);
bw.Write(outbyte);
bw.Flush();
// Close the output file.
bw.Close();
fs.Close();
}
// get the end
dbConn.Close();
}
catch (Exception excep)
{
Debug.WriteLine(excep.Message);
return;
}
// Display image
curImage = Image.FromFile(savedImageName);
imgBox.Image = curImage;
imgBox.Invalidate();
}
 
G

Guest

For the 1st error, I reckon you could use a full namespace
"System.Web.UI.WebControls.Image" instead of "Image"
However, in this case you don't need to load the image from a path, what you
need is to set the ImageUrl.

For the 2nd error, you need to set the image to the Image class by setting
the ImageUrl property of Image class

For the 3rd error, there is no method called "Invalidate" for
System.Web.UI.WebControls.Image class, in your case you don't need to call
any method to render the image, it will be display when the ImageUrl property
is set
and the webpage has been refreshed.

The modified code is shown below:

dale zhang said:
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 application, while I am
doing for web. I can only find Image from web forms control and HTML control.
This may be the root cause of my problem. For read button, I converted his VB
to the C#. But the compiler complains:

1. For Image,
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(421): 'Image'
is an ambiguous reference

2. For imgBox.Image,
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(422):
'System.Web.UI.WebControls.Image' does not contain a definition for 'Image'

3. For imgBox.Invalidate(),
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(423):
'System.Web.UI.WebControls.Image' does not contain a definition for
'Invalidate'

The related codes are attached below. Any suggestion? Thanks. -Dale

VB:
Private Sub UseReaderBtn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles UseReaderBtn.Click

' Construct a SQL string and a connection object
Dim sql As String = "SELECT UserPhoto FROM Users"
Dim conn As OleDbConnection = New OleDbConnection()
conn.ConnectionString = connectionString
' Open connection
If conn.State <> ConnectionState.Open Then
conn.Open()
End If

Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
Dim fs As FileStream
Dim bw As BinaryWriter
Dim bufferSize As Integer = 300000
Dim outbyte(300000 - 1) As Byte
Dim retval As Long
Dim startIndex As Long = 0
Dim pub_id As String = ""
Dim reader As OleDbDataReader = _
cmd.ExecuteReader(CommandBehavior.SequentialAccess)
' Read first record
reader.Read()
fs = New FileStream(savedImageName, _
FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
startIndex = 0
retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
bw.Write(outbyte)
bw.Flush()
' Close the output file.
bw.Close()
fs.Close()
reader.Close()
' Display image
curImage = Image.FromFile(savedImageName)
PictureBox1.Image = curImage
PictureBox1.Invalidate()
' Clean up connection
If conn.State = ConnectionState.Open Then
conn.Close()
' Dispose connection
conn.Dispose()
End If
End Sub

C#:
private void btnReadFmDB_Click(object sender, System.EventArgs e)
{
OleDbConnection dbConn;
OleDbCommand dbCmd;
OleDbDataReader dbDR;
string applicationState = ((string)(Application["DBType"])).ToLower();
string sConn = dbClass.Connect(applicationState);
// Construct a SQL string and a connection object
string sql = ("SELECT * FROM UserFiles WHERE Username =\'" +
(dbClass.DelimString(dListUsers.SelectedValue) + "\'"));
FileStream fs;
BinaryWriter bw;
int bufferSize = 300000;
byte[] outbyte;
long retval;
long startIndex = 0;
string pub_id = "";

try
{
dbConn = new OleDbConnection(sConn);
dbConn.Open();
dbCmd = new OleDbCommand(sql, dbConn);
dbDR = dbCmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dbDR.Read())
{
fs = new FileStream(savedImageName, FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);
startIndex = 0;
retval = dbDR.GetBytes(0, 0, outbyte, 0, bufferSize);
bw.Write(outbyte);
bw.Flush();
// Close the output file.
bw.Close();
fs.Close();
}
// get the end
dbConn.Close();
}
catch (Exception excep)
{
Debug.WriteLine(excep.Message);
return;
}
// Display image
imgBox.ImageUrl = savedImageName;
 
O

Ollie Riches

have you tried using any of the free converters:

http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

HTH

Ollie Riches

dale zhang said:
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 application, while I am
doing for web. I can only find Image from web forms control and HTML
control.
This may be the root cause of my problem. For read button, I converted his
VB
to the C#. But the compiler complains:

1. For Image,
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(421):
'Image'
is an ambiguous reference

2. For imgBox.Image,
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(422):
'System.Web.UI.WebControls.Image' does not contain a definition for
'Image'

3. For imgBox.Invalidate(),
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(423):
'System.Web.UI.WebControls.Image' does not contain a definition for
'Invalidate'

The related codes are attached below. Any suggestion? Thanks. -Dale

VB:
Private Sub UseReaderBtn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles UseReaderBtn.Click

' Construct a SQL string and a connection object
Dim sql As String = "SELECT UserPhoto FROM Users"
Dim conn As OleDbConnection = New OleDbConnection()
conn.ConnectionString = connectionString
' Open connection
If conn.State <> ConnectionState.Open Then
conn.Open()
End If

Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
Dim fs As FileStream
Dim bw As BinaryWriter
Dim bufferSize As Integer = 300000
Dim outbyte(300000 - 1) As Byte
Dim retval As Long
Dim startIndex As Long = 0
Dim pub_id As String = ""
Dim reader As OleDbDataReader = _
cmd.ExecuteReader(CommandBehavior.SequentialAccess)
' Read first record
reader.Read()
fs = New FileStream(savedImageName, _
FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
startIndex = 0
retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
bw.Write(outbyte)
bw.Flush()
' Close the output file.
bw.Close()
fs.Close()
reader.Close()
' Display image
curImage = Image.FromFile(savedImageName)
PictureBox1.Image = curImage
PictureBox1.Invalidate()
' Clean up connection
If conn.State = ConnectionState.Open Then
conn.Close()
' Dispose connection
conn.Dispose()
End If
End Sub

C#:
private void btnReadFmDB_Click(object sender, System.EventArgs e)
{
OleDbConnection dbConn;
OleDbCommand dbCmd;
OleDbDataReader dbDR;
string applicationState = ((string)(Application["DBType"])).ToLower();
string sConn = dbClass.Connect(applicationState);
// Construct a SQL string and a connection object
string sql = ("SELECT * FROM UserFiles WHERE Username =\'" +
(dbClass.DelimString(dListUsers.SelectedValue) + "\'"));
FileStream fs;
BinaryWriter bw;
int bufferSize = 300000;
byte[] outbyte;
long retval;
long startIndex = 0;
string pub_id = "";

try
{
dbConn = new OleDbConnection(sConn);
dbConn.Open();
dbCmd = new OleDbCommand(sql, dbConn);
dbDR = dbCmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dbDR.Read())
{
fs = new FileStream(savedImageName, FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);
startIndex = 0;
retval = dbDR.GetBytes(0, 0, outbyte, 0, bufferSize);
bw.Write(outbyte);
bw.Flush();
// Close the output file.
bw.Close();
fs.Close();
}
// get the end
dbConn.Close();
}
catch (Exception excep)
{
Debug.WriteLine(excep.Message);
return;
}
// Display image
curImage = Image.FromFile(savedImageName);
imgBox.Image = curImage;
imgBox.Invalidate();
}
 
G

Guest

Thank you for the link. This one seems better than I used.

After reading the ole object from db, I saved it to C: as file1.bmp and
displayed on the web. But it can not be displayed. After I manually sent the
file to wordpad, it shows

System.Byte [ ]

Now I suspect my saving an image might be wrong. The table has 2 columns:
username and userfile. Userfile column shows

..ong binary data

I can not find a way to verify if data was saved ok? But saving does not
report any errors. I am attached my saving and reading codes here to see if
anyone can help?

Thanks. -dale
private void btnSaveToDB_Click(object sender, System.EventArgs e)
{
if (dListUsers.SelectedValue == "")
{
lblFileAccess.Text = "You need to select an user first!";
return;
}
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();
lblFileAccess.Text = "The file has been saved successfully for "
+ dListUsers.SelectedValue + ".";
return;
}
catch (Exception excep)
{
Debug.WriteLine(excep.Message);
lblFileAccess.Text = "The file has not been saved successfully for "
+ dListUsers.SelectedValue + ".";
return;
}
}

private void btnReadFmDB_Click(object sender, System.EventArgs e)
{
OleDbConnection dbConn;
OleDbCommand dbCmd;
OleDbDataReader dbDR;
string applicationState = ((string)(Application["DBType"])).ToLower();
string sConn = dbClass.Connect(applicationState);
// Construct a SQL string and a connection object
string sql = "SELECT UserFile FROM UserFiles WHERE Username =\'"
+ dListUsers.SelectedValue + "\'";
FileStream fs;
BinaryWriter bw;
int bufferSize = 300000;
//byte[] outbyte;
byte[] outbyte = new byte[300000 - 1];

long retval;
//long startIndex = 0;
//string pub_id = "";

try
{
dbConn = new OleDbConnection(sConn);
dbConn.Open();
dbCmd = new OleDbCommand(sql, dbConn);
dbDR = dbCmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dbDR.Read())
{
fs = new FileStream(savedImageName, FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);
//startIndex = 0;
retval = dbDR.GetBytes(0, 0, outbyte, 0, bufferSize);

bw.Write(outbyte);
bw.Flush();
// Close the output file.
bw.Close();
fs.Close();
}
// get the end
dbConn.Close();
}
catch (Exception excep)
{
Debug.WriteLine(excep.Message);
return;
}
// Display image
//curImage = System.Web.UI.WebControls.Image.FromFile(savedImageName);
imgBox.ImageUrl = savedImageName;

}



Ollie Riches said:
have you tried using any of the free converters:

http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

HTH

Ollie Riches

dale zhang said:
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 application, while I am
doing for web. I can only find Image from web forms control and HTML
control.
This may be the root cause of my problem. For read button, I converted his
VB
to the C#. But the compiler complains:

1. For Image,
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(421):
'Image'
is an ambiguous reference

2. For imgBox.Image,
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(422):
'System.Web.UI.WebControls.Image' does not contain a definition for
'Image'

3. For imgBox.Invalidate(),
C:\Inetpub\wwwroot\passwordProtectCSharp\adminUserFile.aspx.cs(423):
'System.Web.UI.WebControls.Image' does not contain a definition for
'Invalidate'

The related codes are attached below. Any suggestion? Thanks. -Dale

VB:
Private Sub UseReaderBtn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles UseReaderBtn.Click

' Construct a SQL string and a connection object
Dim sql As String = "SELECT UserPhoto FROM Users"
Dim conn As OleDbConnection = New OleDbConnection()
conn.ConnectionString = connectionString
' Open connection
If conn.State <> ConnectionState.Open Then
conn.Open()
End If

Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
Dim fs As FileStream
Dim bw As BinaryWriter
Dim bufferSize As Integer = 300000
Dim outbyte(300000 - 1) As Byte
Dim retval As Long
Dim startIndex As Long = 0
Dim pub_id As String = ""
Dim reader As OleDbDataReader = _
cmd.ExecuteReader(CommandBehavior.SequentialAccess)
' Read first record
reader.Read()
fs = New FileStream(savedImageName, _
FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
startIndex = 0
retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
bw.Write(outbyte)
bw.Flush()
' Close the output file.
bw.Close()
fs.Close()
reader.Close()
' Display image
curImage = Image.FromFile(savedImageName)
PictureBox1.Image = curImage
PictureBox1.Invalidate()
' Clean up connection
If conn.State = ConnectionState.Open Then
conn.Close()
' Dispose connection
conn.Dispose()
End If
End Sub

C#:
private void btnReadFmDB_Click(object sender, System.EventArgs e)
{
OleDbConnection dbConn;
OleDbCommand dbCmd;
OleDbDataReader dbDR;
string applicationState = ((string)(Application["DBType"])).ToLower();
string sConn = dbClass.Connect(applicationState);
// Construct a SQL string and a connection object
string sql = ("SELECT * FROM UserFiles WHERE Username =\'" +
(dbClass.DelimString(dListUsers.SelectedValue) + "\'"));
FileStream fs;
BinaryWriter bw;
int bufferSize = 300000;
byte[] outbyte;
long retval;
long startIndex = 0;
string pub_id = "";

try
{
dbConn = new OleDbConnection(sConn);
dbConn.Open();
dbCmd = new OleDbCommand(sql, dbConn);
dbDR = dbCmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dbDR.Read())
{
fs = new FileStream(savedImageName, FileMode.OpenOrCreate,
FileAccess.Write);
bw = new BinaryWriter(fs);
startIndex = 0;
retval = dbDR.GetBytes(0, 0, outbyte, 0, bufferSize);
bw.Write(outbyte);
bw.Flush();
// Close the output file.
bw.Close();
fs.Close();
}
// get the end
dbConn.Close();
}
catch (Exception excep)
{
Debug.WriteLine(excep.Message);
return;
}
// Display image
curImage = Image.FromFile(savedImageName);
imgBox.Image = curImage;
imgBox.Invalidate();
}
 

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