CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2009
    Posts
    2

    upload images asp.net

    hello glad to be here,

    i have a upload images tutoiral that i got from here

    http://geekswithblogs.net/dotNETvinz...n-asp.net.aspx

    but i can not get it to work, it is slightly tweaked for me.



    errors i am getting are:

    ------ Build started: Project: C:\WebSite4\, Configuration: Debug Any CPU ------
    Validating Web Site
    Building directory '/WebSite4/'.

    C:\WebSite4\Default.aspx.cs(35,9): error CS0246: The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?)
    C:\WebSite4\Default.aspx.cs(35,34): error CS0246: The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?)
    C:\WebSite4\Default.aspx.cs(42,13): error CS0246: The type or namespace name 'SqlCommand' could not be found (are you missing a using directive or an assembly reference?)
    C:\WebSite4\Default.aspx.cs(42,34): error CS0246: The type or namespace name 'SqlCommand' could not be found (are you missing a using directive or an assembly reference?)
    C:\WebSite4\Default.aspx.cs(43,13): error CS0246: The type or namespace name 'SqlParameter' could not be found (are you missing a using directive or an assembly reference?)
    C:\WebSite4\Default.aspx.cs(43,40): error CS0246: The type or namespace name 'SqlParameter' could not be found (are you missing a using directive or an assembly reference?)
    C:\WebSite4\Default.aspx.cs(46,28): error CS0246: The type or namespace name 'SqlParameter' could not be found (are you missing a using directive or an assembly reference?)
    C:\WebSite4\Default.aspx.cs(46,49): error CS0103: The name 'SqlDbType' does not exist in the current context
    C:\WebSite4\Default.aspx.cs(47,28): error CS0246: The type or namespace name 'SqlParameter' could not be found (are you missing a using directive or an assembly reference?)
    C:\WebSite4\Default.aspx.cs(47,50): error CS0103: The name 'SqlDbType' does not exist in the current context
    C:\WebSite4\Default.aspx.cs(48,28): error CS0246: The type or namespace name 'SqlParameter' could not be found (are you missing a using directive or an assembly reference?)
    C:\WebSite4\Default.aspx.cs(48,53): error CS0103: The name 'SqlDbType' does not exist in the current context
    C:\WebSite4\Default.aspx.cs(49,28): error CS0246: The type or namespace name 'SqlParameter' could not be found (are you missing a using directive or an assembly reference?)
    C:\WebSite4\Default.aspx.cs(49,53): error CS0103: The name 'SqlDbType' does not exist in the current context
    C:\WebSite4\Default.aspx.cs(61,31): error CS0103: The name 'CommandType' does not exist in the current context
    Validation Complete
    ========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

    PHP Code:
    <b>Default.aspx</b>

    <
    code>



    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head runat="server">
        <
    title></title>
    </
    head>
    <
    body>
        <
    form id="form1" runat="server">
        <
    div>
        
            
    Browse Image:<br />
            <
    br />
            <
    asp:FileUpload ID="FileUpload1" runat="server" />
            <
    br />
            <
    br />
            <
    asp:Button ID="Button1" runat="server" Text="Save" Width="79px" />
        
        </
    div>
        </
    form>
    </
    body>
    </
    html

    <b>Default.aspx.cs</b>

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page 
    {
        private void StartUpLoad()
        {
            //get the image file that was posted (binary format)
            byte[] theImage = new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile Image = FileUpload1.PostedFile;
            Image.InputStream.Read(theImage, 0, (int)FileUpload1.PostedFile.ContentLength);
            int length = theImage.Length; //get the length of the image
            string fileName = FileUpload1.FileName.ToString(); //get the file name of the posted image
            string type = FileUpload1.PostedFile.ContentType; //get the type of the posted image
            int size = FileUpload1.PostedFile.ContentLength; //get the size in bytes that
            if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
            {
                //Call the method to execute Insertion of data to the Database
                ExecuteInsert(theImage, type, size, fileName, length);
                Response.Write("Save Successfully!");
            }
        }
        public string GetConnectionString()
        {
            //sets the connection string from your web config file "ConnString" is the name of your Connection String
            return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
        }
    
        private void ExecuteInsert(byte[] Image, string Type, Int64 Size, string Name, int length)
        {
            SqlConnection conn = new SqlConnection(GetConnectionString());
    
            string sql = "INSERT INTO TblImages (Image, ImageType, ImageSize, ImageName) VALUES "
                        + " (@img,@type,@imgsize,@imgname)";
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter[] param = new SqlParameter[4];
    
                //param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
                param[0] = new SqlParameter("@img", SqlDbType.Image, length);
                param[1] = new SqlParameter("@type", SqlDbType.NVarChar, 50);
                param[2] = new SqlParameter("@imgsize", SqlDbType.BigInt, 9999);
                param[3] = new SqlParameter("@imgname", SqlDbType.NVarChar, 50);
    
                param[0].Value = Image;
                param[1].Value = Type;
                param[2].Value = Size;
                param[3].Value = Name;
    
                for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }
    
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Insert Error:";
                msg += ex.Message;
                throw new Exception(msg);
    
            }
            finally
            {
                conn.Close();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            StartUpLoad();
        }
       
    
    }
    <b>web.config</b>
    Code:
     
    
    <connectionStrings>
            <add name="MyConsString" connectionString="ZONE777\SQLS;Initial" Catalog="newDB;Integrated" Security="True;"
           providerName="System.Data.SqlClient" />
        </connectionStrings>
    i have tried a whoel bunch of these tutorials and i have not got only one to work but then
    the display did not work.


    if any one know of a tutorial that show how to store just the path of the image and then
    store and display them with asp.net, I would greatly appreciate it.
    thanks,

    craig
    Last edited by HanneSThEGreaT; August 3rd, 2009 at 01:10 AM. Reason: Added Code Tags!

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: upload images asp.net

    Download the samples from my signature block.

    MyConsString
    is the name you said to find in your Web.Config section, but you need to modify that to access YOUR server


    Also, go back and correct your post. You need to use code tags [] [/] around your code,
    Code:
    ' Like this!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: upload images asp.net

    [ Moved ]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured