Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating a messenger using a database and I'm stuck on trying to retrieve messages. This is what I have so far.
C#
strQuery = "SELECT message FROM chat WHERE chat.from ='" & TextBox5.Text & "' AND chat.to ='" & TextBox3.Text & "'"

What the query does is retrieve messages from the database with textbox5 being the sender and textbox3 being the retriever. Currently it only displays messages sent to you by the other party and I have it working fine. But I want it to display both sent messages and your messages. For example:
Tom J.: Hey man, what's up?
Me: Nothing much, how about yourself?

It's currently only showing sent messages. The database structure is of the following:
ID (Message ID) Message (Message area) From (ID of sender) To (ID of retriever)

I want to be able to retrieve both, FROM and TO and display them in order using a richtextbox. Is this possible? If so how would I go about doing this?
Posted
Updated 25-Sep-14 15:24pm
v2
Comments
Kschuler 25-Sep-14 15:05pm    
If it were me...I'd give every conversation an Message ID, and use THAT to pull the data. That way you'd get both the FROM and the TOs.

You can concatenate the result from your query like this
SQL
SELECT concat(message_from, ' ', message_to) AS message FROM chat;

This is MySQL syntax, but the syntax should be similar in other flavors.

I agree with Mehdi, though, that you should consider to change your design.
 
Share this answer
 
You need a new model for this, like "Conversation" and "Conversation Details" where the "Conversation" has a list of participants (you can have more than 2 people in the chat) and an ID and the "Conversation Details" has a datetime, UserId, Message, ConversationId etc.
 
Share this answer
 
Comments
CodexHawk 25-Sep-14 20:12pm    
Is there not a way to pull both sql columns and list them as 1 result?
try this

SQL
SELECT userName +' : '+ [Message] as message FROM chat
INNER JOIN userTbl ON userTbl.userId=chat.[from]


if u have a date field then use order by datefield desc


good luck ;-)
 
Share this answer
 
Comments
CodexHawk 28-Sep-14 15:32pm    
I'm a bit confused on how to set this query up.. Can you set it up using the information I've provided above?
george4986 28-Sep-14 23:32pm    
as per ur given info the chat table structure is like below right?
ID (Message ID) Message (Message area) From (ID of sender) To (ID of retriever).
can u provide the user table structure?
Solved this by easily using UNION.

SQL
SELECT * FROM Table1
UNION
SELECT * FROM Table2
 
Share this answer
 

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