Click here to Skip to main content
15,905,558 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.



C#
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);



        }
    }
Posted
Comments
Sergey Alexandrovich Kryukov 24-Sep-13 9:26am    
What is "goto null in database"? It looks like your question has nothing to do with database but is about UI, but then ask about UI.
—SA
Mohd Arif Khan 24-Sep-13 9:28am    
i mean in filename variable which is stored in database as image name. in that field
Sergey Alexandrovich Kryukov 24-Sep-13 9:39am    
Is the following right? If the image is not selected, the database should not be modified. Or not?
—SA
ArunRajendra 25-Sep-13 0:00am    
What is the error. post it?
Mohd Arif Khan 25-Sep-13 0:18am    
thanks for your concern i have solved it.

You can check if there is a file first. See http://asp.net-tutorials.com/controls/file-upload-control/[^] for an example.
 
Share this answer
 
Hi,

Check
upldphoto.PostedFile
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.
 
Share this answer
 

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