Click here to Skip to main content
16,015,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
  Whenever i try to save picture from picturebox to access database i usually get this error message  "An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll

Additional information: Path cannot be null."


This is my code for saving to database
C#
private void cmdSave_Click(object sender, EventArgs e)
       {

           int rowCount;
           rowCount = num.Next();
          // OpenFileDialog f1 = new OpenFileDialog();
         //  f1.ShowDialog();
          // picImageDisplay.ImageLocation = f1.FileName;

           //save the image to the database
           OleDbCommand cmd = new OleDbCommand();
           cmd.Connection = DBConnection;

           //creating a filestream and streamreader converting the pictures
           // to binary before saving to the database
           FileStream stream = null;
           StreamReader reader = null;

           stream = new FileStream(picPreview.ImageLocation, FileMode.Open, FileAccess.Read, FileShare.Read);
           reader = new StreamReader(stream);

           byte[] fbyteArray = new byte[stream.Length];

           stream.Read(fbyteArray, 0, Convert.ToInt32(stream.Length));
           if(cboCoordinateLabel.Text=="Artery")
           {
               cmd.CommandText = "INSERT INTO artery VALUES(@image_id,@coordinate,@image)";
               cmd.Parameters.Add("@image_id", OleDbType.Integer).Value = rowCount;
               cmd.Parameters.Add("@coordinate", OleDbType.VarChar).Value = coordinate;
               cmd.Parameters.Add("@image", OleDbType.Binary, Convert.ToInt32(stream.Length)).Value = fbyteArray;

               DBConnection.Open();
           }

           else if(cboCoordinateLabel.Text=="Vein")
           {
               cmd.CommandText = "INSERT INTO veins VALUES(@id,coordinate,@image)";
               cmd.Parameters.Add("@image_id", OleDbType.Integer).Value = rowCount;
               cmd.Parameters.Add("@coordinate", OleDbType.VarChar).Value = coordinate;
               cmd.Parameters.Add("@image", OleDbType.Binary, Convert.ToInt32(stream.Length)).Value = fbyteArray;

               DBConnection.Open();
           }

           else
           {
               MessageBox.Show("Please select an option from the coordinate label to proceed");
           }

           try
           {
               cmd.ExecuteNonQuery();
               MessageBox.Show("Record has been saved");

           }

           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());
               MessageBox.Show(ex.StackTrace.ToString());
           }
           finally
           {
               DBConnection.Close();
           }
       }
Posted

Have a look at this: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it's for SQL Server rather than Access but it's exactly the same process with OleDBConnection and command instead.
 
Share this answer
 
private void cmdSave_Click(object sender, EventArgs e)
{


arteryCount++;
veinCount++;




if(cboCoordinateLabel.Text != "")
{
if(cboCoordinateLabel.Text=="Artery")
{
try
{
//using MemoryStream
MemoryStream ms = new MemoryStream();
picPreview.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);

//save data
cmd.CommandText = "insert into artery values(@image_id,coordinate,image)";
cmd.Parameters.Add("@image_id", OleDbType.Integer).Value = arteryCount;
cmd.Parameters.Add("@coordinate", OleDbType.VarChar).Value = coordinate;
cmd.Parameters.AddWithValue("@image", photo_aray);
DBConnection.Open();
cmd.Connection = DBConnection;

cmd.ExecuteNonQuery();
MessageBox.Show("Record has been saved");


}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
MessageBox.Show(ex.StackTrace.ToString());
}
finally
{
DBConnection.Close();
}

}
else
{
try
{
//using MemoryStream
MemoryStream ms = new MemoryStream();
picPreview.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);

//save data
cmd.CommandText = "insert into vein values(@image_id,coordinate,image)";
cmd.Parameters.Add("@image_id", OleDbType.Integer).Value = veinCount;
cmd.Parameters.Add("@coordinate", OleDbType.VarChar).Value = coordinate;
cmd.Parameters.AddWithValue("@image", photo_aray);

DBConnection.Open();
cmd.Connection = DBConnection;

cmd.ExecuteNonQuery();
MessageBox.Show("Record has been saved");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
MessageBox.Show(ex.StackTrace.ToString());
}
finally
{
DBConnection.Close();
}

}
}
//if the combo box is empty
else
{
MessageBox.Show("Please select an option from the coordinate label to proceed");
}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900