不允许将null映像更新到数据库:UPDATE进程发生时显示错误c#

时间:2018-01-31 16:03:03

标签: c# sql-server-2014

我正在使用Visual Studio 2015和SQL Server 2014.问题基于C#。

这是一个库存盘点系统。我有两种形式:

  1. 股票报名表(它有SAVE,UPDATE,DELETE按钮)
  2. 股票gridview表单
  3. 我可以保存没有图像的库存条目这个过程有效,如果我需要更新,我将点击单元格双击Stock gridview表格,然后股票条目表格将打开所有条目,然后我可以进行更改并且更新但没有图像我无法更新该问题。

    我更新现有记录时出现的错误,该记录确实有图像,我正在进行更改并再次更新没有图像,然后弹出此错误:

      

    对象引用未设置为对象的实例

    这是通过SAVE按钮代码:工作正常,这意味着我可以保存没有图像:

    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();
            }
    
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
    }
    

    这是我的UPDATE按钮代码:

    private void btnupdate_Click(object sender, EventArgs e)
    {
            try
            {
                conn.Close();
                conn.Open();
    
                SqlCommand command = new SqlCommand("Update Stock_Gems  set Stock_Type = @Stock_Type, Stock_No = @Stock_No , No_of_pieces = @No_of_pieces, Gem_Type = @Gem_Type, Weight = @Weight, Image = @Image, Cost = @Cost, Update_Date = @Update_Date  WHERE  Stock_No = @Stock_No", conn);
    
                //command.Parameters.Add("@ID", SqlDbType.Int).Value = txt_ID.Text;
                command.Parameters.Add("@Stock_Type", SqlDbType.VarChar).Value = Stock_Type.Text;
                command.Parameters.Add("@stock_no", SqlDbType.NVarChar).Value = txtstock_no.Text;
                command.Parameters.Add("@No_of_pieces", SqlDbType.Int).Value = txtno_of_peices.Text;
                command.Parameters.Add("@Gem_Type", SqlDbType.NVarChar).Value = txt_gems.Text;
                command.Parameters.Add("@Weight", SqlDbType.Float).Value = txt_weight.Text;
    
                using (MemoryStream ms = new MemoryStream())
                {
                    pb1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    command.Parameters.Add("@Image", SqlDbType.Image).Value = ms.ToArray();
                }
                command.Parameters.Add("@Cost", SqlDbType.Decimal).Value = txt_cost.Text;
                command.Parameters.Add("@Update_Date", SqlDbType.DateTime).Value = label11.Text;
                command.ExecuteNonQuery();
    
                if (cmbStockType.SelectedIndex == 0)
                    _lastUG++;
                else
                    _lastMG++;
                saveLastNumbers();
    
                MessageBox.Show("You've updated successfully!", "Successful Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            conn.Close();
            this.Close();
    }
    

    这是我的gridview单元格双击代码:

    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 = this.dataGridView1.CurrentRow.Cells[6].Value as byte[];
    
            if (pic != null)
            {
                MemoryStream stream = new MemoryStream(pic);
                myForm.pb1.Image = Image.FromStream(stream);
            }
            else
                myForm.pb1.Image = null;
    
            myForm.txt_cost.Text = this.dataGridView1.CurrentRow.Cells[7].Value.ToString();
            //  myForm.hello.Text = this.dataGridView1.CurrentRow.Cells[10].Value.ToString();
            myForm.ShowDialog();
    }
    

    我不知道从哪里得到这些错误请帮助

0 个答案:

没有答案