Click here to Skip to main content
15,898,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:


VB
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=stamp;user=sa;password=gariahat")
       Dim da As New SqlDataAdapter
       Dim cmd As New SqlCommand
       Dim ds As New DataSet
       'Dim dr As SqlDataReader
       cmd.Connection = cn
       cn.Open()

       cmd.CommandText = "insert into item(broker_id,name,bname,place,date,year,lot_no,lname,description,curr,amt) values('" & ComboBox1.Text & "', '" & TextBox1.Text & "','" & RichTextBox3.Text & "','" & TextBox2.Text & "','" & Format(CDate(TextBox3.Text), "MM/dd/yyyy") & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & RichTextBox1.Text & "','" & RichTextBox2.Text & "','" & ComboBox2.Text & "','" & TextBox8.Text & "')"
       cmd.ExecuteNonQuery()
       MsgBox("Data successfully added", MsgBoxStyle.Information)
       Me.ItemTableAdapter.Insert(ComboBox1.Text, TextBox1.Text, RichTextBox3.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, RichTextBox1.Text, RichTextBox2.Text, ComboBox2.Text, TextBox8.Text)
       Me.ItemTableAdapter.Fill(Me.StampDataSet.item)
       ComboBox1.Text = ""
       ComboBox2.Text = ""
       TextBox1.Text = ""
       TextBox2.Text = ""
       TextBox3.Text = ""
       TextBox4.Text = ""
       TextBox5.Text = ""
       RichTextBox3.Text = ""
       RichTextBox1.Text = ""
       RichTextBox2.Text = ""
       TextBox8.Text = ""

       cn.Close()



the error showing is:-

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

the error showing in line
Me.ItemTableAdapter.Fill(Me.StampDataSet.item)

Can anyone tell me why the error is coming? cant understand it.
Posted
Updated 18-Aug-13 19:24pm
v2
Comments
sudeshna from bangkok 19-Aug-13 1:25am    
my database is getting filled with data but then its showing the error in
me.itemtableadapter.fill

i have 2 tables agent and item/

in agent table broker_id is the primary key and in item table its the foreign key.
Sriram Bala 19-Aug-13 1:52am    
Hi Sudeshna,

The error message clearly shows the data is either not unique or null which is against the primary and Unique key rule. Please debug and check the ComboBox1.Text is having unique value or not. for testing purpose try with some hardcoded values and check wether it is getting inserted in to the database since you have mentioned only broker id is primary key. Make sure you are NOT inserting the same value for the primary key again and again. It is invalid.

Thanks.Sriram.B
sudeshna from bangkok 19-Aug-13 2:07am    
yes my data is getting inserted into database.

i have made comment on that line, so now its working. but just for clarification wanted to know why that statement is used for?

i have now made that fill statement commented,so no error showing

1 solution

My impression is that you insert your data twice:
- first with a cmd.ExecuteNonQuery() statement,
- next, with Me.ItemTableAdapter.Insert(...
and that may conflict with a unique key.
Which of the values inserted a primary key (e.g. broker_id), or which one of the values inserted has a unique index? That one causes the exception to be thrown when you insert the data the second time.
 
Share this answer
 
Comments
sudeshna from bangkok 19-Aug-13 5:13am    
only broker_id is the primary key in agent table and foreign key in item table,no other fields are unique.
sudeshna from bangkok 19-Aug-13 5:15am    
and now another problem showing that:-
in this curr column in item table, its a combo box where 3currency symbols are there, dollar,thai bhat and pound sign.

now in place of pound sign,? sign getting inserted in database.
whay is that so?
sudeshna from bangkok 19-Aug-13 6:07am    
can anyone please help me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900