Click here to Skip to main content
15,914,014 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i want to show message from message table with user detail from registation table
and using following code.
pls help
C#
protected void Page_Load(object sender, EventArgs e)
{
   
    string friend = Request.QueryString["friend"].ToString();
    string me = Session["userid"].ToString();
    string S = ConfigurationManager.ConnectionStrings["A1"].ConnectionString;
    con = new SqlConnection(S);
    //following quiery showing userdetail and message but showing agian and agian 7time because i have 7 users in Registration table

    ad = new SqlDataAdapter("select * from Message1 as a,Registration as b where( b.userid=a.friend or b.userid=a.me) and (friend=" + friend + " and me=" +me+") OR (me= " +friend + " and friend=" +me+ ")", con);
    ds = new DataSet();

 // and this following quiery work  but no user detail
//  ad = new SqlDataAdapter("select * from Message1 where (friend=" + friend + " and me=" + me + ") OR (me= " + friend + " and friend=" + me + ")", con);
     // ds = new DataSet();
 ad.Fill(ds);
 listview1.DataSource = ds.Tables[0];
        listview1.DataBind();
Posted
Updated 6-May-15 19:17pm
v5
Comments
ZurdoDev 5-May-15 12:35pm    
What is your question?
Mantu Singh 5-May-15 13:04pm    
what are columns of your table ?
Member 11659119 5-May-15 13:49pm    
register table

[userid] NUMERIC (18) IDENTITY (1, 1) NOT NULL,
[Username] VARCHAR (50) NOT NULL,
[Password] VARCHAR (50) NOT NULL,
[Nikname] VARCHAR (50) NULL,
[FirstName] VARCHAR (50) NULL,
[LastName] VARCHAR (50) NULL,
[date] VARCHAR (50) NULL,
[City] VARCHAR (50) NULL,
[State] NCHAR (10) NULL,


message1 table

[Messageid] NUMERIC (18) IDENTITY (1, 1) NOT NULL,
[Date] VARCHAR (50) NULL,
[me] VARCHAR (50) NULL,
[friend] VARCHAR (50) NULL,
[Message] VARCHAR (MAX) NULL,
[status] VARCHAR (50) NULL,
[time] VARCHAR (50) NULL,
virusstorm 6-May-15 10:30am    
Your issue is your table structure. You do not have a clear relationship defined between your two tables. The SQL needed to achieve what you are looking for is going to be highly complex and even then, I don't think it will be accurate. I strongly suggest revisiting your database
Member 11659119 8-May-15 15:07pm    
plss tell me an example like how do to clear relationship defined between tables

1 solution

This solution is a total stab in the dark as I don't know what your project is trying to do.

SQL
CREATE TABLE dbo.Users (
	UserId     INT IDENTITY NOT NULL,
	Username   VARCHAR(50)  NOT NULL,
	[Password] VARCHAR(50)  NOT NULL,
	NickName   VARCHAR(50)  NULL,
	FirstName  VARCHAR(50)  NULL,
	LastName   VARCHAR(50)  NULL,
	[Date]     DATE         NULL,
	City       VARCHAR(50)  NULL,
	[State]    VARCHAR(10)  NULL,
	CONSTRAINT PK_Users PRIMARY KEY(UserId)
)

CREATE TABLE dbo.[Messages] (
	MessageId   INT IDENTITY NOT NULL,
	[DateTime]  DATETIME     NOT NULL,
	FromUserId  INT          NOT NULL,
	ToUserId    INT          NOT NULL,
	[Message]   VARCHAR(MAX) NOT NULL,
	[Status]    VARCHAR(50)  NOT NULL,
	CONSTRAINT PK_Messages PRIMARY KEY (MessageId),
	CONSTRAINT FK_Messages_Users_FromUserId FOREIGN KEY (FromUserId) REFERENCES dbo.Users(UserId),
	CONSTRAINT FK_Messages_Users_ToUserId FOREIGN KEY (ToUserId) REFERENCES dbo.Users(UserId)
)

SELECT *
FROM
(
		--This will get all of the messages addressed to the user
		SELECT
			*
		FROM dbo.Users
		INNER JOIN dbo.[Messages]
			ON Users.UserId = [Messages].ToUserId
	UNION
		--This will get all of the messages from the user
		SELECT
			*
		FROM dbo.Users
		INNER JOIN dbo.[Messages]
			ON Users.UserId = [Messages].FromUserId
) AS AllMessages
WHERE
	UserId = @userId


I highly recommend taking a look at this presentation on the Microsoft Virtual Academy web site to learn about database design:
http://www.microsoftvirtualacademy.com/training-courses/database-fundamentals[^]
 
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