Click here to Skip to main content
16,015,594 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Dear All,
I am developing a member registration form. it also have file upload for uploading member photo. I created stored Procedure for inserting member information. when user doesn't select image then it display error on parameter for image in c# coding because image bytes is null so how can i ignore this error.

i have given condition for
MIDL
if (uploadPhoto.PostedFile.FileName != "")
        {
        }

please tell me solution for that
Posted

If there is no image then write else condition like this :

C#
string fileName = string.Empty;

if (txtImage.HasFile && var == true)
{
fileName = @"~\Users\" + txtImage.FileName.ToString();
txtImage.SaveAs(Server.MapPath("~/Users/") + txtImage.FileName.ToString());
}
else
{
fileName = @"~\Images\" + "Nophoto.png";
}


Don't forger to mark as answer if it helps. :)
 
Share this answer
 
Hai, if the user is not selecting the image, in the else part u can send null value as parameter to that stored procedure.
 
Share this answer
 
hi..
i am jst uploading and saving images in database, images are displayed in GridView...

in this iam not using a stored procedure but you can make neccesary changes to fit into your application..



<pre lang="C++">protected void btnUpload_Click(object sender, EventArgs e)
{
 string strImageName = txtName.Text.ToString();
 if (FileUpload1.PostedFile != null &&
     FileUpload1.PostedFile.FileName != "")
  {
   byte[] imageSize = new byte
                 [FileUpload1.PostedFile.ContentLength];
  HttpPostedFile uploadedImage = FileUpload1.PostedFile;
  uploadedImage.InputStream.Read
     (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);

 // Create SQL Connection
  SqlConnection con = new SqlConnection();
  con.ConnectionString = ConfigurationManager.ConnectionStrings
                         ["ConnectionString"].ConnectionString;

 // Create SQL Command

 SqlCommand cmd = new SqlCommand();
 cmd.CommandText = "INSERT INTO Images(ImageName,Image)" +
                   " VALUES (@ImageName,@Image)";
 cmd.CommandType = CommandType.Text;
 cmd.Connection = con;

 SqlParameter ImageName = new SqlParameter
                     ("@ImageName", SqlDbType.VarChar, 50);
 ImageName.Value = strImageName.ToString();
 cmd.Parameters.Add(ImageName);

 SqlParameter UploadedImage = new SqlParameter
               ("@Image", SqlDbType.Image, imageSize.Length);
 UploadedImage.Value = imageSize;
 cmd.Parameters.Add(UploadedImage);
 con.Open();
 int result = cmd.ExecuteNonQuery();
 con.Close();
 if (result > 0)
 lblMessage.Text = "File Uploaded";
 GridView1.DataBind();
 }
}



Happy Coding...:cool:
 
Share this answer
 
v2
Lalit Kumar 5733 wrote:
please tell me solution for that

There can be various ways:
WAY 1: Keep two SP's(one that has image as parameter and other that doesn't). Based on the image selection call the SP.

WAY 2: In SP, check the value of the image parameter. Use a IF statement in SP and use the insert statement accordingly

WAY 3: Whenever user doesnot select any image, use a pre-defined dummy image

There can be more... based on what suits you and your application you can handle the same.
 
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