vimal
vimal

Reputation: 19

inserting an image and thenfilepath to the database

I'm designing in my web page:

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
             onflbload(sender, e);
        }
        public void onflbload(object sender, EventArgs e)
        {
            // Create a byte[] from the input file

            int len = flbload.PostedFile.ContentLength;
            byte[] pic = new byte[len];
            flbload.PostedFile.InputStream.Read(pic, 0, len);
            // Insert the image and comment into the database

            SqlConnection connection = new SqlConnection(@"Data Source=DEVI\SQLEXPRESS; 
                          Initial Catalog =cat; Integrated Security=SSPI");

            try
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand("insert into tblphotosettings " +
                    "(BillNo,CustomerName,Address,StartDate,EndDate,Systemurl,Numberofcopies,Amount,Total)  
                    values (@BillNo,@CustomerName,@Address,@StartDate,@EndDate,@Systemurl,@Numberofcopies,@Amount,@Total)", connection);
                cmd.Parameters.Add("@BillNo", SqlDbType.NVarChar).Value = TextBox1.Text;
                cmd.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value =TextBox2.Text;
                cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = TextBox3.Text;
                cmd.Parameters.Add("@StartDate", SqlDbType.NVarChar).Value = Rdbsdate.SelectedDate;
                cmd.Parameters.Add("@EndDate", SqlDbType.NVarChar).Value = Rdbddate.SelectedDate;
                cmd.Parameters.Add("@Systemurl", SqlDbType.Image).Value = pic;
                SqlParameter Src = new SqlParameter("@FilePath", SqlDbType.VarChar, 450);
                Src.Value = pic.GetName();
                cmd.Parameters.Add(Src);

                cmd.Parameters.Add("@Numberofcopies", SqlDbType.NVarChar).Value =TextBox7.Text;
                cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = TextBox8.Text;
                cmd.Parameters.Add("@Total", SqlDbType.NVarChar).Value = TextBox9.Text;
                cmd.ExecuteNonQuery();
            }

            finally
            {
                connection.Close();
            }
        }
    }
 }

My error

Error   1   'System.Array' does not contain a definition for 'GetName'   
and no extension method 'GetName' accepting a first argument of type 'System.Array' could 
be found (are you missing a using directive or an assembly reference?)  
C:\Documents and Settings\Administrator\My Documents\Visual Studio 
2008\Projects\photoshops\photoshops\photosetting.aspx.cs    52  29  photoshops

Upvotes: 0

Views: 698

Answers (3)

Rizwan Ali Khan
Rizwan Ali Khan

Reputation: 21

You just need to do code 2 line through that you can be able store image from filepath and can be able to store into database.

string flPath = "C:\\1\\noimg.png";
byte[] imageBytes =  File.ReadAllBytes(flPath);

imageBytes is your part of interest holding whole arrays of bytes of your image, you just need to insert this to column of table of image datatype.

Upvotes: 2

r12
r12

Reputation: 59

string filename = FileUpload1.FileName.ToString();
         if (filename != "")
            {

                ImageName = FileUpload1.FileName.ToString();

                ImagePath = Server.MapPath("Images");
                SaveLocation = ImagePath + "\\" + ImageName;
                SaveLocation1 = "~/Image/" + ImageName;
                sl1 = "Images/" + ImageName;
                FileUpload1.PostedFile.SaveAs(SaveLocation);

            }...........try it for image

Upvotes: 0

p.campbell
p.campbell

Reputation: 100567

It sounds like you want the image location in the database.

Your code actually has the byte[] being used in the INSERT statement:

 cmd.Parameters.Add("@Systemurl", SqlDbType.Image).Value = pic;

Suggest pick one:

  • Continue with saving the binary to the database as you have above.
  • Save the byte[] as a file to a location on disk. Use that filename like so:
cmd.Parameters.Add("@ImagePath", SqlDbType.NVarChar).Value = myImagePathOnDisk;

Upvotes: 2

Related Questions