Contraint Violation When Reading From Database
Apr 4, 2008
I have created a typed-dataset AuthorsDataSet and created a table in with name Authors, manually by right-click > New > Table. I have kept the data-types, sizes and contstriants exactly same as the table in the database, though I have kept the Column names different. I am filling the AuthorsDataSet with the following code on Form_Loadif (!IsPostBack)
{ SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT au_id, au_lname, au_fname, phone, address, city, state, zip, contract FROM authors", conn); using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
conn.Open();
da.Fill(ds.Authors);
grdAuthors.DataSource = ds.Authors;
grdAuthors.DataBind();
conn.Close();
}
conn.Dispose();
cmd.Dispose();
}
The connection string is as follows:<connectionStrings>
<add name="pubsConnectionString" connectionString="Data Source = localhost; Initial Catalog = pubs; Integrated Security = SSPI"/>
</connectionStrings>
The following error occurs when I run the page
System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
View 3 Replies
Jun 24, 2007
Hi,
Background:
This could be a simple C++ issue as I am not that familiar with the language. I am trying to prototype a small program to test the performance of inserting data using the BCP interface. I want to BCP data that my program generates and passes via program variables.
I have narrowed the issue down to a sprintf call. Following are two variations of the code - one that works and one that does not. The error happens when I try to execute the bcp_sendrow() call.
What is the appropriate way to create / send string data to the BCP interface. Any good examples available? I have created the following from the BulkCopyFromVariables example on the Microsoft SQL Server downloads site.
Thanks in advance, Kerry
---- Works ----
Code Snippet
for(idx=1; idx<=5; idx++){
//sprintf(k1, "%d", idx); //does not work...
sprintf(k1, "oo"); //works....
if ( (SendRet = bcp_sendrow(hdbc1) ) != SUCCEED ) {
printf("bcp_sendrow(hdbc1) Failed");
Cleanup();
return(9);
}
}
--- Causes Access Violation ----
Code Snippet
for(idx=1; idx<=5; idx++){
sprintf(k1, "%d", idx); //does not work...
//sprintf(k1, "oo"); //works....
if ( (SendRet = bcp_sendrow(hdbc1) ) != SUCCEED ) {
printf("bcp_sendrow(hdbc1) Failed");
Cleanup();
return(9);
}
}
--- Additional information ---
---------------------------
Target Table Definition
---------------------------
Code SnippetCREATE TABLE PtTest(
[ID] [int] IDENTITY(1,1) NOT NULL,
[PtID] [int] NOT NULL,
[ParamID] [nchar](10) NULL DEFAULT ((200)), --Above example trying to insert data here via k1 variable...
[ParamValue] [nchar](10) NULL DEFAULT ((200)),
[InsDate] [datetime] NULL DEFAULT (getdate()),
)
----------------------------
-----------------
Bind statements
-----------------
Code Snippet
int idx;
char k1[5];
char *terminator = "