Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
please tell me how to check that mobile number entered for each employee is different.if the number already exists in database it should give error when i go to next textbox using tab or click.
Posted
Updated 6-Sep-12 1:09am
v2

You can do it using several methods:

1. Using the phone number as a key that don't allow duplicates.
2. Prior to add the value execute a select call and check the result of the call:
http://www.w3schools.com/sql/sql_select.asp[^]
http://www.fryan0911.com/2009/06/vbnet-how-to-select-records-from-sql.html[^]

Showing the error depends on you but I would use a MessageBox or any other method to tell the user that something has gone wrong.

Hope this helps...
 
Share this answer
 
SQL
--for existing data
select * from table1 where MobileNo in -- fetch whole row having same numbers
(
     select mobileno from table1 group by  a having  count(*) > 1 -- fetch mobileno list which are repeated
)


When entering data
VB
'on textbox leave event
       Dim exist = DBGetData("select count(*) from tabel1 where mobileno='" & txtmobileno.text & "'")
       If exist > 0 Then
           'show message not allowed
           'empty Mobile number textbox
           'focus txtmobileno
       End If

Happy Coding!
:)
 
Share this answer
 
Comments
[no name] 6-Sep-12 7:30am    
please can u tell me in detail about the below part

--for existing data
select * from table1 where MobileNo in -- fetch whole row having same numbers
(
select mobileno from table1 group by a having count(*) > 1 -- fetch mobileno list which are repeated
)

after 'in' what i have to give..
Aarti Meswania 6-Sep-12 7:36am    
execute this query after modifying table & column names as per your database
It will give you list of rows which have same mobile numbers in your table.
read comment lines it shows meaning of written query
[no name] 7-Sep-12 2:34am    
please tell me what is this DBGetData...??
Aarti Meswania 7-Sep-12 2:41am    
DBGetData is a function that will connect to database and will execute query and return output,
sqlconnection...,sqlDataAdapter... all that process will be done inside function.
you can fetch data using any method you like but the sql query will be

"select count(*) from tabel1 where mobileno='" & txtmobileno.text & "'"
[no name] 7-Sep-12 3:14am    
i wrote the code in textbox leave event but its not responding..please check the below code

Private Sub textBox8_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textBox8.Leave

Dim exist = "select count(*) from Employee where mobile='" & textBox8.Text & "'"
If exist > 0 Then
'show message
MsgBox("not allowed")
'empty Mobile number textbox
'focus txtmobileno
End If
try this
Make Stored Procudureof below code:
SQL
create procedure check_contact_No
	@parameter_entered_contact_num as varchar(50)
as
begin
if(not Exists(select ad_contact_1 from admin_user_profile where ad_contact_1 = @parameter_entered_contact_num  ))
if( Exists(select ad_contact_1 from admin_user_profile where ad_contact_1 = @parameter_entered_contact_num  ))
	select COUNT(*) from admin_user_profile 
else 
	select COUNT(*) from admin_user_profile where ad_contact_1 = @parameter_entered_contact_num 
end

and check this in vb.net code


VB
'on textbox leave event
       Dim objcls As New database_Connection
        Dim da As New OleDbDataAdapter
        Dim ds As New DataSet
        Dim cmd As New OleDbCommand
        Dim con As New OleDbConnection
        Dim flag As Boolean
        Try
            con = objcls.GetConnection()
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "check_contact_No"
            cmd.Parameters.Add(New OleDbParameter("@parameter_entered_contact_num", txt_contactno.Text))
            cmd.Connection = con
            da.SelectCommand = cmd
            da.Fill(ds)
            If (ds.Tables(0).Rows.Count = 0) Then
                'show message not allowed
                'empty Mobile number textbox
                'focus txtmobileno
            Else
               'number is unique
               'do your coding
            End If
            Return flag
        Catch ex As Exception

        End Try
 
Share this answer
 
v2
Comments
[no name] 7-Sep-12 1:47am    
it;s giving error in OLEdb connections.please tell me why..???
prashant patil 4987 7-Sep-12 1:58am    
can you post error??
u can use SqlConnection instead of OledbConnection.
[no name] 7-Sep-12 2:23am    
it's not returning any error but not producing any output..i gave a breakpoint but the control is not going there..
prashant patil 4987 7-Sep-12 4:16am    
can you post your code....

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