Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi the below code of button i am facing a problem with it, as it suppose to check if all files has file and then check all uploadfile extension with .jpg or .png if yes then complete the rest of the code
C#
var files = new[] { LogoFileExtention, BizImg1FileExtention, BizImg2FileExtention, BizImg3FileExtention, PersImgFileExtention };
                        var extensions = new[] { ".jpg", ".png"};
                        if ((files.Intersect(extensions).Count()) > 0)
                        {


and if not then move to below code, but what is happening is if the extension is .doc or .pdf it complete as normally without stop on the below part of the code
C#
else
                        {
                            imagesformtLbl.Text = "Images should be in .JPG or .PNG format only";
                        }


C#
protected void btnSave_Click(object sender, EventArgs e)
        {
            string LogoFileExtention = System.IO.Path.GetExtension(logoFileUpload.FileName);
            string BizImg1FileExtention = System.IO.Path.GetExtension(FileUploadimage1.FileName);
            string BizImg2FileExtention = System.IO.Path.GetExtension(FileUploadImage2.FileName);
            string BizImg3FileExtention = System.IO.Path.GetExtension(FileUploadImage3.FileName);
            string PersImgFileExtention = System.IO.Path.GetExtension(persimgFileUpload1.FileName);

            
            if (citiesdrdolst.Items.Count == 0)
            {
                alertpanel.Visible = true;
                StateReqLbl.Text = "Please select state where you live.";
            }



            else
            {

                HttpCookie cookie = Request.Cookies.Get("Location");
                string Location = string.Empty;
                SqlConnection cn = new SqlConnection(sc);
                SqlCommand cmd = new SqlCommand();
                Location = cookie.Value;

                if (CheckBox1.Checked)
                {
                    if (logoFileUpload.HasFile || FileUploadimage1.HasFile || FileUploadImage2.HasFile || FileUploadImage3.HasFile || persimgFileUpload1.HasFile)
                    {
                 
                        
                        var files = new[] { LogoFileExtention, BizImg1FileExtention, BizImg2FileExtention, BizImg3FileExtention, PersImgFileExtention };
                        var extensions = new[] { ".jpg", ".png" };
                        if ((files.Intersect(extensions).Count()) > 0)
                        {

                                string sqlstatment = @"INSERT INTO UserInfo (UID, FN, LN, Password, RePass, Email,Country, State,City, Post, Img, Logo,
RegDate,Address, UsrType,BizCateg,BizSubCateg, CompNme, Facebook, GooglePlus, Twitter, Website, image1, image2, image3) VALUES 
(@UID,@FN,@LN,@Password,@RePass,@Email,@Country,@State,@City,@Post,@Img,@Logo,@RegDate,@Address,@UsrType,@BizCateg,@BizSubCateg,
@CompNme,@Facebook,@GooglePlus,@Twitter,@Website,@image1,@image2,@image3)";

                                cmd.Connection = cn;
                                cmd.CommandType = CommandType.Text;
                                cmd.CommandText = sqlstatment;

                                //Insert the parameters first
                                cmd.Parameters.AddWithValue("@UID", UsrNme.Text);
                                cmd.Parameters.AddWithValue("@FN", fnbox.Text);
                                cmd.Parameters.AddWithValue("@LN", lnamebox.Text);
                   

                                }

                                Response.Redirect("User panel.aspx");
                            }

                        else
                        {
                            imagesformtLbl.Text = "Images should be in .JPG or .PNG format only";
                        }

                        
                      
                    }

                    else
                    {
                         string sqlstatment = @"INSERT INTO UserInfo (UID, FN, LN, Password, RePass, Email,Country, State,City, Post, Img,
RegDate,Address, UsrType,BizCateg,BizSubCateg, CompNme, Facebook, GooglePlus, Twitter, Website) VALUES 
(@UID,@FN,@LN,@Password,@RePass,@Email,@Country,@State,@City,@Post,@Img,@RegDate,@Address,@UsrType,@BizCateg,@BizSubCateg,
@CompNme,@Facebook,@GooglePlus,@Twitter,@Website)";

                                cmd.Connection = cn;
                                cmd.CommandType = CommandType.Text;
                                cmd.CommandText = sqlstatment;

                                //Insert the parameters first
                                cmd.Parameters.AddWithValue("@UID", UsrNme.Text);
                                cmd.Parameters.AddWithValue("@FN", fnbox.Text);
                                cmd.Parameters.AddWithValue("@LN", lnamebox.Text);
                          

                               
                                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                                DataSet ds = new DataSet();
                                ad.SelectCommand = cmd;
                                ad.Fill(ds);
                                Session["UsrNme"] = UsrNme.Text;


                                }

                                Response.Redirect("User panel.aspx");
                    }

                }
                    else
                    {
                        Label1.Text = "please check the box to continue";
                    }

                }

            }
Posted

If you have codes below to some line, then that would execute for sure.

If you don't want to execute the below code, then you need to return or design your event in that way, where that line would be the last line of code.
 
Share this answer
 
Comments
George Jonsson 25-Sep-15 1:00am    
Maybe show an example.
Like...

if(IsAllowedExtension)
{
// Do something.
}
else{
// Do something...
}

// No codes here which should again deal with the label control which shows the error message.

NOTE: I am suggesting this to OP. He needs to debug and identify that line of code which is dealing with the error message label.
George Jonsson 25-Sep-15 1:17am    
You have a point
You cannot compare the whole file name with an extension, test.jpg will never be equal to .jpg.
So you need to make your own compare object, where you take that into consideration.
See my small example below.

You need to create your own class that inherits from IEqualityComparer and implement the two methods.
C#
class ExtensionComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        string extensionX = Path.GetExtension(x);
        string extensionY = Path.GetExtension(y);
        return (extensionX == extensionY);
    }

    public int GetHashCode(string obj)
    {
        string extension = Path.GetExtension(obj.ToString());
        return extension.GetHashCode();
    }
}


Just a little example for how to do it.
C#
string[] files = new string[] { "test.jpg", "test.png", "test.txt" };

string[] extensions = new string[] { ".jpg", ".png" };
bool success = (files.Intersect(extensions, new ExtensionComparer()).Count() > 0);

What you do when you have a success is up to you.
 
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