Answered by:
SqlDataSource InsertCommand with C#?

Question
-
User260076833 posted
Hi,
I need an InsertCommand for a SqlDataSource with a distinction of cases:
if (field1 = "A") INSERT INTO table1 (...) VALUES (...); else INSERT INTO table2 (...) VALUES (...);
I cannot do this by setting the InsertCommand property to an INSERT statement.
What I found is that I could use a Stored Procedure.
What else can I do?
Could I attache some C# code to the SqlDataSource that does the insert?
Thanks
MagnusFriday, June 26, 2015 2:20 AM
Answers
-
User2024324573 posted
You can have a look to the example at: http://www.codeproject.com/Tips/331181/How-to-insert-Data-in-SQL-server-via-SqlDataSource
For the details about SqLDataSource this one you can follow: http://www.asp.net/web-forms/overview/data-access/accessing-the-database-directly-from-an-aspnet-page/inserting-updating-and-deleting-data-with-the-sqldatasource-cs
Also this one you can have a look: http://www.dreamincode.net/forums/topic/187000-parameter-for-sqldatasource-inserting-using-c%23/
Thanks,
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 26, 2015 2:41 AM -
User61956409 posted
Hi Magnus,
What I found is that I could use a Stored Procedure.
What else can I do?
Could I attache some C# code to the SqlDataSource that does the insert?
You dynamically set InsertCommand property according to the value of field1, your code may look like this.
if (field1 == "A") { SqlDataSource1.InsertCommand = "INSERT INTO table1 (...) VALUES (...)"; } else { SqlDataSource1.InsertCommand = "INSERT INTO table2 (...) VALUES (...)"; } //call Insert() Method to perform an insert operation SqlDataSource1.Insert();
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 9, 2015 5:14 AM
All replies
-
User2024324573 posted
You can have a look to the example at: http://www.codeproject.com/Tips/331181/How-to-insert-Data-in-SQL-server-via-SqlDataSource
For the details about SqLDataSource this one you can follow: http://www.asp.net/web-forms/overview/data-access/accessing-the-database-directly-from-an-aspnet-page/inserting-updating-and-deleting-data-with-the-sqldatasource-cs
Also this one you can have a look: http://www.dreamincode.net/forums/topic/187000-parameter-for-sqldatasource-inserting-using-c%23/
Thanks,
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 26, 2015 2:41 AM -
User61956409 posted
Hi Magnus,
What I found is that I could use a Stored Procedure.
What else can I do?
Could I attache some C# code to the SqlDataSource that does the insert?
You dynamically set InsertCommand property according to the value of field1, your code may look like this.
if (field1 == "A") { SqlDataSource1.InsertCommand = "INSERT INTO table1 (...) VALUES (...)"; } else { SqlDataSource1.InsertCommand = "INSERT INTO table2 (...) VALUES (...)"; } //call Insert() Method to perform an insert operation SqlDataSource1.Insert();
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 9, 2015 5:14 AM