Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I click update button without changing the picture, it raises an error, "Value can not be null". even if am inserting a new row it gives the same error.

C#
private void UpdateButton_Click(object sender, EventArgs e)
        {
            connect.Open();
            //byte[] image;

           SqlCommand cmd = connect.CreateCommand();
           cmd = new SqlCommand("sp_UpdateDepartStaff", connect);
           cmd.CommandType = CommandType.StoredProcedure;

           //Read Image Bytes into a byte array
           byte[] imageData = ReadFile(profilePicPictureBox.ImageLocation);

            try
            {

                cmd.Parameters.AddWithValue("@StaffID", SqlDbType.Int).Value = logInIDTextBox.Text.ToString();
                cmd.Parameters.AddWithValue("@LogInID", SqlDbType.Int).Value = staffIDTextBox.Text.ToString();
                cmd.Parameters.AddWithValue("@StaffRoleID", SqlDbType.Int).Value = staffRoleIDTextBox.Text.ToString();
                cmd.Parameters.AddWithValue("@FirstName", SqlDbType.NVarChar).Value = firstNameTextBox.Text;
                cmd.Parameters.AddWithValue("@LastName", SqlDbType.NVarChar).Value = lastNameTextBox.Text;
                cmd.Parameters.AddWithValue("@UserName", SqlDbType.NVarChar).Value = userNameTextBox.Text;
                cmd.Parameters.AddWithValue("@Password", SqlDbType.NVarChar).Value = passwordTextBox.Text;
                cmd.Parameters.AddWithValue("@Email", SqlDbType.NVarChar).Value = emailTextBox.Text;
                cmd.Parameters.AddWithValue("@Phone", SqlDbType.NVarChar).Value = phoneTextBox.Text;

                cmd.Parameters.AddWithValue("@StaffRole", SqlDbType.NVarChar).Value = staffRoleTextBox.Text;
                cmd.Parameters.AddWithValue("@Department", SqlDbType.NVarChar).Value = departmentTextBox.Text;
                cmd.Parameters.Add(new SqlParameter("@ProfilePic", (object)imageData));


                cmd.ExecuteNonQuery();
                MessageBox.Show("Updated Successfully!");
                connect.Close();


            }

            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void InsertButton_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = connect.CreateCommand();
            cmd = new SqlCommand("sp_InsertDepartStaff", connect);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
              
                //Read Image Bytes into a byte array
                byte[] imageData = ReadFile(profilePicPictureBox.ImageLocation);

                connect.Open();


                //connect.Open();
                SqlParameter parm = new SqlParameter("@StaffID", SqlDbType.Int);
                parm.Direction = ParameterDirection.Output;
                cmd.Parameters.Add(parm);
                SqlParameter parm1 = new SqlParameter("@LoginID", SqlDbType.Int);
                parm1.Direction = ParameterDirection.Output;
                cmd.Parameters.Add(parm1);
                SqlParameter parm4 = new SqlParameter("@StaffRoleID", SqlDbType.Int);
                parm4.Direction = ParameterDirection.Output;
                cmd.Parameters.Add(parm4);


                cmd.Parameters.AddWithValue("@FirstName", SqlDbType.NVarChar).Value = firstNameTextBox.Text;
                cmd.Parameters.AddWithValue("@LastName", SqlDbType.NVarChar).Value = lastNameTextBox.Text;
                cmd.Parameters.AddWithValue("@UserName", SqlDbType.NVarChar).Value = userNameTextBox.Text;
                cmd.Parameters.AddWithValue("@Password", SqlDbType.NVarChar).Value = passwordTextBox.Text;
                cmd.Parameters.AddWithValue("@Email", SqlDbType.NVarChar).Value = emailTextBox.Text;
                cmd.Parameters.AddWithValue("@Phone", SqlDbType.NVarChar).Value = phoneTextBox.Text;

                cmd.Parameters.AddWithValue("@StaffRole", SqlDbType.NVarChar).Value = staffRoleTextBox.Text;
                cmd.Parameters.AddWithValue("@Department", SqlDbType.NVarChar).Value = departmentTextBox.Text;
                cmd.Parameters.Add(new SqlParameter("@ProfilePic", (object)imageData));

                //clear();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Saved Successfully!");
                connect.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void BrowseButton_Click(object sender, EventArgs e)
        {
           
            //Ask user to select file.
            OpenFileDialog dlg = new OpenFileDialog();
            DialogResult dlgRes = dlg.ShowDialog();
            if (dlgRes != DialogResult.Cancel)
            {
                //Set image in picture box
                profilePicPictureBox.ImageLocation = dlg.FileName;

                //Provide file path in txtImagePath text box.
                //txtImagePath.Text = dlg.FileName;
           }

        }

             //Open file in to a filestream and read data in a byte array.
        byte[] ReadFile(string sPath)
        {
            //Initialize byte array with a null value initially.
            MemoryStream mstr = new MemoryStream();

            byte[] data = mstr.GetBuffer();    

            //Use FileInfo object to get file size.
           FileInfo fInfo = new FileInfo(sPath);  // error on this code(value cannot be null)
           // long numBytes = fInfo.Length;
            
            //Open FileStream to read file
            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

            //Use BinaryReader to read file stream into byte array.
            BinaryReader br = new BinaryReader(fStream);

            //When you use BinaryReader, you need to supply number of bytes to read from file.
            //In this case we want to read entire file. So supplying total number of bytes.
            //data = br.ReadBytes((int)numBytes);
            return data;
       }
Posted
Updated 2-Dec-13 22:24pm
v3
Comments
Pheonyx 3-Dec-13 4:22am    
What debugging steps have you taken?
skydger 3-Dec-13 8:56am    
What is the target table structure? Does it have any 'not null' constraints?
Member 10424259 4-Dec-13 0:17am    
no everything is set to null
Karthik_Mahalingam 4-Dec-13 4:03am    
while debugging, in which line you r getting error ???

1 solution

First of all I suggest you to rewrite your AddWithValue code like this:

C#
// Why to use SqlDbType.[sometype] at AddWithValue method, it pointless?
cmd.Parameters.AddWithValue("@FirstName", SqlDbType.NVarChar).Value = firstNameTextBox.Text;
cmd.Parameters.AddWithValue("@FirstName", firstNameTextBox.Text);

Please see SqlParameterCollection.AddWithValue Method[^] for details. AddWithValue expects value, not SqlDbType.
You also could convert values to respective data types of parameters (text to int integer etc) and put this value as second argument.

Secondly. Have you debugged your code? Are you sure that ReadFile method really reads anything from file?
I think not. You should modify your code of ReadFile method. This code does not read anything and your data buffer stays always with 0 bytes long and your stored procedure always receives 'null'. Consider to rewrite this method like following:

C#
 byte[] ReadFile(string sPath)
 {
     //Initialize byte array with a null value initially.
     MemoryStream mstr = new MemoryStream();
     byte[] data = mstr.GetBuffer();

     // Why to use FileInfo?
     FileInfo fInfo = new FileInfo(sPath);  // error on this code(value cannot be null)
     //Open FileStream to read file
     FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

     //Use BinaryReader to read file stream into byte array.
     BinaryReader br = new BinaryReader(fStream);

     mstr.SetLength(fStream.Length);
     fStream.Read(mstr.GetBuffer(), 0, (int)fStream.Length);
     byte[] data = mstr.GetBuffer();

     return data;
}

Good luck :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900