Question Able to insert image and saving also but, its showing error when am edit/update image.

Johnykick

New member
Joined
Oct 16, 2019
Messages
1
Programming Experience
Beginner
Able to insert image in form and saving also but, its showing error when am edit/update image and not able to save with update.

When I remove only image code then both Insert and update/edit is working fine.

Can any please help me out on that image error. I will show you code C# below.


C# insert code: (This was working fine)


C#:
public void insert()
{
    SqlConnection connection = new SqlConnection(dbFunctions.connectionstring);
    try
    {
        MemoryStream stream;
        connection.Open();
        SqlCommand command = new SqlCommand {
            Connection = connection,
            CommandType = CommandType.StoredProcedure,
            CommandText = "Pr_Insert_CustomerMaster"
            };
        command.Parameters.Add("@CM_Customer_name", SqlDbType.VarChar).Value = this.CM_Customer_name.Text.ToString();
        command.Parameters.Add("@CM_Address", SqlDbType.VarChar).Value = this.CM_Address.Text.ToString();
        command.Parameters.Add("@CM_Others", SqlDbType.VarChar).Value = this.CM_Others.Text.ToString();
        command.Parameters.Add("@CM_PhoneNo", SqlDbType.VarChar).Value = this.CM_PhoneNo.Text.ToString();
        command.Parameters.Add("@CM_City", SqlDbType.VarChar).Value = this.CM_City.Text.ToString();
        byte[] buffer = null;
        try
        {
            stream = new MemoryStream();
            this.Person.Image.Save(stream, ImageFormat.Jpeg);
            buffer = stream.GetBuffer();
        }
        catch
        {
        }
        command.Parameters.Add("@CM_Image", SqlDbType.Image).Value = buffer;
        byte[] buffer2 = null;
        try
        {
            stream = new MemoryStream();
            this.Proof.Image.Save(stream, ImageFormat.Jpeg);
            buffer2 = stream.GetBuffer();
        }
        catch
        {
        }
        command.Parameters.Add("@CM_Proof", SqlDbType.Image).Value = buffer2;
        command.Parameters.Add("@CM_Lodge", SqlDbType.VarChar).Value = dbFunctions.Lodge;
        command.Parameters.Add("@CM_GSTNo", SqlDbType.VarChar).Value = this.txt_GSTNo.Text.ToString();
        if (this.CM_GuestCompanyName.Text == "")
        {
            command.Parameters.Add("@CM_Guest_company_name", SqlDbType.VarChar).Value = "0";
        }
        else
        {
            command.Parameters.Add("@CM_Guest_company_name", SqlDbType.VarChar).Value = this.CM_GuestCompanyName.SelectedValue.ToString();
        }
        command.Parameters.Add("@CM_PassPort_No", SqlDbType.VarChar).Value = this.CM_PassPort_No.Text.ToString();
        command.Parameters.Add("@CM_Visa_No", SqlDbType.VarChar).Value = this.CM_Visa_No.Text.ToString();
        command.Parameters.Add("@CM_Issue_Date", SqlDbType.VarChar).Value = this.CM_Issue_Date.Text.ToString();
        command.Parameters.Add("@CM_Expire_Date", SqlDbType.VarChar).Value = this.CM_Expire_Date.Text.ToString();
        command.Parameters.Add("@CM_Place", SqlDbType.VarChar).Value = this.CM_IssuePlace.Text.ToString();
        command.Parameters.Add("@CM_Country", SqlDbType.VarChar).Value = this.CM_Country.Text.ToString();
        command.Parameters.Add("@CM_Visa_IssDate", SqlDbType.VarChar).Value = this.CM_Visa_IssDate.Text.ToString();
        command.Parameters.Add("@CM_Visa_Expiry_Date", SqlDbType.VarChar).Value = this.CM_Visa_IssDate.Text.ToString();
        command.Parameters.Add("@Proof_Name", SqlDbType.VarChar).Value = this.Proof_Name.Text.ToString();
        command.Parameters.Add("@Proof_Id", SqlDbType.VarChar).Value = this.Proof_Id.Text.ToString();
        command.ExecuteNonQuery();
        MessageBox.Show("Details Saved Successfully ", "Sucess", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        this.Clear();
        this.display();
    }
    catch (Exception exception)
    {
        dbFunctions.Logs(exception.Message, dbFunctions.username);
        MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
}



C# Update code: (its showing error, see Red color error)

C#:
public void Update()
{
    SqlConnection connection = new SqlConnection(dbFunctions.connectionstring);
    try
    {
        MemoryStream stream;
        connection.Open();
        SqlCommand command = new SqlCommand {
            Connection = connection,
            CommandType = CommandType.StoredProcedure,
            CommandText = "Pr_Update_CustomerMaster"
            };
        command.Parameters.Add("@CM_Customer_name", SqlDbType.VarChar).Value = this.CM_Customer_name.Text.ToString();
        command.Parameters.Add("@CM_Address", SqlDbType.VarChar).Value = this.CM_Address.Text.ToString();
        command.Parameters.Add("@CM_Others", SqlDbType.VarChar).Value = this.CM_Others.Text.ToString();
        command.Parameters.Add("@CM_PhoneNo", SqlDbType.VarChar).Value = this.CM_PhoneNo.Text.ToString();
        command.Parameters.Add("@CM_City", SqlDbType.VarChar).Value = this.CM_City.Text.ToString();
        byte[] buffer = null;
        try
        {
            stream = new MemoryStream();
            this.Person.Image.Save(stream, ImageFormat.Jpeg);
            buffer = stream.GetBuffer();
        }
        catch
        {
        }
        command.Parameters.Add("@CM_Image", SqlDbType.Image).Value = buffer;
        byte[] buffer2 = null;
        try
        {
            stream = new MemoryStream();
            this.Proof.Image.Save(stream, ImageFormat.Jpeg);
            buffer2 = stream.GetBuffer();
        }
        catch
        {
        }
        command.Parameters.Add("@CM_Proof", SqlDbType.Image).Value = buffer2;
        command.Parameters.Add("@CM_Lodge", SqlDbType.VarChar).Value = dbFunctions.Lodge;
        command.Parameters.Add("@CM_GSTNo", SqlDbType.VarChar).Value = this.txt_GSTNo.Text.ToString();
        if (this.CM_GuestCompanyName.Text == "")
        {
            command.Parameters.Add("@CM_Guest_company_name", SqlDbType.VarChar).Value = "0";
        }
        else
        {
            command.Parameters.Add("@CM_Guest_company_name", SqlDbType.VarChar).Value = this.CM_GuestCompanyName.SelectedValue.ToString();
        }
        command.Parameters.Add("@CM_PassPort_No", SqlDbType.VarChar).Value = this.CM_PassPort_No.Text.ToString();
        command.Parameters.Add("@CM_Visa_No", SqlDbType.VarChar).Value = this.CM_Visa_No.Text.ToString();
        command.Parameters.Add("@CM_Issue_Date", SqlDbType.VarChar).Value = this.CM_Issue_Date.Text.ToString();
        command.Parameters.Add("@CM_Expire_Date", SqlDbType.VarChar).Value = this.CM_Expire_Date.Text.ToString();
        command.Parameters.Add("@CM_Place", SqlDbType.VarChar).Value = this.CM_IssuePlace.Text.ToString();
        command.Parameters.Add("@CM_Country", SqlDbType.VarChar).Value = this.CM_Country.Text.ToString();
        command.Parameters.Add("@CM_Visa_IssDate", SqlDbType.VarChar).Value = this.CM_Visa_IssDate.Text.ToString();
        command.Parameters.Add("@CM_Visa_Expiry_Date", SqlDbType.VarChar).Value = this.CM_Visa_IssDate.Text.ToString();
        command.Parameters.Add("@Proof_Name", SqlDbType.VarChar).Value = this.Proof_Name.Text.ToString();
        command.Parameters.Add("@Proof_Id", SqlDbType.VarChar).Value = this.Proof_Id.Text.ToString();
        command.ExecuteNonQuery();
        MessageBox.Show("Details Saved Successfully ", "Sucess", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        this.Clear();
        this.display();
    }
    catch (Exception exception)
    {
        dbFunctions.Logs(exception.Message, dbFunctions.username);
        MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
}
 
Last edited by a moderator:
Firstly, please don't post unformatted code. It is very hard to read, making it time consuming and error prone. Secondly, please don;t tell us that an error occurs without telling us EXACTLY where and EXACTLY what. You need to get rid of all those empty catch blocks for a start. If an exception can reasonably be thrown in those places than deal with it. At the very least you should have a Debug.WriteLine call in there to display the details in the Output window while debugging. If there's no specific expectation of a reasonable exception then get rid of the try block altogether.

Once you have provided all the relevant information and we know what we're looking for, we can probably come up with a solution fairly quickly.
 
How are we supposed to know what is inside your stored procedures Pr_Insert_CustomerMaster versus Pr_Update_CustomerMaster if you don't show us the code for those stored procedures?

And even if you did show us the contents of the stored procedures, you are in the wrong forum. This is a C# forum, not a SQL forum.
 
Back
Top Bottom