简体   繁体   中英

Getting a 'Parameter Not Valid' error, when reading images from Database

I'm currently having issues with a simple win forms application i'm building. I'm am currently getting a parameter not valid error, when i try to read images from a DB.

Any help will be most appreciated. Thanks in advance :)

My DB table Definition is as follow:

CREATE TABLE [dbo].[Employee] (
    [EmployeeID]   INT          IDENTITY (1, 1) NOT NULL,
    [EmployeeName] VARCHAR (50) NULL,
    [EmployeeAge]  INT          NULL,
    [EmployeePic]  IMAGE        NULL,
    PRIMARY KEY CLUSTERED ([EmployeeID] ASC)
);

The image field in the DB table is of type Image.

Below is my code:

    private void Form1_Load(object sender, EventArgs e)
    {
        LoadDataEntryScreenDATA();
    }

    public void LoadDataEntryScreenDATA()
    {
        //Displays DataGrid data
        SqlDataAdapter adap = new SqlDataAdapter("select EmployeeID, EmployeeName, EmployeeAge from Employee", SessionInfo.con);
        DataSet ds = new System.Data.DataSet();
        adap.Fill(ds);
        dataGridView_EmployeeList.DataSource = ds.Tables[0];
    }

    private void picImage_Click(object sender, EventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.InitialDirectory = @"C:\";
        dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|All Files(*.*)|*.*";
        dlg.Title = "Select Firearm Picture.";
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            string picLoc = dlg.FileName.ToString();
            picboxEmployeePic.ImageLocation = picLoc;
        }
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        string Query = "insert into Employee (EmployeeName, EmployeeAge, EmployeePic)" +
            "values ('" + this.txtEmployeeName.Text + "','" + Convert.ToInt32(this.txtEmployeeAge.Text) + "','" + this.picboxEmployeePic.Image + "');";
        SqlCommand cmdDataBase = new SqlCommand(Query, SessionInfo.con);
        SqlDataReader Reader;
        try
        {
            SessionInfo.con.Open();
            Reader = cmdDataBase.ExecuteReader();
            MessageBox.Show("New Employee User added", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            while (Reader.Read())
            {

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            SessionInfo.con.Close();
        }
        LoadDataEntryScreenDATA();
    }

    private void dataGridView_EmployeeList_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow row = this.dataGridView_EmployeeList.Rows[e.RowIndex];

            txtEmployeeName.Text = row.Cells["EmployeeName"].Value.ToString();
            txtEmployeeAge.Text = row.Cells["EmployeeAge"].Value.ToString();

            string QueryImage = "select EmployeePic from Employee where EmployeeID='" + row.Cells["EmployeeID"].Value.ToString() + "'";
            SqlCommand cmdDataBaseImage = new SqlCommand(QueryImage, SessionInfo.con);
            SqlDataReader ReaderImage;
            try
            {
                SessionInfo.con.Open();
                ReaderImage = cmdDataBaseImage.ExecuteReader();
                while (ReaderImage.Read())
                {
                    byte[] data = (byte[])ReaderImage["EmployeePic"];
                    MemoryStream ms = new MemoryStream(data);
                    picboxEmployeePic.Image = Image.FromStream(ms);
                    //picboxEmployeePic.Image = (Image)ReaderImage["EmployeePic"];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                SessionInfo.con.Close();
            }
        }
    }

Thanks for the advice, in regards to using varbinary(MAX) and also using storedprocs. i was able to get it working :)

What i changed in my DB table definition:

CREATE TABLE [dbo].[Employee] (
    [EmployeeID]   INT             IDENTITY (1, 1) NOT NULL,
    [EmployeeName] VARCHAR (50)    NULL,
    [EmployeeAge]  INT             NULL,
    [EmployeePic]  VARBINARY (MAX) NULL,
    PRIMARY KEY CLUSTERED ([EmployeeID] ASC)
);

Below is what i changed in my code:

 private void Form1_Load(object sender, EventArgs e)
        {
            LoadDataEntryScreenDATA();
        }

        public void LoadDataEntryScreenDATA()
        {
            //Displays DataGrid data
            SqlDataAdapter adap = new SqlDataAdapter("select EmployeeID, EmployeeName, EmployeeAge from Employee", SessionInfo.con);
            DataSet ds = new System.Data.DataSet();
            adap.Fill(ds);
            dataGridView_EmployeeList.DataSource = ds.Tables[0];
        }

        private void picImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = @"C:\";
            dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|All Files(*.*)|*.*";
            dlg.Title = "Select Firearm Picture.";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string picLoc = dlg.FileName.ToString();
                picboxEmployeePic.ImageLocation = picLoc;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            byte[] imgData = File.ReadAllBytes(this.picboxEmployeePic.ImageLocation);

            SqlCommand insertCommand = new SqlCommand("spSaveImage", SessionInfo.con);
            insertCommand.CommandType = CommandType.StoredProcedure;
            SessionInfo.con.Open();

            SqlParameter sqlParam1 = insertCommand.Parameters.Add("@EmployeeName", SqlDbType.VarChar, 50);
            sqlParam1.Direction = ParameterDirection.Input;
            sqlParam1.Value = this.txtEmployeeName.Text;
            SqlParameter sqlParam2 = insertCommand.Parameters.Add("@EmployeeAge", SqlDbType.Int);
            sqlParam2.Direction = ParameterDirection.Input;
            sqlParam2.Value = Convert.ToInt32(this.txtEmployeeAge.Text);
            SqlParameter sqlParam3 = insertCommand.Parameters.Add("@EmployeePic", SqlDbType.VarBinary);
            sqlParam3.Direction = ParameterDirection.Input;
            sqlParam3.Value = imgData;

            SqlDataReader rd = insertCommand.ExecuteReader(CommandBehavior.CloseConnection);
            while (rd.Read())
            {

            }
            MessageBox.Show("New Employee added", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            SessionInfo.con.Close();
            LoadDataEntryScreenDATA();
        }

        private void dataGridView_EmployeeList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = this.dataGridView_EmployeeList.Rows[e.RowIndex];

                txtEmployeeID.Text = row.Cells["EmployeeID"].Value.ToString();
                txtEmployeeName.Text = row.Cells["EmployeeName"].Value.ToString();
                txtEmployeeAge.Text = row.Cells["EmployeeAge"].Value.ToString();

                string QueryImage = "select EmployeePic from Employee where EmployeeID='" + row.Cells["EmployeeID"].Value.ToString() + "'";
                SqlCommand cmdDataBaseImage = new SqlCommand(QueryImage, SessionInfo.con);
                SqlDataReader ReaderImage;
                try
                {
                    SessionInfo.con.Open();
                    ReaderImage = cmdDataBaseImage.ExecuteReader();
                    while (ReaderImage.Read())
                    {
                        byte[] data = (byte[])ReaderImage[0];  //(byte[])ReaderImage["EmployeePic"];
                        using (MemoryStream ms = new MemoryStream(data))
                        {
                            picboxEmployeePic.Image = Image.FromStream(ms);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    SessionInfo.con.Close();
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            byte[] imgData = File.ReadAllBytes(this.picboxEmployeePic.ImageLocation);

            SqlCommand insertCommand = new SqlCommand("spUpdateImage", SessionInfo.con);
            insertCommand.CommandType = CommandType.StoredProcedure;
            SessionInfo.con.Open();

            SqlParameter sqlParam1 = insertCommand.Parameters.Add("@EmployeeName", SqlDbType.VarChar, 50);
            sqlParam1.Direction = ParameterDirection.Input;
            sqlParam1.Value = this.txtEmployeeName.Text;
            SqlParameter sqlParam2 = insertCommand.Parameters.Add("@EmployeeAge", SqlDbType.Int);
            sqlParam2.Direction = ParameterDirection.Input;
            sqlParam2.Value = Convert.ToInt32(this.txtEmployeeAge.Text);
            SqlParameter sqlParam3 = insertCommand.Parameters.Add("@EmployeePic", SqlDbType.VarBinary);
            sqlParam3.Direction = ParameterDirection.Input;
            sqlParam3.Value = imgData;
            SqlParameter sqlParam4 = insertCommand.Parameters.Add("@EmployeeID", SqlDbType.Int);
            sqlParam4.Direction = ParameterDirection.Input;
            sqlParam4.Value = Convert.ToInt32(this.txtEmployeeID.Text);

            SqlDataReader rd = insertCommand.ExecuteReader(CommandBehavior.CloseConnection);
            while (rd.Read())
            {

            }
            MessageBox.Show("Employee Information Updated", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            SessionInfo.con.Close();
            LoadDataEntryScreenDATA();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM