Check if User in Account Table exists in another table using C# in ASP.Net

Last Reply on Apr 27, 2016 07:11 AM By dharmendr

Posted on Apr 27, 2016 05:32 AM

I want to check if a user in User table is found either in UserName Column or in FriendUserName Column in UserFollow table and if yes display lable found, But if no display lable notfound. This code will be excuted onse a user logs in the connection code


protected void Page_Load(object sender, EventArgs e)
        if (Session["UserName"] != null && Session["UserName"].ToString() != string.Empty)

            string userName = Session["UserName"].ToString();
            if (!this.IsPostBack)
                FormView1follow.DataSource = GetData(userName);
    private DataTable GetData(string userName)
        DataTable dt = new DataTable();
        string constr = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;

        string query = "SELECT u.UserName,u.FriendUserName,u.FollowStatus,u.Status,u.Status2 ,up.UserName,up.Name FROM USERFollow as u, User3 as up WHERE up.UserName = '" + userName + "' OR u.FriendUserName='" + userName + "'";
        using (SqlConnection con = new SqlConnection(constr))
            using (SqlCommand cmd = new SqlCommand(query, con))
                //     cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@UserName", Request.QueryString["Id"].ToString());
                //     cmd.Parameters.AddWithValue("@FriendUserName", Session["UserName"].ToString());
                cmd.Parameters.AddWithValue("@FriendUserName", Request.QueryString["Id"].ToString());
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))

        return dt;



<asp:FormView ID="FormView1follow" runat="server" OnDataBound="OnDataBound" Width="100%">
            <asp:HiddenField ID="hfUserName" runat="server" Value='<%# Eval("UserName") %>' />
            <asp:HiddenField ID="hfFriendsUserName" runat="server" Value='<%# Eval("FriendUserName") %>' />
            <asp:HiddenField ID="hfFollowStatus" runat="server" Value='<%# Eval("FollowStatus") %>' />
            <asp:LinkButton ID="btnfollowFollow" runat="server" ToolTip="follow user" CssClass=" btn btn-twitter fa fa-user" Font-Bold="True" >
                <i class=""  style="margin-right:2px" ></i>  <asp:Label ID="lblFollow" runat="server" Text='<%# Eval("Status") %>' />
            </asp:LinkButton>&nbsp; <asp:LinkButton ID="Unbtnfollow" runat="server" ToolTip="follow user" CssClass="  btn btn-twitter fa fa-user-plus" Font-Bold="True">
                <i class=""  style="margin-right:2px" ></i>  <asp:Label ID="lblUnFollow" runat="server" Text='<%# Eval("Status2") %>' />


You are viewing reply posted by: dharmendr on Apr 27, 2016 07:11 AM.