• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Inser image ERROR

Hello,

I'm working on Form for Hotels and I'm trying to insert an image in my database but I'm always have a error message.

Could please have look on the code and help me to find the problems.

Best regards



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;


namespace BookingWebApplication
{
   
    public partial class Addyourhotel : System.Web.UI.Page
    {

        string AddType;
        string AddRates;
        string AddHotelName;
        string AddCity;
        string AddStreet;
        string AddZipCode;
        string AddCountry;
        string AddBuildingNo;

        string AddContactName;
        string AddPhone;
        string AddFax;
        string AddWebsite;
        string AddEmail;

        string AddHotelDescription;
        string AddCheckIn;
        string AddCheckOut;

        string AddDistanceCenter;
        string AddDistanceTrain;
        string AddDistanceBeach;

        string AddFacilities;
        string AddService;

        string AddRoomName;
        string AddRoomName2;
        string AddRoomName3;
        string AddRoomDescription;
        string AddRoomDescription2;
        string AddRoomDescription3;

        int AddPersonPerRoom;
        int AddPersonPrice;

        string AddTerms;
        string AddPolicy;


        protected void Page_Load(object sender, EventArgs e)
        {

        }
         protected void Addyourhotel_Click(object sender, EventArgs e)
        {
            AddType = id_type.SelectedItem.Value;
            AddRates = id_rate.SelectedItem.Value;
            AddHotelName = id_HotelName.Text;
            AddCity = id_city.Text;
            AddStreet = id_street.Text;
            AddZipCode = id_zipCode.Text;
            AddCountry = id_country_select.SelectedItem.Value;
            AddBuildingNo = id_buildingNo.Text;

            AddContactName = id_name.Text;
            AddPhone = id_phone.Text;
            AddFax = id_fax.Text;
            AddWebsite = id_web.Text;
            AddEmail = id_email.Text;
             
            AddHotelDescription = id_hotelDescription.Text;
            AddCheckIn = id_checkIn.Text;
            AddCheckOut = id_checkOut.Text;

            AddDistanceCenter = id_distance_center.Text;
            AddDistanceTrain = id_distance_train.Text;
            AddDistanceBeach = id_distance_beach.Text;

            AddFacilities = id_facilities.Text;
            AddService = id_service.Text;

            AddRoomName = id_room_name.Text;

            AddRoomName2 = id_room_name2.Text;

            AddRoomName3 = id_room_name3.Text;


            AddRoomDescription = id_room_description.Text;

            AddRoomDescription2 = id_room_description2.Text;

            AddRoomDescription3 = id_room_description3.Text;


            ///AddPersonPerRoom = Convert.ToInt32(id_room_person.Text);
            ///AddPersonPrice = Convert.ToInt32(id_room_price.Text);

            AddTerms = id_terms.Text;
            AddPolicy = id_policy.Text;
             

         










            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString =
            @"Data Source=localhost\SQLEXPRESS;
                   AttachDbFilename=C:\Users\Anas\Documents\Visual Studio 2010\Projects\BookingWebApplication\BookingWebApplication\App_Data\Booking.mdf;
                   Integrated Security=True;
                   User Instance=True";

            cnn.Open();



            Label1.Text = "<b>Server Version:</b> " + cnn.ServerVersion;

            SqlCommand query = cnn.CreateCommand();

            query.CommandText = "INSERT INTO [Hotel] ([Type],[Rates],[Name],[City],[Street],[ZipCode],[Country],[BulidingNumber],[ContactFullName],[PhoneNumber],[FaxNumber],[Website],[Email],[Hotel_description],[Check_in],[Check_out],[Distance_center],[Distance_train],[Distance_beach],[FacilitiesDescription],[ServiceDescription],[Terms_condition],[Policy]) VALUES ('" + AddType + "','" + AddRates + "','" + AddHotelName + "','" + AddCity + "','" + AddStreet + "','" + AddZipCode + "','" + AddCountry + "','" + AddBuildingNo + "','" + AddContactName + "','" + AddPhone + "','" + AddFax + "','" + AddWebsite + "','" + AddEmail + "','" + AddHotelDescription + "','" + AddCheckIn + "','" + AddCheckOut + "','" + AddDistanceCenter + "','" + AddDistanceTrain + "','" + AddDistanceBeach + "','" + AddFacilities + "','" + AddService + "','" + AddTerms + "','" + AddPolicy + "'); SELECT SCOPE_IDENTITY();";

            decimal hotelid = (decimal)query.ExecuteScalar();


            query.CommandText = "INSERT INTO [Room_type] ([Hotels_id],[Name],[Description]) VALUES ('" + hotelid.ToString() + "','" + AddRoomName + "','" + AddRoomDescription + "');";

            query.CommandText += "INSERT INTO [Room_type] ([Hotels_id],[Name],[Description]) VALUES ('" + hotelid.ToString() + "','" + AddRoomName2 + "','" + AddRoomDescription2 + "')";

            query.CommandText += "INSERT INTO [Room_type] ([Hotels_id],[Name],[Description]) VALUES ('" + hotelid.ToString() + "','" + AddRoomName3 + "','" + AddRoomDescription3 + "')";


           
               
                if (query.Parameters.Count == 0)
                {
                    query.CommandText = "INSERT INTO [Images] ([Hotels_id],[Picture]) VALUES ('" + hotelid.ToString() + "','" + Upload + "',)";
                   
                    query.Parameters.Add("@Picture",
                                     System.Data.SqlDbType.Image);
                }

                query.Parameters["@Picture"].Value = Upload;

                //int iresult = this.query.ExecuteNonQuery();
                //MessageBox.Show(Convert.ToString(iresult));
         

           



              if (Page.IsValid) //save the image
            {
                Stream imgStream = UploadFile.PostedFile.InputStream;
                int imgLen = UploadFile.PostedFile.ContentLength;
                string img_contenttype = UploadFile.PostedFile.ContentType;
                string img_name = txtImgName.Value;
                byte[] img_data = new byte[imgLen];
                int n = imgStream.Read(img_data, 0, imgLen);
 
                String idis = Request.QueryString["id"];
                int business_id = System.Convert.ToInt32(idis);
 
                int RowsAffected = SaveToDB(img_name, img_data, img_contenttype, business_id);
                if (RowsAffected > 0)
                              {
                    TextBox TextBox2 = new TextBox();
                    TextBox2.Text = "The Image was Saved";
                    /*Response.Write("<BR>The Image was saved");*/
                               }
                else
                              {
                    Response.Write("<BR>An error occurred uploading the image");
                              }
            }

        }
       
       
 
 
        private int SaveToDB(string img_name, byte[] img_data, string img_contenttype, int business_id)
        {
            //use the web.config to store the connection string


            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString =
            @"Data Source=localhost\SQLEXPRESS;
                   AttachDbFilename=C:\Users\Anas\Documents\Visual Studio 2010\Projects\BookingWebApplication\BookingWebApplication\App_Data\Booking.mdf;
                   Integrated Security=True;
                   User Instance=True";

            cnn.Open();
           

           

            SqlCommand query = cnn.CreateCommand();

            query.CommandText = "INSERT INTO Image (img_name, img_data, img_contenttype, business_id) VALUES ( @img_name, @img_data,@img_contenttype,@business_id)";
                       
                       
            SqlParameter param0 = new SqlParameter("@img_name", System.Data.SqlDbType.VarChar, 50);
            param0.Value = img_name;
            query.Parameters.Add(param0);
 
            SqlParameter param1 = new SqlParameter("@img_data", System.Data.SqlDbType.Image);
            param1.Value = img_data;
            query.Parameters.Add(param1);
 
            SqlParameter param2 = new SqlParameter("@img_contenttype", System.Data.SqlDbType.VarChar, 50);
            param2.Value = img_contenttype;
            query.Parameters.Add(param2);
 
            SqlParameter param3 = new SqlParameter("@business_id", System.Data.SqlDbType.Int, 4);
            param3.Value = business_id;
            query.Parameters.Add(param3);
 
           
            int numRowsAffected = query.ExecuteNonQuery();
            cnn.Close();
 
            return numRowsAffected;
        }





        }
   
}
0
Alomiry
Asked:
Alomiry
  • 9
  • 6
1 Solution
 
dj_alikCommented:
What is the message?
0
 
AlomiryAuthor Commented:
Invalid object name 'Image'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'Image'.
0
 
dj_alikCommented:
SqlParameter param1 = new SqlParameter("@img_data", System.Data.SqlDbType.Image);
       

replace to

SqlParameter param1 = new SqlParameter("@img_data", System.Data.SqlDbType.Image,img_data.Length);
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AlomiryAuthor Commented:
I'm still new in ASP.NET environment.

It's a professional way to open two connection to database, and it s a right way to do the code as i wrote it .
0
 
dj_alikCommented:
You are beginner and your code have many problems and need to correction

About message: table name 'Image' does not exists.
INSERT INTO Image
correct the table name
0
 
AlomiryAuthor Commented:
can u guide me where s the Wrong and what's the best way to do it?
If u don't mind

Regards
0
 
dj_alikCommented:
you can use Enterprise library for your DATA ACCSESS LAYER
Start with:
DAAB in Enterprise Library for .NET 2.0
http://blogs.msdn.com/b/tomholl/archive/2005/09/14/466298.aspx



           
0
 
dj_alikCommented:
Some overview:
Microsoft Application Block for Data Access in .NET
http://www.codeproject.com/KB/database/MS_Application_Blocks.aspx
0
 
dj_alikCommented:
Main Site:
patterns & practices – Enterprise Library
http://entlib.codeplex.com/
0
 
dj_alikCommented:
Use Tiers:
3-Tier Architecture in ASP.NET with C#
http://www.dotnetfunda.com/articles/article71.aspx
0
 
dj_alikCommented:
For begining is enough :-)
0
 
AlomiryAuthor Commented:
Thanks for your help,
But i just have question?

Can i use SqlConnection two times at same function or same page.
 as i have used in my codebehind

Regards
0
 
dj_alikCommented:
yes yes  you can use singletone Connection
0
 
AlomiryAuthor Commented:
thanks.......... it's a perfect tutorials
0
 
AlomiryAuthor Commented:
Perfect
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now