2
//-------------SELECT STATEMENT---------
 private void comboBoxLogin_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxLogin.SelectedIndex.ToString() != string.Empty)
                splitContainer1.Panel2.Enabled = true;
            if (comboBoxLogin.SelectedItem.ToString() == "Create New User")
                groupBoxNewUser.Visible = true;
            else groupBoxNewUser.Visible = false;
            if(comboBoxLogin.SelectedItem.ToString()!="Create New User"){

                string DBConnection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Timesheet.mdf;Integrated Security=True;User Instance=True";
               sqlConnection = new SqlConnection(DBConnection);

                try {
                    sqlConnection.Open();
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();
                    sqlCommand.CommandType = System.Data.CommandType.Text;
                    sqlCommand.CommandText = "SELECT firstname, lastname, NDFuserID, picture FROM UserRegistration WHERE NDFuserID='" +comboBoxLogin.SelectedItem  + "'";
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

                    if(sqlDataReader.Read())
                    {
                        labelUserDetails.Text = sqlDataReader["firstname"].ToString() + " " + sqlDataReader["lastname"].ToString();
                        byte[] pictureByteReader = (byte[])sqlDataReader["picture"];
                        MemoryStream ms = new MemoryStream(pictureByteReader);
                        Image picture = Image.FromStream(ms);
                        pictureBoxUserDetails.Image = picture;
                    }

                    comboBoxItems.Refresh();
                }
                catch(Exception ex){
                    MessageBox.Show(ex.ToString());
                }
                finally{
                sqlConnection.Close();
                }
            }
        }
//--------------------INSERT STATEMENT-------------------------

private void btnCreateNewUser_Click(object sender, EventArgs e)
        {
            string DBConnection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Timesheet.mdf;Integrated Security=True;User Instance=True";
            sqlConnection = new SqlConnection(DBConnection);

            try
            {
                //--Insert statement for a picture-----------------------
                FileInfo fileImage = new FileInfo(txtPictureURL.Text);
                var fileLength = fileImage.Length;
                byte[] picutreByte = new byte[Convert.ToInt32(fileLength)];
                FileStream fileStreams = new FileStream(txtPictureURL.Text, FileMode.Open, FileAccess.Read, FileShare.Read);
                int readByte = fileStreams.Read(picutreByte, 0, Convert.ToInt32(fileLength));
                fileStreams.Close();

                sqlConnection.Open();
                SqlCommand sqlCommand = sqlConnection.CreateCommand();
                sqlCommand.CommandType = System.Data.CommandType.Text;
                sqlCommand.CommandText = "INSERT INTO UserRegistration(firstname, lastname, NDFuserID, phone, picture) VALUES(@firstname, @lastname, @NDFuserID, @phone, @picture)";

                sqlCommand.Parameters.Add("@firstname", SqlDbType.NVarChar, 50);
                sqlCommand.Parameters.Add("@lastname", SqlDbType.NVarChar, 50);
                sqlCommand.Parameters.Add("@NDFuserID", SqlDbType.NChar, 10);
                sqlCommand.Parameters.Add("@phone", SqlDbType.NVarChar);
                sqlCommand.Parameters.Add("@picture", SqlDbType.Image);

                sqlCommand.Parameters["@firstname"].Value = txtFirstName.Text;
                sqlCommand.Parameters["@lastname"].Value = txtLastname.Text;
                sqlCommand.Parameters["@NDFuserID"].Value = "NDF-" +txtUserID.Text;
                sqlCommand.Parameters["@phone"].Value = maskedtxtPhone.Text;
                sqlCommand.Parameters["@picture"].Value = picutreByte;
                sqlCommand.ExecuteNonQuery();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                sqlConnection.Close();
                txtFirstName.Text = "";
                txtLastname.Text = "";
                txtPictureURL.Text = "";
                txtUserID.Text = "";
                maskedtxtPhone.Text = "";
            }
        }

I do not know which of this has a problem. either the insert statement or the select statement. When i insert it does not give any exception but when I try to select it show an exception and the picture appears blurred in the picturebox. What have i done wrong? Please help.Thanks.

ken4ward
  • 2,246
  • 5
  • 49
  • 89

1 Answers1

1

While there are easier ways to accomplish some of your steps such as
byte[] PictureBytes = File.ReadAllBytes(txtPictureURL.Text);
Also the comment from @Khan should be heeded.

Neither of your methods appear to be causing any degradation in the copied. I suspect the PictureBox properties might be scaling or stretching the image.

To fix that issue change the PictureBox.SizeMode property to AutoSize.
If the image is larger than the PictureBox, then you can implement scrollbars like this answer: https://stackoverflow.com/a/4710193/2549384

Community
  • 1
  • 1
Moon
  • 1,141
  • 2
  • 11
  • 25