.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Post New Web Links

SQL insert command

Posted By:      Posted Date: May 22, 2011    Points: 0   Category :


When I try to do this I get an error: Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_menuitem_menu". The conflict occurred in database "wsdinnernow", table "dbo.menu", column 'MenuId'.
The statement has been terminated.

INSERT INTO wsdinnernow.dbo.menuitem(MenuItemId, MenuId, ItemName, ItemDescription, ItemImageLocn, ItemPrice, ItemPrepTime)

 SELECT X.menuitem.query('MenuItemId').value('.', 'INT'),
  X.menuitem.query('MenuId').value('.', 'INT'),
  X.menuitem.query('ItemName').value('.', 'NVARCHAR(100)'),
  X.menuitem.query('ItemDescription').value('.', 'NVARCHAR(100)'), 

View Complete Post

More Related Resource Links

The data source control failed to execute the insert command


Hi All,

I have create table in SQL 2005 and retrive its data in to sharepoint  with the help of Database connection option through Sharepoint Designer. When configuring the connection i have selected a custom quer with stored procedure option and from the dropdown option selected the insert stored procedure everything is ok.

Stored Procedure
CREATE PROCEDURE ChangeTable_insert

 @PROJECT varchar(50) ,

INSERT INTO [dbo].[ChangeTable]





i have created new aspx page in Sharepoint Designer from the datasource details-->Insert selected fields as-->selected new item form and saved this page.
When i opened this page in to browser and try to save data it gives an below error
Go back to site 
Error: "The data source control failed to execute the insert command.
 Troubleshoot issues with Windows SharePoint Services."

please help me. i have to save data to sql database through sharepoint

thanks in advance

Insert command not using selected value from DDL in a DetailsView


My web form contains a detailsview control that I have added a templated field to with a DropDownList in the InsertItemTemplate. The DetailsView default mode is 'insert. My detailsview is attached to a sqldatasource that has a stored procedure in the db to handle the insert. When I enter data in the fields and select insert all values from the detailsview are written to the table except for the selection that was made in the dropdownlist. I am using the ddl.selecteditem.value to populate my variable however when the insert method is envoked it doesn't send my value. The DDL value that I am capturing does not need to be written to the table it is just a flag for some variables that need to be set at the stored proc level. How do I get the DDL selected value to be sent with the insert statement? Any help would be greatly appreciated.

Here is my code behind:





Protected Sub

DropDownList1_SelectedIndexChanged(ByVal sender

Repeating section appears above when insert command clicked


I have a form containing a repeating section.  The section contains a number of control within a layout table.  The repeating section properties are set to allow users to insert a new section.  By default new rows/tables in repeating controls are inserted below the existing when the command line is clicked.

Is there a way to change this default, ie. when the command line is clicked the repeating section is added ABOVE the existing section? 

The form will be operationg as browser enabled within SharePoint (2005)

The data source control failed to execute the insert command


In SharePoint Designer 2007 I'm trying to customize the edit form that you get when you create a list item in a SharePoint 2007 list (EditForm.aspx). I took the following steps:

1.       Open the site in SharePoint Designer 2007.

2.       Browse to the list and open the 'NewForm.aspx' web form.


Code for insert command using C#, SQL, and ASP.NET


I'm having problems getting my code to work.  I have a couple text boxes.  When I hit the button I want it to insert the data from the two text boxes into the SQL database.  Could some show me some C# code that does this from something that actually works?   

"The data source control failed to execute the insert command." when inserting to sql table with tri



I have a data view web part that inserts a new record into a sql table. This works fine.

I have now needed to add a trigger to the sql table that sends an email when a new record is inserted (the code for which works fine) but now my insert fails with 'The data source control failed to execute the insert command.'.

If I disable the trigger the form works fine again.

Any ideas what could be causing this?


How to retrieve ID after Insert command when using DataSet (xsd) ?



I have created a Dataset object (XSD file)  whithin it there is a procedure called InsertWebsite.

In my code, I call this like this:

return Convert.ToInt32(Adapter.InsertWebsite(txtWebsite, txtWebsiteURL, txtWebsiteDescription, txtWebsiteURL2, txtWebsiteURL2Text, txtWebsiteURL3, txtWebsiteURL3Text, DateTime.Now, intLinkTypeID, bitIsLive, txtRecipURL, bitHasPaid, txtSubmitterName, txtSubmitterEmail, txtSubmitterIP, intCategoryID, guidUserID));

The query in the XSD dataset file is:

INSERT INTO [dbo].[tblWebsites] ([txtWebsite], [txtWebsiteURL], [txtWebsiteDescription], [txtWebsiteURL2], [txtWebsiteURL2Text], [txtWebsiteURL3], [txtWebsiteURL3Text], [dteSubmitted], [intLinkTypeID], [bitIsLive], [txtRecipURL], [bitHasPaid], [txtSubmitterName], [txtSubmitterEmail], [txtSubmitterIP], [intCategoryID],[guidUserID]) VALUES (@txtWebsite, @txtWebsiteURL, @txtWebsiteDescription, @txtWebsiteURL2, @txtWebsiteURL2Text, @txtWebsiteURL3, @txtWebsiteURL3Text, @dteSubmitted, @intLinkTypeID, @bitIsLive, @txtRecipURL, @bitHasPaid, @txtSubmitterName, @txtSubmitterEmail, @txtSubmitterIP, @intCategoryID,@guidUserID);SELECT SCOPE_IDENTITY()


When I execute the commans it retrievs value "1" which is not the actual ID genereted by Insert event.....


handled terminal cannot perform just one sql insert command


Hi there,

I have very weird a problem. 

There are 5-6 insert, select queries at my project. All queries and program works fine at device simulator.In fact all queries also works at the device (intermec - windows mobile 6.1 classic) but "only one of the insert query" doesnt work (very simple one : insert into members values(a,b,c,d,e) like this.) at the device.

Why this problem can occur??

Any idea?

Thanks everyone for helps.

ado command does not insert all rows


In my C++ code, when I try to run the ADO command to insert rows into a table, it only inserts a certain number of rows. The same command works when using System.Data.SqlClient in .NET. 

Sql profiler shows the same textdata. Below is my insert commandtext, I'm not sure what else to do to uncover the issue here. Any help on this is appreciated.

Command text : 

declare @i int set @i = 1 while (@i < 255) begin insert into table1 (name,type, order, state) values (@i, N'type',0,0) set @i = @i +1 end

The above command ends at 153 rows. Is this dependent on table size ?

This is my connection string and the code I'm using to build the connection :

ADODB::_ConnectionPtr m_Conn = NULL;  
TCHAR connString[255];
  _stprintf(connString, _T("DRIVER=SQL Server;SERVER=np:(local)\\MyInstance;DATABASE=test;"));

   HRESULT hr;
   hr = m_Conn.CreateInstance(__uuidof(ADODB::Connection));
   if (hr != S_OK) {
     wprintf(_T("CreateInstance(__uuidof(ADODB::Connection)) failed. Error: %d"), hr);
     return 0;

   m_Conn->ConnectionTimeout = 1800;
   m_Conn->CommandTimeout = 1800;

   hr = m_Conn->Open((LPCTSTR)connString, _T(""), _T(""), ADODB::adConnectUnspecified);
   if (hr != S_OK) {

Insert value using Table Value Functions

a real gem in Sql Server 2008. mostly people still using Stored procedure may be they shifted to SQL Server but they are not using TVF right now.

Insert Rows with a GridView

In ASP.NET version 1.x, I used the footer on the DataGrid control to insert new rows by placing a series of TextBox, DropDownLists, CheckBoxes etc. controls on it and then handling a save button. This also works with the GridView in version 2.0 but with one major exception: if your data source is empty, the GridView will hide your footer (and header) and only display the EmptyDataText.

SqlCommand.ExecuteNonQuery() returns -1 when doing Insert / Update / Delete

Sometimes you end up with a return value of -1 when using the SqlClient.SqlCommand.ExecuteNonQuery method.

Why is that?

Well, the ExecuteNonQuery method is there for statements for changing data, ie. DELETE / UPDATE /INSERT, and the returned value are the number of rows affected by that statement.

When checking the documentation we can see that there are some conditions that return -1.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of

rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

Row command working on all rows not selected one?



I have this code that hides a column in a gridview.  (The column houses another nested gridview).

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Select" Then
            If GridView1.Columns(0).Visible = False Then
                GridView1.Columns(0).Visible = True
                GridView1.Columns(0).Visible = False
            End If
        End If
    End Sub

The trouble is that if I click the select link on one row that action to hide is carried out on all rows.  How can I set this up so that it only hides the row that is selected?


Print command



Is there a way of setting up a print area for a part of my webpage, I only want to print a certain part of the page, when I select print at the moment it will print the whole page and the layout is rubbish!!

Insert Error not helpful



I am using VS2010

When I try to updated or insert a record in my User table I am getting an error that does not make sense to me and I cannot figure out what is wrong.

Also, when I try to insert a new record I get a validation error saying that the Id is required. However it is an identity field in the db so it should not be necessary to send it to the server.

Could you please shed some light?


An error has occurred.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Microsoft.Web.UI.WebControls.EntityOperationException: An error has occurred.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace:

[EntityOperationException: An error has occurred.]
   Microsoft.Web.UI.WebControls.DomainDataSourceView.PerformCudOperation(ChangeSetEntry operation, Action`1 operationPerformingEvent, Action`1 operationPerformedEvent) +331
   Microsoft.Web.UI.WebControls.DomainDataSourceView.UpdateObject(Object oldEntity, O

Detecting Edit or Insert Page


I am coding a .cs file that handles the metadata for a particular table. I am putting a custom validation that allows me to update or insert only if the PubMedID is not already in the database. This would be easy except that the PMID should also be allowed to be null as some of the references in the database were not from PubMed. So i perform a query on the database to see if there is already an identical PMID in the database. This works great when using Insert. However, when using Edit, it does not. The PMID is already in the database because the one currently being edited has the same PMID!!! So I know exactly how to change my query etc. However, I do not know how to tell from inside this Edit/Insert agnostic page, which one it is being called from. Thanks for your help! 

command object error


Could someone please tell me what is wrong with my syntax here?  I'd be much appreciative.


objCmd = New SQLCommand("SELECT CASE WHEN objDR('Item') = 'Enter Bill of Lading' then enterbol " _  

& "WHEN objDR('Item') = 'Bill of Lading History' THEN bolhistory " _  

& "WHEN objDR('Item') = 'Request Transfer' or objDR('Item') = 'View Transfer in Progress' THEN transfers " _  

& "END  from plantGroups where plantgroupid=@plantgroupid", objConn)

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend