SQL upload images problem

65 pts.
Tags:
Image Editor
SQL
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:WebSite4Default.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:WebSite4Default.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:WebSite4Default.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:WebSite4Default.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:WebSite4Default.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:WebSite4Default.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:WebSite4Default.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:WebSite4Default.aspx.cs(46,49): error CS0103: The name 'SqlDbType' does not exist in the current context C:WebSite4Default.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:WebSite4Default.aspx.cs(47,50): error CS0103: The name 'SqlDbType' does not exist in the current context C:WebSite4Default.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:WebSite4Default.aspx.cs(48,53): error CS0103: The name 'SqlDbType' does not exist in the current context C:WebSite4Default.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:WebSite4Default.aspx.cs(49,53): error CS0103: The name 'SqlDbType' does not exist in the current context C:WebSite4Default.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 ========== <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> </code> <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(); } } </code> <b>web.config</b> <code> <connectionStrings> <add name="MyConsString" connectionString="ZONE777SQLS;Initial" Catalog="newDB;Integrated" Security="True;" providerName="System.Data.SqlClient" /> </connectionStrings> </code> 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
ASKED: July 31, 2009  9:08 PM
UPDATED: August 3, 2009  2:49 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

The first thing you need to do is add the System.Data and System.Data.SqlClient namespaces.

<pre>using System.Data
using System.Data.SqlClient</pre>

Discuss This Question: 7  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Zpupster
    thank you so much. i am learning, i imagine that needs to be called for these to work: SqlCommand cmd = new SqlCommand(sql, conn); SqlParameter[] param = new SqlParameter[4]; I am not sure what he or she is doing here: catch (System.Data.SqlClient.SqlException ex) well these are the errors i am getting now. ------ Build started: Project: C:WebSite4, Configuration: Debug Any CPU ------ Validating Web Site Building directory '/WebSite4/'. C:WebSite4Default.aspx.cs(47,49): error CS0103: The name 'SqlDbType' does not exist in the current context C:WebSite4Default.aspx.cs(48,50): error CS0103: The name 'SqlDbType' does not exist in the current context C:WebSite4Default.aspx.cs(49,53): error CS0103: The name 'SqlDbType' does not exist in the current context C:WebSite4Default.aspx.cs(50,53): error CS0103: The name 'SqlDbType' does not exist in the current context C:WebSite4Default.aspx.cs(62,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 ========== thanks, craig
    65 pointsBadges:
    report
  • carlosdl
    For the SqlDbType and CommandType enums, you need to include the System.Data namespace
    65,110 pointsBadges:
    report
  • carlosdl
    Sorry, this line:
    catch (System.Data.SqlClient.SqlException ex)
    is part of a try-catch construct. It will catch any SQL error (exception) generated by the instructions inside the TRY block, and will execute these lines:
    string msg = "Insert Error:";
    msg += ex.Message;
    throw new Exception(msg);
    65,110 pointsBadges:
    report
  • Zpupster
    thanks carlos that worked by adding those 2. but this is not loading into the database. i checked by going to the table and asked it to show the table data. it also is not responding " Save Successfully! " i do not why it is not working, do know of any other examples that may work? thanks craig
    65 pointsBadges:
    report
  • Zpupster
    carlos, i an not getting any errors just is not working
    65 pointsBadges:
    report
  • carlosdl
    Maybe the Button1_Click procedure is not being called. Try this: Remove this part from your code:
    protected void Button1_Click(object sender, EventArgs e)
    {
    StartUpLoad();
    }
    Then go to the view designer and double-click on "Save" button, and put this inside the procedure it has just generated, and try again.:
    StartUpLoad();
    65,110 pointsBadges:
    report
  • Zpupster
    ty carlos, thanks for hanging in there with me. i got the images to go into the database. now i have to find a script to get to view them, but that will be tommorow. good night, thanks again craig
    65 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following