troubleshooting Question

GRIDVIEW UPLOAD

Avatar of BOEING39
BOEING39 asked on
C#ASP.NET
15 Comments1 Solution469 ViewsLast Modified:
I have attached the code behind for 'GRIDVIEW UPLOAD".   This code works fine for "JPEG" files only.   Need assistance with code to upload the following extensions:   .xlsx, .doc and .pdf formats......


using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
  protected void Button1_Click(object sender, EventArgs e)
  {
    Label1.Visible = true;
    if (FileUpload1.HasFile)
      if (System.IO.Path.GetExtension(FileUpload1.FileName).ToLower() == ".jpg")
    {
      SqlConnection conn =
        new SqlConnection(SqlDataSource1.ConnectionString);
      SqlCommand cmd =
        new SqlCommand
          ("INSERT INTO tblUploads (Filename) VALUES (@Filename); " +
           "SELECT @FileID = SCOPE_IDENTITY();",
           conn);
      cmd.Parameters.Add
        ("@Filename", SqlDbType.NVarChar, 50).Value = FileUpload1.FileName;
      cmd.Parameters.Add("@FileID", SqlDbType.Int, 4);
      cmd.Parameters["@FileID"].Direction = ParameterDirection.Output;
      conn.Open();
      cmd.ExecuteNonQuery();
      FileUpload1.SaveAs
        (Server.MapPath("~/Uploads/" + cmd.Parameters["@FileID"].Value.ToString()
          + ".jpg"));
      GridView1.DataBind();
      Label1.Visible = false;
    }
  }
  protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  {
    GridViewRow row = GridView1.SelectedRow;
    Image1.ImageUrl = "~/uploads/" + row.Cells[1].Text + ".jpg";
    Image1.AlternateText = "Original filename: " + row.Cells[2].Text;
  }
}
ASKER CERTIFIED SOLUTION
ddayx10

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 15 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 15 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros