当我在图片框中的图片为空时尝试向数据库添加数据时出现此错误,但当图片框中包含图像时,系统运行良好。
错误:
发生了类型为“System.ArgumentNullException”的未处理异常 在mscorlib.dll中
其他信息:路径不能为空。
指出错误:
fsStream = new FileStream(this.imagePath,FileMode.Open, FileAccess.Read);
以下是我正在使用的代码:
Random _random = new Random();
Timer _timer = new Timer();
Wait _wait = new Wait();
FileStream fsStream;
BinaryReader bReader;
byte[] imageByte = null;
string imagePath;
string gender;
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\db1.accdb";
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "JPG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|All Files (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(dlg.FileName.ToString());
imagePath = dlg.FileName;
}
}
}
public virtual void button2_Click(object sender, EventArgs e)
{
fsStream = new FileStream(this.imagePath, FileMode.Open, FileAccess.Read);
bReader = new BinaryReader(fsStream);
imageByte = bReader.ReadBytes((int)fsStream.Length);
if (this.pictureBox1.Image == null || this.textBox2.Text == "" || this.textBox3.Text == "" || this.textBox4.Text == "" || this.textBox5.Text == "")
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("Cannot Submit!", "Warning", MessageBoxButtons.OK);
}
else if (this.label9.Visible == false)
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("You have to check the Student ID before proceeding!", "Warning", MessageBoxButtons.OK);
}
else if (this.label8.Visible == true)
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("You cannot use this Student ID. Please Get another Student ID instead!", "Warning", MessageBoxButtons.OK);
}
else if (this.label7.Visible == true)
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("Student ID is being verified! Please wait!", "Warning", MessageBoxButtons.OK);
}
else
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "INSERT INTO [Student] ([StudentID], [FirstName], [LastName], [Photo], [DateOfBirth], [Address], [JoinedDate], [Class], [Gender]) VALUES (@StudentID, @FirstName, @LastName, @Photo, @DateOfBirth, @Address, @JoinedDate, @Class, @Gender)";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
cmd.Parameters.Add("@StudentID", System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["@StudentID"].Value = this.textBox1.Text;
cmd.Parameters.Add("@FirstName", System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["@FirstName"].Value = this.textBox2.Text;
cmd.Parameters.Add("@LastName", System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["@LastName"].Value = this.textBox3.Text;
cmd.Parameters.Add("@Photo", System.Data.OleDb.OleDbType.LongVarBinary);
cmd.Parameters["@Photo"].Value = imageByte;
cmd.Parameters.Add("@DateOfBirth", System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["@DateOfBirth"].Value = this.dateTimePicker1.Text;
cmd.Parameters.Add("@Address", System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["@Address"].Value = this.textBox4.Text;
cmd.Parameters.Add("@JoinedDate", System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["@JoinedDate"].Value = this.dateTimePicker2.Text;
cmd.Parameters.Add("@Class", System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["@Class"].Value = this.textBox5.Text;
cmd.Parameters.Add("@Gender", System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["@Gender"].Value = gender;
_wait.ShowDialog();
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("Added Successfully!", "Success", MessageBoxButtons.OK);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
这是图片:
button1_Click适用于上传照片(图片将显示在顶部 右上角)
button2_Click用于提交(向数据库添加数据)
任何人都可以帮助我?
你的回答非常感谢!
答案 0 :(得分:3)
我猜(也许是错的)错误发生在这一行:
fsStream = new FileStream(this.imagePath, FileMode.Open, FileAccess.Read);
所以你应该添加一个空检查
if (this.imagePath != null)
在此行之前,并更正以下代码。
正如您在msdn中看到的,如果路径为空,FileStream的这个ctor将引发ArgumentNullException
。