-4

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

Patrick Artner
  • 43,256
  • 8
  • 36
  • 57
mms mms
  • 37
  • 5
  • allow NULL for the image column in the database .... and add a NULL as param for the image – Patrick Artner Jan 03 '18 at 12:48
  • You need to make image column Nullable for this to work. – Chetan Ranpariya Jan 03 '18 at 12:49
  • "it is not saving" is not a very helpful error description. Please paste the error you get into your question. – Heinzi Jan 03 '18 at 12:51
  • i can save with an image if i save without an image it says: object reference not set to an instance of an object – mms mms Jan 03 '18 at 12:51
  • check `conn` for null - thats the only thing in this code that I see that could also be null – Patrick Artner Jan 03 '18 at 13:48
  • there is no conn = null; – mms mms Jan 03 '18 at 14:12
  • SqlConnection conn; SqlCommand cmd; public S_Jewelry() { try { DB_CONNECTION dbObj = new DB_CONNECTION(); conn = dbObj.getConnection(); } catch (Exception ex) { MessageBox.Show("Can't Open Connection!! " + ex); } InitializeComponent(); } – mms mms Jan 03 '18 at 14:12
  • i have mentioned: SqlConnection conn; – mms mms Jan 03 '18 at 14:12
  • You do not supply any connection string to https://msdn.microsoft.com/en-us/library/system.data.common.dbconnection(v=vs.110).aspx - and yours looks weird. Why is the class spelled DB_CONNECTION() - allcaps? Look up and try https://stackoverflow.com/questions/185474/c-sharp-retrieving-correct-dbconnection-object-by-connection-string – Patrick Artner Jan 03 '18 at 14:25

1 Answers1

0

If your image column allows for null values, change to

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 
{
    command.Parameters.AddWithValue("@image", System.Data.SqlTypes.SqlBinary.Null); 
    // edit: replaced incorreect DBNull.Value due to comment by Heinzi
}

System.Data.SqlTypes.SqlBinary.Null

Currently you are not providing enough parameters to your Sql if pb1 is null. When constructing the Sql it tries to access the SqlParameterCollection attached to your SqlCommand instance. Does not find anything for @image as given in the SqlCommand - hence the error.

Patrick Artner
  • 43,256
  • 8
  • 36
  • 57
  • 1
    If the image column is a `varbinary` column (which it should be), you can't use AddWithValue with DBNull.Value, see https://stackoverflow.com/q/18170985/87698 – Heinzi Jan 03 '18 at 12:52
  • i tried the code as to the answer still it didnt work – mms mms Jan 03 '18 at 12:59
  • my table column is : Image. – mms mms Jan 03 '18 at 12:59
  • sir mine is not a varbinary, it is an image. do you thing varbinary is better than image. – mms mms Jan 03 '18 at 13:05
  • @mmsmms Your **database type** for your column _Image_ is an byte-array. Your code takes the `Image`type and saves its datastream into a `byte[]` which is then appended to your SQL as parameter `'@image'`to be stored. See [ntext-text-and-image-transact-sql](https://docs.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql) - image is just a byte[] – Patrick Artner Jan 03 '18 at 13:08
  • thank you for the code and your time sir.. i tried but still the same error..object reference not set to an instance of an object – mms mms Jan 03 '18 at 13:22
  • @mmsmms: Then you should follow the steps at https://stackoverflow.com/q/4660142/87698 – Heinzi Jan 03 '18 at 13:24
  • Where did i go wrong can you please point it out. i hope i have the correct validation. – mms mms Jan 03 '18 at 14:19
  • i dont know where else to mention null; – mms mms Jan 03 '18 at 14:21
  • please help me out. – mms mms Jan 03 '18 at 14:21