We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,397 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how to select the name of one column from one table and insert into other.

hi m getting the following exception:
System.Data.SqlClient.SqlException: Incorrect syntax near '4'.
the following is what i was trying:

public void hostel_cpacity(ManageHostelRoomsBE hmr)
        {
            string str;
            string conString =                           System.Configuration.ConfigurationManager.ConnectionStrings["Hosteluog"].ConnectionString;
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter();
            str = "select HostleName from tbl_Hostel where (HostelID ='" + hmr.HostelID + "')";
            cmd.CommandText = "insert into hostel_info(hostelName,rooms,capacity) values ('" + str + "','" + hmr.RoomNO + "','" + hmr.CapacityID + "')";
            cmd.ExecuteNonQuery();


            con.Close();

        }

plz help me in this regard.

3
Contributors
5
Replies
21 Hours
Discussion Span
5 Months Ago
Last Updated
6
Views
Question
Answered
shahai.ali
Light Poster
48 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I'd do it like this:

"insert into hostel_info(hostelName,rooms,capacity) select HostleName, '" + hmr.RoomNO + "','" + hmr.CapacityID + "'  from tbl_Hostel where (HostelID ='" + hmr.HostelID + "')";

Clean example:

INSERT INTO hostel_info(hostelName,rooms,capacity) 
SELECT HostleName, 'rooms', 'capacity'  
FROM tbl_Hostel 
WHERE (HostelID = 'id')
AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 23

but i think when we have to insert into the table we can work only with a single table. there is a problem while using a sub query for making insertion into table...

shahai.ali
Light Poster
48 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

No, there is not a problem. There is only methods to do it. You can make a insert from a select, like I posted.

AleMonteiro
Master Poster
752 posts since Aug 2010
Reputation Points: 129
Solved Threads: 140
Skill Endorsements: 23

In AleMonteiro's example, he is only showing you how to insert into one table, but from the results of a query from another.

JorgeM
Industrious Poster
4,024 posts since Dec 2011
Reputation Points: 297
Solved Threads: 549
Skill Endorsements: 115

thnx

shahai.ali
Light Poster
48 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 5 Months Ago by AleMonteiro and JorgeM

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0789 seconds using 2.79MB