i need to save this record without an image if its necessary.
this is my code i have used the if statement too:
private void btnsave_Click(object sender, EventArgs e)
{
try
{
string commandText = "INSERT INTO Stock_Jewelry VALUES(@Stock_Type,@stock_no,@Quantity,@image)";
SqlCommand command = new SqlCommand(commandText, conn);
command.Parameters.Add("@Stock_Type", SqlDbType.VarChar);
command.Parameters["@Stock_Type"].Value = Stock_Type.Text;
command.Parameters.Add("@stock_no", SqlDbType.NVarChar);
command.Parameters["@stock_no"].Value = txt_stock_no.Text;
command.Parameters.Add("@Quantity", SqlDbType.Int);
command.Parameters["@Quantity"].Value = txt_qty.Text;
if(pb1 != null) {
MemoryStream stream = new MemoryStream();
pb1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = stream.ToArray();
command.Parameters.AddWithValue("@image", pic);
}
else {
pb1 = null;
}
command.ExecuteNonQuery();
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);
}
}
Edit from comment:
i can save with an image if i save without an image it says:
object reference not set to an instance of an object