TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Al Mamun
NA
3
0
Problem Working With Image...
Nov 20 2007 7:18 AM
Can Somebody help,,
I am using this code on button2 Click Event and it will
generate the list with
Name & Description...
and create .JPG file into local drive corresponding to
Name
and when I click to grid then image box will load the
JPG
Image from the local drive.
Now the Problem is when I put some value and click then it will generate list, and put new value into text box then it will generate list with new value, but if i just put the first value and click then an Exception occurs (IOException), this is used by another process...see the
red
Hilighted area.
private void button2_Click(object sender, EventArgs e)
{
try
{
SqlDataReader myReader = null;
//button1.Enabled = false;
imgTest.Image = null;
txtDesc.Text = "";
string iPath = Application.StartupPath.ToString().Replace("\\", "#$");
iPath = iPath.Replace('#', '\\');
iPath = iPath.Replace('$', '\\');
iPath += "\\" + "\\TestPic";
/*if (Directory.Exists(iPath) == true)
{
Directory.Delete(iPath,true);
}
Directory.CreateDirectory(iPath);*/
//MessageBox.Show(iPath);
//iPath += "\\TestPic" + "\\" + st1 + ".JPG";
// **** Read BLOB from the Database and save it on the Filesystem
//int protorefno = int.Parse(DataService.GetSingleValue(conn, "select protorefno from tblProtoList where protono='" + cmbProtoNo.Text.ToString() + "'"));
SqlCommand getPic = new SqlCommand(
"SELECT srcname, srcdesc, pic " +
"FROM tblTestReportImg " +
"WHERE srcdesc like '%"+ textBox1.Text.ToString()+"%'", conn);
//getPic.Parameters.Add("@PRN", SqlDbType.BigInt).Value = protorefno;
//getPic.Parameters.Add("@PRN", SqlDbType.VarChar).Value = textBox1.Text;
//getPic.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10).Value = pfirstName;
FileStream fs; // Writes the BLOB to a file (*.bmp).
BinaryWriter bw; // Streams the BLOB to the FileStream object.
int bufferSize = 100; // Size of the BLOB buffer.
byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes.
long retval; // The bytes returned from GetBytes.
long startIndex = 0; // The starting position in the BLOB output.
string picname = ""; // The Picture Name to use in the file name.
string desc = ""; // The Description to use in the file name.
//Define the Destination Path
// Open the connection and read data into the DataReader.
if (conn.State.ToString().ToUpper() != "OPEN")
conn.Open();
myReader = getPic.ExecuteReader(CommandBehavior.SequentialAccess);
lstImage.Items.Clear();
if (myReader.HasRows == true)
{
while (myReader.Read())
{
// Get the employee id, which must occur before getting the employee.
picname = myReader.GetString(0).ToString(); //.GetInt32(0).ToString();
desc = myReader.GetString(1).ToString();
ListViewItem it1 = lstImage.Items.Add(picname.ToString());
it1.SubItems.Add(desc.ToString());
// Create a file to hold the output.
iPath += "\\" + "\\";
if (File.Exists(iPath + picname + ".JPG") == true)
{
//MessageBox.Show(File.GetLastAccessTime(iPath + picname + ".JPG").ToString());
File.Delete(iPath + picname + ".JPG");
//Error msg: You cant delete this file it is being used by anathor process
}
//Here comes Exception when I erase upper red line.
fs = new FileStream(iPath + picname + ".JPG", FileMode.Create, FileAccess.Write);
//If I click button2 after few time then there is no error or exception
//When I click button2 then Create jpg file to local drive and again same click then error occurs that this file is used by anathor process.
//Is there any solution so that I can realize or stoped process to continue above line
bw = new BinaryWriter(fs);
// Reset the starting byte for the new BLOB.
startIndex = 0;
// Read the bytes into outbyte[] and retain the number of bytes returned.
retval = myReader.GetBytes(2, startIndex, outbyte, 0, bufferSize);
// Continue reading and writing while there are bytes beyond the size of the buffer.
while (retval == bufferSize)
{
bw.Write(outbyte);
bw.Flush();
// Reposition the start index to the end of the last buffer and fill the buffer.
startIndex += bufferSize;
retval = myReader.GetBytes(2, startIndex, outbyte, 0, bufferSize);
}
// Write the remaining buffer.
bw.Write(outbyte, 0, (int)retval);
bw.Flush();
// Close the output file.
bw.Close();
fs.Close();
}
// Close the reader and the connection.
myReader.Close();
if (conn.State.ToString().ToUpper() == "OPEN")
conn.Close();
}
else
{
myReader.Close();
if (conn.State.ToString().ToUpper() == "OPEN")
conn.Close();
}
/*sql = "select tr.* from tblProtoList p,tblTestReportImg tr where p.protono='" + cmbProtoNo.Text.ToString() + "' and p.protorefno=tr.protorefno order by tr.picind asc";
r = DataService.SelectData(conn, sql);
lstImage.Items.Clear();
if (r.HasRows == true)
{
while (r.Read())
{
ListViewItem it1 = lstImage.Items.Add(r["srcname"].ToString());
it1.SubItems.Add(r["srcdesc"].ToString());
//MessageBox.Show(bt.ToString());
//it1.SubItems.Add(r["pic"].ToString());
}
r.Close();
}*/
//button1.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
I am not good in English.
Pardon me if there is/are any mistake.
Best Regards,
Abdullah Al Mamun
Reply
Answers (
0
)
Set Start Parameters in a service
NP59 Changes in Color