guys,

how to check data if it is already in the database?

i am using sql server..

thanks

Recommended Answers

All 8 Replies

Hi you can use IF EXISTS(Select * from table where condition is)

can u give me a full example?..im really a noob..:)

what exactly you want to search for? can you please give me the table and column name? so that i can write query

what i did, is first i search the record on the database....
then use sqlCommand.ExecuteScalar()

SELECT field1 from tableName  WHERE field1 = '" & txtbox1.text & "'

Dim ReturnValue as  integer
ReturnValue = SqlCommand.executeScalar

if executeScalar returns 1 then its existing....

'try this 
'open th connection to the database
'cmd is the variable of the sqlcommand
'cnn is the variable of sqlconnection

cnn.open
Dim ReturnValue as  string
cmd=new sqlcommand("SELECT field1 from tableName  WHERE field1 = '" & txtbox1.text & "'",cnn)
ReturnValue =cmd.executeScalar
if ReturnValue  <> "" then
msgbox("Record Already Exist"):exit sub
end if
cnn.close

Hi guys!

If you are not aware of "SQL Injection" please read this article or just google it.

Do not use SQL query by concatenating hard-coded strings because this way malicious code is inserted into sql strings.

Do use Type-Safe SQL Parameters.

Dim Cnn as New SqlConnection()
Dim Cmd as New SqlCommand()

Cnn.ConnectionString="Connection_string"

Cmd.CommandText="SELECT field1 from tableName  WHERE field1 = @para1"
Cmd.Connection=Cnn
Cmd.Parameters.AddWithValue("@para1",TextBox1.Text)

Dim isFound as boolean=false

Dim reader as SqlDataReader

Cnn.Open()
reader=Cmd.ExecuteReader()
If reader.Read() Then
    isFound=true
End If
reader.Close()
Cnn.Close()

Hi guys!

If you are not aware of "SQL Injection" please read this article or just google it.

Do not use SQL query by concatenating hard-coded strings because this way malicious code is inserted into sql strings.

Do use Type-Safe SQL Parameters.

Dim Cnn as New SqlConnection()
Dim Cmd as New SqlCommand()

Cnn.ConnectionString="Connection_string"

Cmd.CommandText="SELECT field1 from tableName  WHERE field1 = @para1"
Cmd.Connection=Cnn
Cmd.Parameters.AddWithValue("@para1",TextBox1.Text)

Dim isFound as boolean=false

Dim reader as SqlDataReader

Cnn.Open()
reader=Cmd.ExecuteReader()
If reader.Read() Then
    isFound=true
End If
reader.Close()
Cnn.Close()

adatapost, i have a question for you about paramiterized queries.
do you mind if i PM you a question, or if i start a thread would u reply to it ?

Hi jlego,

Please start a new thread.

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.