Empty path name is not legal
-
I want to insert an image in database. But if I don't choose any image, I receive : `Empty path name is not legal`. In database, `image` field is BLOB and is null. Where is my mistake cause I can't figure it out..I think that I should verify if textBox5 is null and if is, `imageBt=null`. I tried this but it didn't worked. This is the code that I'm using to insert the image:
byte[] imageBt = null;
FileStream fstream = new FileStream(this.textBox5.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
SQLiteCommand com = new SQLiteCommand("insert into questions(image) values(@IMG)", Conexiune.getConnection());
com.Parameters.Add(new SQLiteParameter("@IMG", imageBt));
com.ExecuteNonQuery();Try this instead of just checking for null:
if (!String.IsNullOrWhiteSpace(textBox5.Text))
{
// open file etc.
}If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
I want to insert an image in database. But if I don't choose any image, I receive : `Empty path name is not legal`. In database, `image` field is BLOB and is null. Where is my mistake cause I can't figure it out..I think that I should verify if textBox5 is null and if is, `imageBt=null`. I tried this but it didn't worked. This is the code that I'm using to insert the image:
byte[] imageBt = null;
FileStream fstream = new FileStream(this.textBox5.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
SQLiteCommand com = new SQLiteCommand("insert into questions(image) values(@IMG)", Conexiune.getConnection());
com.Parameters.Add(new SQLiteParameter("@IMG", imageBt));
com.ExecuteNonQuery();First of all, you should name your textboxes with a more meaningful name. Then, you really want to test some validity like this:
if (string.IsNullOrWhitespace(textBox5.Text)) return;
if (!File.Exists(textBox5.Text)) return; -
Try this instead of just checking for null:
if (!String.IsNullOrWhiteSpace(textBox5.Text))
{
// open file etc.
}If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
First of all, you should name your textboxes with a more meaningful name. Then, you really want to test some validity like this:
if (string.IsNullOrWhitespace(textBox5.Text)) return;
if (!File.Exists(textBox5.Text)) return; -
"Around" your posted code. Or what Pete replied before your posted code (that if (!File.Exists(textBox5.Text)) that he posted also).
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
Before the point that your code blows up would probably seem a sensible place don't you think?
-
Before the point that your code blows up would probably seem a sensible place don't you think?
-
"Around" your posted code. Or what Pete replied before your posted code (that if (!File.Exists(textBox5.Text)) that he posted also).
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
Hehe, alright :) To cover that topic in a general statement: Always check your inputs before trusting them.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
:)