双击单元格错误"无法转换类型为' System.DBNull'的对象输入' System.Byte []'。"}"

时间:2018-01-26 15:24:19

标签: c# sql

请帮助谢谢 出现错误,因为无法转换类型' System.DBNull'输入' System.Byte []'。"

当我" SAVE"创业板参赛表格没有"图像"它工作正常。 当我" SAVE"创业板参赛表格带有"图像"它工作正常。

错误发生在" Gem" grid-view查看Gems列表。 当我需要"更新"宝石记录,我将DoubleClick的单元格。 因此,如果存在,如果我需要更改图像和"更新"它有效..但

当我需要" UPDATE"时出现

错误一条没有图像的记录,该行弹出错误...作为图像:enter image description here

这是我的股票代码:

    private void btnsave_Click(object sender, EventArgs e)
    {
        try
        {
            conn.Close();
            conn.Open();

            String query;

            if (pb1.Image == null)
            {
                query = "INSERT INTO Stock_Gems (Stock_Type,Stock_no,No_of_pieces,Gem_Type,Weight,Cost,Create_Date,Update_Date,UserID) VALUES(@Stock_Type,@stock_no,@No_of_pieces,@Gem_Type,@Weight,@Cost,@Created_Date,@Updated_Date,@User_Create)";
            }
            else
            {
                query = "INSERT INTO Stock_Gems VALUES(@Stock_Type,@stock_no,@No_of_pieces,@Gem_Type,@Weight,@image,@Cost,@Created_Date,@Updated_Date,@User_Create)";
            }
            SqlCommand command = new SqlCommand(query, conn);


            command.Parameters.Add("@Stock_Type", SqlDbType.VarChar);
            command.Parameters["@Stock_Type"].Value = Stock_Type.Text;

            command.Parameters.Add("@stock_no", SqlDbType.VarChar);
            command.Parameters["@stock_no"].Value = txtstock_no.Text;

            command.Parameters.Add("@No_of_pieces", SqlDbType.Int);
            command.Parameters["@No_of_pieces"].Value = txtno_of_peices.Text;

            command.Parameters.Add("@Gem_Type", SqlDbType.NVarChar);
            command.Parameters["@Gem_Type"].Value = txt_gems.Text;

            command.Parameters.Add("@Weight", SqlDbType.Float);
            command.Parameters["@Weight"].Value = txt_weight.Text;

               if (pb1.Image != null)
               {
                   MemoryStream stream = new MemoryStream();
                   pb1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                   byte[] pic = stream.ToArray();
                   command.Parameters.Add("@image", SqlDbType.Binary);
                   command.Parameters["@image"].Value = pic;
               }

            command.Parameters.Add("@Cost", SqlDbType.Decimal);
            command.Parameters["@Cost"].Value = txt_cost.Text;

            command.Parameters.Add("@Created_Date", SqlDbType.DateTime);
            command.Parameters["@Created_Date"].Value = label11.Text;

            command.Parameters.Add("@Updated_Date", SqlDbType.DateTime);
            command.Parameters["@Updated_Date"].Value = label11.Text;

            command.Parameters.Add("@User_Create", SqlDbType.NVarChar);
            command.Parameters["@User_Create"].Value = hello.Text;

            command.ExecuteNonQuery();
            conn.Close();

            if (cmbStockType.SelectedIndex == 0)
                _lastUG++;
            else
                _lastMG++;
            saveLastNumbers();

            MessageBox.Show("You've inserted successfully!", "Successful Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Hide();
        }
    }

这是我的gride-view代码: private void dataGridView1_CellDoubleClick(object sender,DataGridViewCellEventArgs e)         {

        S_Gems myForm = new S_Gems();

        myForm.Stock_Type.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
        myForm.txtstock_no.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
        myForm.txtno_of_peices.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
        myForm.txt_gems.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
        myForm.txt_weight.Text = this.dataGridView1.CurrentRow.Cells[5].Value.ToString();

        byte[] pic = (Byte[])dataGridView1.CurrentRow.Cells[6].Value;
        MemoryStream stream = new MemoryStream(pic);

        myForm.pb1.Image = Image.FromStream(stream);

        myForm.txt_cost.Text = this.dataGridView1.CurrentRow.Cells[7].Value.ToString();
      //  myForm.label11.Text = this.dataGridView1.CurrentRow.Cells[8].Value.ToString();
       // myForm.label11.Text = this.dataGridView1.CurrentRow.Cells[9].Value.ToString();
        myForm.ShowDialog();
    }

我正在使用sql server 2014: 我的宝石入口表看起来像: 列名:

ID|Stock_Type|Stock_No|No_of_pieces|Gem_Type|Weight|Image|Cost|Create_Date|Update_Date|UserID|

Image的数据类型是varbinary(MAX) 我勾选了允许NUlls

1 个答案:

答案 0 :(得分:1)

没有图片的元素在该列的db中具有NULL值。因此dataGridView1.CurrentRow.Cells[6]不包含代表图片的byte[],而是DBNull.Value

您的代码应该知道这种可能性并跳过图像处理:

if (dataGridView1.CurrentRow.Cells[6] is byte[] pic) // C#7
{
    MemoryStream stream = new MemoryStream(pic);
    myForm.pb1.Image = Image.FromStream(stream);
}
else
    myForm.pb1.Image = null;

在C#7之前,你应该做

byte[] pic = dataGridView1.CurrentRow.Cells[6] as byte[];
if (pic != null)
{
    MemoryStream stream = new MemoryStream(pic);
    myForm.pb1.Image = Image.FromStream(stream);
}
else
    myForm.pb1.Image = null;