How to check if user in user account table is opposite FriendUserName column in USERFollow table

Last Reply on Apr 27, 2016 09:29 AM By dharmendr

Posted on Apr 27, 2016 08:54 AM

How to check if a user in user account table is opposite FriendUserName column or UserName Column in USERFollow table and if yes show lable Marged , but if not show lable Not Merged

HTML

<div>
    <asp:Label ID="lblMerged" Text="Merged" runat="server" Visible="false" />
    <asp:Label ID="lblNotMerged" Text="NotMerged" runat="server" Visible="false" />
   
</div>

C#

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] != null && Session["UserName"].ToString() != string.Empty)
        {
            Session["Name"] = Session["Name"].ToString();
            string username = Session["UserName"].ToString();
            if (Request.QueryString["UserName"] != null)
            {
                if (!IsPostBack)
                {
                    DataTable dt = GetData(Request.QueryString["UserName"]);
                    if (dt.Rows.Count > 0)
                    {
                        string userStatus = dt.Rows[0]["UserStatus"].ToString().ToLower();
                        if (userStatus == "exist")
                        {
                            lblMerged.Visible = true;
                        }
                        else if (userStatus == "notexist")
                        {
                            lblNotMerged.Visible = true;
                        }
                       
                    }
                }
            }
        }
    }
 
private DataTable GetData(string userName)
{
    DataTable dt = new DataTable();
    string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    string query = "Getbuttons";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
             cmd.Parameters.AddWithValue("@UserName", Request.QueryString["Id"].ToString());
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(dt);
            }
            con.Close();
        }
    }
 
    return dt;
}  

 

You are viewing reply posted by: dharmendr on Apr 27, 2016 09:29 AM.
Posted on Apr 27, 2016 09:29 AM

Hi micah,

I have created sample. You need to change as per your requirement.

HTML

<div>
    <asp:Label ID="lblMerged" Text="Merged" runat="server" Visible="false" />
    <asp:Label ID="lblNotMerged" Text="NotMerged" runat="server" Visible="false" />
    <asp:Label ID="lblUserNotValid" Text="User Not Valid" runat="server" Visible="false" />
</div>

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["UserName"] != null)
    {
        if (!IsPostBack)
        {
            DataTable dt = GetData(Request.QueryString["UserName"]);
            if (dt.Rows.Count > 0)
            {
                string FriendUserName = string.Empty;
                string userName = dt.Rows[0]["UserName"].ToString().ToLower();
                if (userName != "user not valid")
                {
                    FriendUserName = dt.Rows[0]["FriendUserName"].ToString().ToLower();

                    if (userName != FriendUserName)
                    {
                        lblMerged.Visible = true;
                    }
                    else
                    {
                        lblNotMerged.Visible = true;
                    }
                }
                else
                {
                    lblUserNotValid.Visible = true;
                }
            }
        }
    }
}

private DataTable GetData(string userName)
{
    DataTable dt = new DataTable();
    string constr = ConfigurationManager.ConnectionStrings[1].ConnectionString;
    string query = "Getbuttons";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserName", userName);
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(dt);
            }
            con.Close();
        }
    }

    return dt;
}

SQL

CREATE PROC Getbuttons
	@UserName VARCHAR(50)
AS
BEGIN
	IF EXISTS(SELECT UserName FROM User3 WHERE UserName = @UserName)
	BEGIN
		SELECT UserName,FriendUserName FROM UserFollow WHERE UserName = @UserName OR FriendUserName = @UserName
	END
	ELSE
	BEGIN
		SELECT 'User not valid' AS UserName
	END
END

--EXEC Getbuttons 'Steve1'