kvnmck18 Posted June 28, 2007 Share Posted June 28, 2007 I have a textbox and a datagrid that need to be inserted into two different tables.Textbox needs to go into table1 and the data grid needs to go into table2. string sql = "insert into table1 (column1) values (blahblah.Text)"; The string sql is passed throughout the whole .cs, so is there a way to amend this string to include a second insert table? Link to comment Share on other sites More sharing options...
aspnetguy Posted June 28, 2007 Share Posted June 28, 2007 you can insert multiple rows into the same table in 1 statement but not the same row into 2 different tables at once. You need to execute 2 different statements Link to comment Share on other sites More sharing options...
kvnmck18 Posted June 28, 2007 Author Share Posted June 28, 2007 err...that's what I hoped not to hear. That makes this all so much more work to do. Link to comment Share on other sites More sharing options...
kvnmck18 Posted June 28, 2007 Author Share Posted June 28, 2007 (the second string is called 'sql2') comm.CommandText = sql, sql2; How can I make something like this not be full of errors.What is the best way of doing 2 seperate tables at the same time? Link to comment Share on other sites More sharing options...
jesh Posted June 28, 2007 Share Posted June 28, 2007 Why not try something like this: string sql = "insert into table1 (column1) values (value1); insert into table2 (column1) values (value1);"; Link to comment Share on other sites More sharing options...
kvnmck18 Posted June 28, 2007 Author Share Posted June 28, 2007 doesn't work...I tried that already... Link to comment Share on other sites More sharing options...
jesh Posted June 28, 2007 Share Posted June 28, 2007 doesn't work...I tried that already...Well, then how about using stored procedures as I do.C#SqlCommand command = new SqlCommand("MyAwesomeStoredProc", MySqlConnection);command.CommandType = CommandType.StoredProcedure;command.Parameters.AddWithValue("@Value1", value1);command.Parameters.AddWithValue("@Value2", value2);command.ExecuteNonQuery() Stored Procedure CREATE PROCEDURE MyAwesomeStoredProc(@Value1 int,@Value2 int)ASINSERT INTO Table1 (Column1) VALUES (@Value1)INSERT INTO Table2 (Column1) VALUES (@Value2)GO Link to comment Share on other sites More sharing options...
aspnetguy Posted June 29, 2007 Share Posted June 29, 2007 you could do this; (I'm using Sql server objects for the xample but oyu can change them how ever you like) SqlConnection conn = new SqlConnection(....);SqlCommand cmd;string sql1 = "statement 1";string sql2 = "statement 2";conn.Open();cmd = new SqlCommand(sql1,conn);cmd.ExecuteNonQuery();cmd = new SqlCommand(sql2,conn);cmd.ExecuteNonQuery();conn.Close(); It should be that simple Link to comment Share on other sites More sharing options...
kvnmck18 Posted June 29, 2007 Author Share Posted June 29, 2007 I wish that it was that simple, the first original string is processed so much...so adding it in just one process doesn't work. Look at the code again. The "if (!(recordexist(sql)))" part. Link to comment Share on other sites More sharing options...
aspnetguy Posted June 29, 2007 Share Posted June 29, 2007 I know you don't want/can't post your code but I don't have the time to sort through it and figure out what all it is doing. It must be 10-20 pages, sorry Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.