1,105,391 Community Members

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

Member Avatar
shahai.ali
Light Poster
48 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
AleMonteiro
Master Poster
758 posts since Aug 2010
Reputation Points: 115 [?]
Q&As Helped to Solve: 143 [?]
Skill Endorsements: 29 [?]
 
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')
Member Avatar
shahai.ali
Light Poster
48 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
AleMonteiro
Master Poster
758 posts since Aug 2010
Reputation Points: 115 [?]
Q&As Helped to Solve: 143 [?]
Skill Endorsements: 29 [?]
 
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.

Member Avatar
JorgeM
IT Addict
6,418 posts since Dec 2011
Reputation Points: 581 [?]
Q&As Helped to Solve: 963 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
0
 

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

Member Avatar
shahai.ali
Light Poster
48 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thnx

Question Answered as of 1 Year Ago by AleMonteiro and JorgeM
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: