Click here to Skip to main content
16,004,587 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to retrieve all message from database of oledb on the page of asp.
in the format of left side is sender message and right side is reciever message.

What I have tried:

dim cmd as new oledbcommand("select count(*) from message where sender='"&textbox1&"'",con)
n=cmd.executesclare
if n>0 then
cmd=new oledbcommand("select msg from message where sender='"&textbox1&"'",con)
label1.text=cmd.executescalar()
endif
Posted

Ignoring that that won't work, because a Textbox is not the same as the content, start by never doing it like that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
For example:
VB
Dim cmd As New OledbCommand("SELECT COUNT(*) FROM message WHERE sender = @SNDR",con)
cmd.Parameters.AddWithValue("@SNDR", textbox1.Text)
n=cmd.ExecuteScalar()

Fixing that may solve your problem as well, but concatenating strings to do this is very dangerous, particularly in a web application, where anyone could delete your database from the other side of the world just by typing in your text box!
 
Share this answer
 
First, don't be so rude. And use a proper title for your question.

The most important thing you need to learn about data access is how to use parameters. Never concatenate user values into an SQL statement.
Next, getting the count of records is a pointless waste of time.
Finally, if you expect to receive many records, use ExecuteReader, not ExecuteScalar.

I assume you are using Access as a database. Using parameters with Access, and some other databases, is not as easy as with SQL Server and some others, but it can be done if you're careful.

My VB-fu is weak, but this should be enough to get you going:

VB
dim cmd as new oledbcommand("select msg from message where sender=@sendername",con)
dim prm as cmd.createparameter()
prm.parametername="sendername"
prm.value=textbox1
cmd.parameters.add(prm)
dim rdr as cmd.executereader()
while(rdr.read())
  'do stuff with the data values
end while
 
Share this answer
 
Comments
GIRISH SONI 26-May-16 1:50am    
Thank you so much i will do try my best.
i am not known proper english but i trying it.

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