在C#(winforms)中更新图像时,空路径不是合法的例外。 [英] Empty path is not legal exception while updating images in C# (winforms).

查看:56
本文介绍了在C#(winforms)中更新图像时,空路径不是合法的例外。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在为我的朋友办公室开发1个桌面应用程序,在该应用程序中我可以存储多个图像,我可以检索相同的信息..我的问题是在点击更新按钮后检索信息后我得到运行时异常即(空路径不合法例外)..



这里是我在updatebutton方法下编写的代码:



Hi Everyone,

I am developing 1 desktop application to my friend office,in that application i can store multiple images and i can retrieve the information for the same..My issue here is after retrieving the information when i click on update button I'm getting runtime exception i.e (Empty path is not legal exception)..

Here is the code which I wrote under updatebutton method:

public partial class CustomerDetails : Form
    {

        string imageloc = "";
        string imageloc1 = "";
        string imageloc2 = "";
        string imageloc3 = "";
        string imageloc4= " ";
   public static Byte[] Imagesave(string imagelocation)
        {
            byte[] img = null;
           /*Empty path is not legal error*/ FileStream fs = new FileStream(imagelocation, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            img = br.ReadBytes((int)fs.Length);
            fs.Close();
            br.Close();
            return img;
        }
private void btnupdateinfo_Click(object sender, EventArgs e)
        {

            string scn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString;
            using (SqlConnection cn = new SqlConnection(scn))
            {
                using (SqlCommand cmd = new SqlCommand("SP_UpdateInfo", cn))
                {
                    try
                    {


                        cmd.CommandType = CommandType.StoredProcedure;

                        cmd.Parameters.AddWithValue("@Hp_Number", tbhpnum.Text);
                        cmd.Parameters.AddWithValue("@Customer_Name", tbcusnam.Text);
                        cmd.Parameters.AddWithValue("@Customer_Contact_Number", tbcusmblno.Text);
                        cmd.Parameters.AddWithValue("@Guarantor_Name", tbguanam.Text);
                        cmd.Parameters.AddWithValue("@Guarantor_Contact_Number", tbguamblno.Text);
                        cmd.Parameters.AddWithValue("@Hp_Date", DateTime.Parse(tbhpdat.Text));
                        cmd.Parameters.AddWithValue("@Customer_Address", tbcusadd.Text);
                        cmd.Parameters.AddWithValue("@Vehicle", tbveh.SelectedItem.ToString());
                        cmd.Parameters.AddWithValue("@Vehicle_Model", tbvehmod.SelectedItem.ToString());
                        cmd.Parameters.AddWithValue("@Vehicle_Number", tbvehnum.Text);
                        cmd.Parameters.AddWithValue("@Chasis_Number", tbchanum.Text);
                        cmd.Parameters.AddWithValue("@Engine_Number", tbengnum.Text);
                        cmd.Parameters.AddWithValue("@FC_Date", DateTime.Parse(tbfcdat.Text));
                        cmd.Parameters.AddWithValue("@Insurance_Date", DateTime.Parse(tbinsdat.Text));
                        cmd.Parameters.AddWithValue("@Insurance_Amount",                               Convert.ToInt32(tbinsamt.Text));
                        cmd.Parameters.AddWithValue("@Paid_Amount", Convert.ToInt32(tbpaiamt.Text));
                        cmd.Parameters.AddWithValue("@Paid_Date", DateTime.Parse(tbpaidat.Text));
                        cmd.Parameters.AddWithValue("@Vehicle_Pic",Imagesave(imageloc));
                        cmd.Parameters.AddWithValue("@Customer_Pic", Imagesave(imageloc1));
                        cmd.Parameters.AddWithValue("@Guarantor_Pic", Imagesave(imageloc2));
                        cmd.Parameters.AddWithValue("@Documents_Pic", Imagesave(imageloc3));
                        cmd.Parameters.AddWithValue("@Insurance_Pic", Imagesave(imageloc4));

                        if (cn.State != ConnectionState.Open)
                            cn.Open();

                        int count = cmd.ExecuteNonQuery();
                        if (count == 1)
                        {
                            MessageBox.Show(count.ToString() + "Customer(s) Record(s) has Updated                     Successfully .", "Succes", MessageBoxButtons.OK);
                        }


                    }

                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.ToString());

                    }
                    finally
                    {
                        if (cn.State == ConnectionState.Open)
                            cn.Close();
                    }
                }
            }

        }

推荐答案





根据例外,异常是累积的,因为你的任何变量imageloc都是空的,为什么。

你需要先验证所有你的本地变量。

imageloc,imageloc1 ... imageloc4然后转换成Image字节。



谢谢和问候,

Maruf
Hi,

As per the exception, the exception is accrued because any of your variable imageloc is empty that why.
you need to validation first all your local variable.
imageloc, imageloc1...imageloc4 then convert into Image bytes.

Thanks and Regards,
Maruf






从数据库中检索信息后,如果我没有对图片框进行任何更改那个时候我才得到这个例外,但如果我更新了图片盒中的所有现有图片,那么我没有得到任何例外......
Hi,

After retrieving information from the database,if i doesn't make any changes in pictureboxes that time only i'm getting this exception,but if i update all the existing pictures from the pictureboxes then i didn't get any exception...


这篇关于在C#(winforms)中更新图像时,空路径不是合法的例外。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆