When a user wants to view another user profile on user profile page this error will show up
ERROR
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 72: protected void OnDataBound(object sender, EventArgs e)
Line 73: {
Line 74: string userName = (FormView1follow.Row.Cells[0].FindControl("hfUserName") as HiddenField).Value;
Line 75: string friendsUserName = (FormView1follow.Row.Cells[0].FindControl("hfFriendsUserName") as HiddenField).Value;
Line 76: string followStatus = (FormView1follow.Row.Cells[0].FindControl("hfFollowStatus") as HiddenField).Value;
HTML
<div class="text-center">
<asp:FormView ID="FormView1follow" runat="server" OnDataBound="OnDataBound" Width="100%">
<ItemTemplate>
<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("Status1") %>' />
</asp:LinkButton>
<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") %>' />
</asp:LinkButton></ItemTemplate>
</asp:FormView>
</div>
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);
FormView1follow.DataBind();
}
}
}
private DataTable GetData(string userName)
{
DataTable dt = new DataTable();
// DataTable dt = new DataTable();
// dt.Columns.AddRange(new DataColumn[3] { new DataColumn("category", typeof(string)), new DataColumn("A10", typeof(Int32)), new DataColumn("A20", typeof(Int32)) });
// dt.Rows.Add('A', 1, 2);
// dt.Rows.Add('B', 5, 3);
// dt.Rows.Add('C', 2, 1);
// dt.Rows.Add('D', 8, 1);
// dt.Rows.Add('E', 7, 5);
// string name = "FF";
//var querycount = (from dd in dt.AsEnumerable()
// where dd.Field<string>("category") == name
// select dd).Count();
string constr = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
string query = "SELECT u.UserName,u.FriendUserName,u.FollowStatus,u.Status1,u.Status2 FROM USERFollow as u WHERE u.UserName = '" + userName + "' OR u.FriendUserName='" + userName + "'";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
con.Close();
}
}
return dt;
}
protected void OnDataBound(object sender, EventArgs e)
{
string userName = (FormView1follow.Row.Cells[0].FindControl("hfUserName") as HiddenField).Value;
string friendsUserName = (FormView1follow.Row.Cells[0].FindControl("hfFriendsUserName") as HiddenField).Value;
string followStatus = (FormView1follow.Row.Cells[0].FindControl("hfFollowStatus") as HiddenField).Value;
LinkButton btnFollow = (LinkButton)FormView1follow.Row.Cells[0].FindControl("btnfollowFollow");
LinkButton btnUnFollow = (LinkButton)FormView1follow.Row.Cells[0].FindControl("Unbtnfollow");
if ((userName.Trim() != friendsUserName.Trim()) && (followStatus.ToLower().Trim() == "true"))
{
btnFollow.Visible = true;
btnUnFollow.Visible = false;
}
else if ((friendsUserName.Trim() != userName.Trim()) && (followStatus.ToLower().Trim() == "false"))
{
btnFollow.Visible = false;
btnUnFollow.Visible = true;
}
}