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();
}
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();
}