还能插入图像并保存,但在编辑/更新图像时显示错误。

强尼

新成员
已加入
2019年10月16日
留言内容
1
编程经验
Beginner
能够以格式插入图像并保存,但在编辑/更新图像时显示错误,并且无法通过更新保存。

当我仅删除图像代码时,插入和更新/编辑都可以正常工作。

能帮我解决那个图像错误吗?我将在下面显示代码C#。


C#插入代码:(工作正常)


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#更新代码:(显示错误,请参见红色错误)

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);
    }
}
 
由主持人最后编辑:

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,501
地点
悉尼,澳大利亚
编程经验
10+
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.

一旦您提供了所有相关信息,并且我们知道我们在寻找什么,我们就可以很快找到解决方案。
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,500
地点
弗吉尼亚州切萨皮克
编程经验
10+
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?

而且即使您确实向我们展示了存储过程的内容,您也会陷入错误的论坛。这是C#论坛,而不是SQL论坛。
 
最佳 底部