In my program theres a form where the user enters the ID number and other details of a customer. i want the program to be able , If the ID number already exists when the user enters it and clicks on ok, to display an error message telling them that the ID already exists otherwise it adds the id and the details to a database. Im not sure how i would do that im thinking it would be an SQL statement? not sure. Thanks

Please show your code work.

huunm on the textboxid keydown event or leave event of the that
you can check the value given by the user like
if in db theres' is 1,5,6,9,8,6
while in any of the one event I'll write the code

dim str as string,sqlstr as string,ans as boolean
con.open() ''write your connection
str=textboxid.text
dim cmd as  OleddbCommand ''creating command
sqlstr ="SELECT id FROM myTable WHERE ID='" & str &'"
cmd=new OledbCommand(sqlstr,con) ''pass the value sqlstrto ''command
ans=cmd.ExequtenonQuery()
if ans= true then
''msgbox enter another no
elseif ans= flase then
''move further that is id is unique
end if

try this code

@ adatapost
i dont have any code for that bit
i need help with it.

@pritesth2010
that code gets underlined when i put it into the program.

huunm on the textboxid keydown event or leave event of the that
you can check the value given by the user like
if in db theres' is 1,5,6,9,8,6
while in any of the one event I'll write the code
Help with Code Tags
VB.NET Syntax (Toggle Plain Text)

1. dim str as string,sqlstr as string,ans as boolean
2. con.open() ''write your connection
3. str=textboxid.text
4. dim cmd as OleddbCommand ''creating command
5. sqlstr ="SELECT id FROM myTable WHERE ID='" & str &'"
6. cmd=new OledbCommand(sqlstr,con) ''pass the value sqlstrto ''command
7. ans=cmd.ExequtenonQuery()
8. if ans= true then
9. ''msgbox enter another no
10. elseif ans= flase then
11. ''move further that is id is unique
12. end if

this is what i get:

[IMG]http://i42.tinypic.com/15nlk7n.png[/IMG]

If i change the Oleddbcommand then i get loads of other errors
whats going wrong?

show me your code with error whatever you are writing and what do you want to do. then i can help you

http://i42.tinypic.com/15nlk7n.png

thats a picture of what happens after i put the code in that u gave me ...

this is a picture of the form

http://i40.tinypic.com/x4hytu.png

when i enter a new id and the details of a customer and click on add.
i want the program to search to check if the ID number entered already exists. If it does then the program tells the user that the ID already exists.

look here at the top of the form code import the OleDb namespace

imports system.data.OLedb
public class form1
dim cmd as OledbCommand
dim 
''after that on keydown event write code
private sub textboxid_keydown()
if e.keycode=keys.enter then
str=textboxid.text
sqlstr ="SELECT id FROM myTable WHERE ID='" & str &'"
 cmd=new OledbCommand(sqlstr,con)
ans=cmd.ExequtenonQuery()
if ans= true then
 ''msgbox enter another no
 elseif ans= flase then
 ''move further that is id is unique
 end if
end if
end sub
end class

same code you can write on textbox leave event
in my previous post i just make mistake in writing the command

dim cmd as OleddbCommand ''creating command

if the ans is false then further insert new number or you can create a random number for that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.