65 pts.
 SQL upload images problem
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

Software/Hardware used:
ASKED: July 31, 2009  9:08 PM
UPDATED: August 3, 2009  2:49 PM

Answer Wiki:
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>
Last Wiki Answer Submitted:  July 31, 2009  11:42 pm  by  carlosdl   63,535 pts.
All Answer Wiki Contributors:  carlosdl   63,535 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

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 pts.

 

For the SqlDbType and CommandType enums, you need to include the System.Data namespace

 63,535 pts.

 

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);
 63,535 pts.

 

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 pts.

 

carlos,

i an not getting any errors just is not working

 65 pts.

 

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();
 63,535 pts.

 

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 pts.