防止生成错误 [英] prevent to generate a error

查看:69
本文介绍了防止生成错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想如果用户没有选择图像,则该值应该在数据库中转到空。

如果未选择图像则引发错误。







i want if user has not select the image the value should goto null in database.
it is raising an error if image is not selected.



protected void btnsubmit_Click(object sender, EventArgs e)
    {

        try
        {
            filename = System.IO.Path.GetFileName(upldphoto.PostedFile.FileName);
           filename = GetUniqueFileName(filename);
           if (filename != null)
           {
               upldphoto.SaveAs(Server.MapPath("../Upload/Docters/" + filename));
           }

           bool a = obj.saveRecord(1,txtName.Text,txtPhone.Text,txtEmail.Text,txtAddress.Text,txtExpertise.Text,txtExp.Text,filename,txtQualification.Text);

           if (a == true)
           {
               Lblinfo.Text = "Record has been succesfully saved";
               txtName.Text = "";
               txtAddress.Text = "";
               txtEmail.Text = "";
               txtExpertise.Text = "";
               txtExp.Text = "";
               txtPhone.Text = "";
               txtQualification.Text = "";

           }
           else
               //Lblinfo.Text = "Record Not Saved";
           Lblinfo.Text = a.ToString();

        }


        catch (SqlException ex)
        {
            Response.Write(ex);



        }
    }

推荐答案

你可以检查一下先有一个文件。请参阅 http://asp.net-tutorials.com/controls/file-upload-control/ [ ^ ]示例。
You can check if there is a file first. See http://asp.net-tutorials.com/controls/file-upload-control/[^] for an example.






检查
Hi,

Check
upldphoto.PostedFile

为null然后将null传递给你的saveRecord函数。



如果你没有选择文件但是试图访问

is null then pass null to your saveRecord function.

If you did not select a file but try to access

upldphoto.PostedFile.FileName

然后它会抛出一个未设置为对象实例的对象错误。

then it will throw an error of object not set to a instance of an object.


这篇关于防止生成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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