Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
    try
    {
        string[] validFileTypes={"bmp","gif","png","jpg","jpeg","JPG","BMP","GIF","PNG","JPEG"};
    string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
    bool isValidFile = false;
    for (int i = 0; i < validFileTypes.Length; i++)
    {
        if (ext == "." + validFileTypes[i] )
        {
            isValidFile = true;
            break;
        }
    }
    if (!isValidFile)
    {
        Response.Write("<script language='javascript'>alert('Invailed File')</script>");
    }
    else if(Image2.ImageUrl==null)
    {
        //delete();
        Byte[] bytes = null;
        if (FileUpload1.HasFile)
        {

            string filename = FileUpload1.PostedFile.FileName;
            string filePath = Path.GetFileName(filename);
            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            bytes = br.ReadBytes((Int32)fs.Length);

        }
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        SqlCommand command = new SqlCommand(
         "INSERT INTO " + U_name.Text + " (U_name, Image) " +
         "Values(@U_name, @Image)", con);
        command.Parameters.Add("@U_name",
        SqlDbType.NVarChar, 20).Value = U_name.Text.Trim();
        command.Parameters.Add("@Image",
        SqlDbType.Binary).Value = bytes;
        con.Open();
        command.ExecuteNonQuery();
        Label2.ForeColor = System.Drawing.Color.Green;
        Label2.Text = "Successfully";
    }
        else
    {

        Byte[] bytes = null;
        if (FileUpload1.HasFile)
        {
            string filename = FileUpload1.PostedFile.FileName;
            string filePath = Path.GetFileName(filename);
            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            bytes = br.ReadBytes((Int32)fs.Length);
        }
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        SqlCommand command = new SqlCommand("UPDATE " + U_name.Text + " SET Image=@Image " +"", con);
        command.Parameters.Add("@Image",SqlDbType.Binary).Value = bytes;
        con.Open();
        command.ExecuteNonQuery();
        Label2.ForeColor = System.Drawing.Color.Green;
        Label2.Text = "UPDATE Successfully";
    }

}
Posted
Comments
Afzaal Ahmad Zeeshan 20-Dec-14 3:45am    
And what is the error that you're encountering?
Praveen Kumar Upadhyay 20-Dec-14 3:49am    
Can you please explain what you want.
iamvinod34 20-Dec-14 4:08am    
else if(Image2.ImageUrl==null)------>UPDATE SUCCESSFULY IS CAME
else if(Image2.ImageUrl!=NULL)------>INSERT SUCCESSFULLY
but i want user first time upload insert command.
user second time upload update command executed.
two commands only one button tell me
plz help me
Tushar sangani 20-Dec-14 6:22am    
first check condition for
your Database that user insert or not
if Insert Then use update else use insert
iamvinod34 20-Dec-14 6:34am    
how to check image data available or not
plz help me

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