?
Solved

i got double value in database

Posted on 2011-05-11
5
Medium Priority
?
345 Views
Last Modified: 2012-05-11
Hello,

when i insert the value from my form and check the database i found double value Why?

My code Behind:

 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]) VALUES ('" + hotelid.ToString() + "','" + AddRoomName + "');";

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

            query.ExecuteNonQuery();
             
             SqlDataReader reader = query.ExecuteReader();



            reader.Close();
0
Comment
Question by:Alomiry
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 35736576
How is your database designed ? What type does your primary key (hotelid) have in your table Hotel ?
0
 

Author Comment

by:Alomiry
ID: 35736587
yes i have Hotel ID

it's working now when i remove :
      query.ExecuteNonQuery();

but it's ok to remove it and when we have to use ExecuteNonQuery();


Best regards
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 2000 total points
ID: 35736597
I don't see why your are using

SqlDataReader reader = query.ExecuteReader();
reader.Close();

Use only the executeNonQuery()
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]) VALUES ('" + hotelid.ToString() + "','" + AddRoomName + "');";

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

            query.ExecuteNonQuery();

Open in new window

0
 

Author Comment

by:Alomiry
ID: 35736616
Perfect .

Best Regards
0
 

Author Closing Comment

by:Alomiry
ID: 35736621
Perfect Soultion
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question