0

I cannot fill records from DataGridView to Textboxes after adding this to my code:

SqlParameter imageParameter = new SqlParameter("@img", SqlDbType.Image);
imageParameter.Value = DBNull.Value;
cmd.Parameters.Add(imageParameter);

I get the following error:

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'

These is my btnSave code:

private void btnSave_Click(object sender, EventArgs e)
{
    ImgLoc = "";
    try
    {
        byte[] img = null;
        if (!string.IsNullOrWhiteSpace(ImgLoc))
        { 
            FileStream fs = new FileStream(ImgLoc, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            img = br.ReadBytes((int)fs.Length);
        }
        //if the image is null, do nothing

        //uploading image from file dialog display

        //Declaring sql database variable
        SqlConnection con;
        SqlCommand cmd;

        con = new SqlConnection("Data Source = Localhost; Initial Catalog = ChurchProject; Integrated Security = True");

        con.Open();

        // using commands to get member records when input
        cmd = new SqlCommand("insert into members(FullName,Gender,DateOfBirth,Auxiliary,Location,Address,"
            + "Telephone,WhatsApp,Email,[Membership Status],"
            + "[Marital Status],[Baptismal Status],Occupation,[Baptismal Year],[Admission Year],[Prev Church],"
            + "Prev_Position,MemberPic)"
            + "values(@name,@sex,@dob,@aux,@loc,@address,@tel,@whatsapp,@email,@memberstat,@marital,@baptismStat,"
            + "@occup,@baptYear,@admission,@prev,@post,@img)",con);
     
        cmd.Parameters.AddWithValue("@name", txtName.Text);
        cmd.Parameters.AddWithValue("@aux", cmbAuxiliary.Text);
        cmd.Parameters.AddWithValue("@dob", dtpDOB.Value);

        SqlParameter imageParameter = new SqlParameter("@img", SqlDbType.Image);
        imageParameter.Value = DBNull.Value;
        cmd.Parameters.Add(imageParameter);

        cmd.Parameters.AddWithValue("@sex", gender);
        cmd.Parameters.AddWithValue("@loc", txtLocation.Text);
        cmd.Parameters.AddWithValue("@address", txtAddress.Text);
        cmd.Parameters.AddWithValue("@tel", txtTelNumber.Text);
        cmd.Parameters.AddWithValue("@whatsapp", txtWhatsApp.Text);
        cmd.Parameters.AddWithValue("@email", txtEmail.Text);
        cmd.Parameters.AddWithValue("@memberstat", cmbMembershipstat.Text);
        cmd.Parameters.AddWithValue("@marital", cmbMarital.Text);
        cmd.Parameters.AddWithValue("@baptismStat", cmbBaptism.Text);
        cmd.Parameters.AddWithValue("@occup", txtOccupation.Text);
        cmd.Parameters.AddWithValue("@baptYear", dtpBaptism.Value);
        cmd.Parameters.AddWithValue("@admission", dtpAdmission.Value);
        cmd.Parameters.AddWithValue("@prev", txtPrevChurch.Text);
        cmd.Parameters.AddWithValue("@post", txtPosition.Text);
        
        if (txtName.Text == "" && dtpDOB.Text == "" && dtpDOB.Text == "" && gender == null)
        {
            MessageBox.Show("All Fields Required!", "Membership Status", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            cmd.ExecuteNonQuery();
            MessageBox.Show("Records Saved Succesfully!", "Membership Status", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        }
        loadMemberRecords();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); ;
    }
}

and this is the code to fill records from DataGridView to Textboxes.

  • 2
    Is the column nullable in the database? – Nikki9696 Aug 24 '20 at 18:12
  • I'm shooting in the dark but, have you tried not specifying the type and let SQL Server infer it? (Assuming the column is nullable): `cmd.Parameters.Add(new SqlParameter("@img", DBNull.Value));` – insane_developer Aug 24 '20 at 18:18
  • yes, the column is nullable in the database – Reuben Hale Aug 24 '20 at 18:18
  • @insane_developer. I did that and had another error >Operand type clash: nvarchar is incompatible with image – Reuben Hale Aug 24 '20 at 18:23
  • I think there is an answer here: https://stackoverflow.com/questions/18170985/null-value-in-a-parameter-varbinary-datatype – insane_developer Aug 24 '20 at 18:25
  • could it also be from here? `sda.SelectCommand = cmd; dt = new DataTable(); sda.Fill(dt); foreach (DataRow dr in dt.Rows) { byte[] img = (Byte[])(dr["MemberPic"]); if (img == null) { picMember.Image = null; } else {MemoryStream ms = new MemoryStream(img); picMember.Image = Image.FromStream(ms); } ;` – Reuben Hale Aug 24 '20 at 18:54

0 Answers0