C#如何修复这些错误(System.Windows.Forms.PictureBox.Image.get返回空错误)并验证picturebox是否为空/空

时间:2018-09-14 04:02:53

标签: c# winforms sql-server-2008

下面是将图像保存到数据库的代码。我想获得帮助的是修复我的这些错误,并在用户不选择图像的情况下对其进行验证,该图像将显示一个消息框。

在这张图片中,当我尝试将这些信息添加到数据库中而不选择将图像添加到计算机文件时,它显示了这些错误(System.Windows.Forms.PictureBox.Image.get返回空错误) 我正在谈论的这个错误

public partial class ADDProduct : MetroForm
{
    SIMSProduct _view;
    public ADDProduct(SIMSProduct view)
    {
        InitializeComponent();
        _view = view;       
    }
     private void btn_add_Click(object sender, EventArgs e)
    {
            using (var con = SQLConnection.GetConnection())
            {
            if (string.IsNullOrEmpty(cbox_supplier.Text) || string.IsNullOrEmpty(txt_code.Text) || string.IsNullOrEmpty(txt_item.Text) || string.IsNullOrEmpty(txt_quantity.Text) || string.IsNullOrEmpty(txt_cost.Text))
            {
                CustomNotifcation.Show("Please input the required fields", CustomNotifcation.AlertType.warning);
            }
            else
            {          
                ValidateCode.IsValidCode(txt_code,lbl_code,ds);
                if (lbl_code.Visible == true)
                {
                    CustomNotifcation.Show("CODE ALREADY EXIST", CustomNotifcation.AlertType.error);
                    lbl_code.Visible = false;

                }
                else
                {
                        using (var select = new SqlCommand("Insert into employee_product (Image, Supplier, Codeitem, Itemdescription, Date, Quantity, Unitcost) Values (@Image, @Supplier, @Codeitem, @Itemdescription, @Date, @Quantity, @Unitcost)", con))
                        {                               
                            var ms = new MemoryStream();
                            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                            var PhotoByte = ms.GetBuffer();                           
                            select.Parameters.Add("@Image", SqlDbType.VarBinary).Value = PhotoByte;
                            select.Parameters.Add("@Supplier", SqlDbType.VarChar).Value = cbox_supplier.Text;
                            select.Parameters.Add("@Codeitem", SqlDbType.VarChar).Value = txt_code.Text.Trim();
                            select.Parameters.Add("@Itemdescription", SqlDbType.VarChar).Value = txt_item.Text.Trim();
                            select.Parameters.Add("@Date", SqlDbType.VarChar).Value = date;
                            select.Parameters.Add("@Quantity", SqlDbType.Int).Value = txt_quantity.Text.Trim();
                            select.Parameters.Add("@Unitcost", SqlDbType.Int).Value = txt_cost.Text.Trim();
                            select.ExecuteNonQuery();
                            CustomMessage.Show("Message: Item successfully added!",CustomMessage.Messagetype.Success);
                            pictureBox1.Image = null;
                            cbox_supplier.Items.Clear();
                            txt_code.Clear();
                            txt_item.Clear();
                            txt_quantity.Clear();
                            txt_cost.Clear();
                            _view.btn_update.Enabled = false;
                            _view.AddingProduct();
                        }
                    }                
                }                
            }
    }
  private void pictureBox1_Click(object sender, EventArgs e)
    {
        var opnfd    = new OpenFileDialog();
        opnfd.Filter            = "Image Files (*.jpg;*.jpeg;.*.png;)|*.jpg;*.jpeg;.*.png;";
        opnfd.Title             = "Select Item";

        if (opnfd.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.SizeMode    = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image       = Image.FromFile(opnfd.FileName);

        }

    }
}

0 个答案:

没有答案