how to check primary key?

mkhurram92

Member
Joined
May 27, 2011
Messages
16
Location
Jeddah(KSA)
Programming Experience
1-3
hello

i defined a Column in Table as Primary Key. Now i am inserting values using Textbox.
When entering Duplicate values it is giving me error of primary Key... How i can check record exits or not to enter unique data ????

Please Help ???
 
VB.NET:
SELECT COUNT(*) FROM SomeTable WHERE ID = @ID
Create an appropriate ADO.NET command object with that SQL and call ExecuteScalar. It will return zero if the ID doesn't exist.
 
VB.NET:
SELECT COUNT(*) FROM SomeTable WHERE ID = @ID
Create an appropriate ADO.NET command object with that SQL and call ExecuteScalar. It will return zero if the ID doesn't exist.

i am sorry i m very new in VB.NET Dont have any idea about ADO.NET.
i have table name itemfile with the field name item_code !
so can u give me a hint how i can do it pls ???
 
Solution

hey thanks all for ur help and reply

i just solved my problem with adding an exception with INSERT Command. My code is under

Try
Me.Validate()
Me.ItemfileBindingSource.EndEdit()
Me.ItemfileTableAdapter.Insert(item_code.Text, item_desc.Text,
item_type.Text, item_cost.Text, unit_code.Text)
Me.TableAdapterManager.UpdateAll(Me.ICSysDataSet)
MsgBox("Added Successfully")
item_code.Text = ""
item_desc.Text = ""
item_type.Text = ""
item_cost.Text = ""
unit_code.Text = ""
Catch ex As SqlException
MsgBox("Item Code Already Exists", MsgBoxStyle.Critical, "Error")
item_code.Text = ""
item_desc.Text = ""
item_type.Text = ""
item_cost.Text = ""
unit_code.Text = ""
End Try
 
Back
Top