Error in Content Page on initializing FacebookClient - c#

I am getting an error "Object Reference is not set to an Instance of an object" in the ContentPage of my MasterPage Facebook Application.
Site.master.cs
public FacebookSession CurrentSession
{
get { return (new CanvasAuthorizer()).Session; }
}
protected void Page_Load(object sender, EventArgs e)
{
var auth = new CanvasAuthorizer { Perms = "email,read_stream,publish_stream,offline_access,user_about_me" };
if (auth.Authorize())
{
ShowFacebookContent();
}
}
private void ShowFacebookContent()
{
var fb = new FacebookClient(this.CurrentSession.AccessToken);
dynamic myInfo = fb.Get("me");
lblName.Text = myInfo.name;
imgProfile.ImageUrl = "https://graph.facebook.com/" + myInfo.id + "/picture";
lblBirthday.Text = myInfo.birthday;
pnlHello.Visible = true;
}
This master Page works OK & displays UserName & ProfilePic.
Default.aspx.cs
SiteMaster myMasterPage;
protected void Page_Load(object sender, EventArgs e)
{
myMasterPage = this.Page.Master as SiteMaster;
}
public void LinkButton1_Click(object sender, EventArgs e)
{
var fb = new FacebookClient(this.myMasterPage.CurrentSession.AccessToken);
dynamic feedparameters = new ExpandoObject();
feedparameters.message = (message_txt.Text == null ? " " : message_txt.Text);
feedparameters.user_message_prompt = "userPrompt";
/*Dictionary<string, object> feedparameters = new Dictionary<string, object>();
feedparameters.Add("message", "Testing Application");
feedparameters.Add("user_message_prompt", "Post To Your Wall");
feedparameters.Add("display", "iframe");*/
dynamic result = fb.Post("me/feed", feedparameters);
}
Even this Page Loads OK but Problem comes when I try to Post using LinkButton.
Following Line gives the error.
var fb = new FacebookClient(this.myMasterPage.CurrentSession.AccessToken);
On LinkButton Click Object Reference is not set to an Instance of an object...
I will really appreciate some help.

Wel finally found what was the problem. Needed to add a hidden field.
<input type="hidden" name="signed_request" value="<%: Request.Params["signed_request"]%>"/>
I think this is neither mentioned any where in the documentation nor in the Provided Samples.

Related

I have the following error when I use DropDownlist for 3rd or second time! "failed to load viewstate is being loaded must .."

I have a "listview" for showing list of employee and a "dropdownlist" to select department. The following error occurs when I use "DropDownlist" for 3rd or second time:
"Failed to load viewstate".
The control tree into which viewstate is being loaded must match the control tree that was used to save "viewstate" during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request."
This is asp.net Webform and I have to use this technology and have no other choice .
namespace .Presentation.general
{
public partial class Listg : PageBase
{
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/App_MasterPages/empty.Master";
}
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateDepartmentsDropDownList();
GeneralObjectDataSource.SelectParameters["Department"].DefaultValue = "";
decimal presence = Convert.ToDecimal(Data.EmployeeDB.Create().GetCountEMPOnlineToday());
decimal visibles = Convert.ToDecimal(Data.EmployeeDB.Create().GetCountVisiblesEmployees());
visibles = (visibles == 0 ? 1 : visibles);
PresenceLabel.Text = System.Math.Round((presence / visibles) * 100, 1).ToString() + "% " + string.Format(" ({0})", presence);
}
}
public void Search(object sender, EventArgs e)
{
GeneralObjectDataSource.SelectParameters["name"].DefaultValue = Common.Converter.ConvertToFarsiYK(NameTextBox.Text.Trim());
if (PresenceRadioBottonList.SelectedValue == "1")
{
GeneralObjectDataSource.SelectParameters["onlyPresence"].DefaultValue = "true";
}
else
{
GeneralObjectDataSource.SelectParameters["onlyPresence"].DefaultValue = "false";
}
DataListView.DataBind();
}
public void select_department_SelectedIndexChanged(object sender, EventArgs e)
{
GeneralObjectDataSource.SelectParameters["name"].DefaultValue = "";
GeneralObjectDataSource.SelectParameters["Department"].DefaultValue = select_department.SelectedItem.Text;
GeneralObjectDataSource.DataBind();
DataListView.DataBind();
}
private void PopulateDepartmentsDropDownList()
{
select_department.DataSource = Biz.EmployeeBO.GetDepartments();
select_department.DataTextField = "Name";
select_department.DataValueField = "ID";
select_department.DataBind();
select_department.Items.Insert(0, new ListItem("", "0"));
select_department.SelectedValue = Biz.Settings.SelectedDepartmentID;
}
}
}

Avoid controls with same ID

I'm creating dynamically controls, assigning it at the same dynamically names and ID's but when I click over a button "A" and then over the button "B" and then again to the button "A" this throw me an error
Multiple controls with the same ID were found. FindControl requires that controls have unique IDs.
this is my code and how I try to avoid the repeating I
protected void DynamicButton()
{
//BAD TOOLS INTO THE LIST AND SHOW
List.ListUsers listsArea = new List.ListUsers();
List<Data.Area> Area = listsArea.AreaList();
List<Data.Area> ListOfEquiposNoOk = Area.Where(x => x.AREA == "ENG" && x.STANDBY == 1).ToList();
List<Button> BotonesBad = new List<Button>();
var TeamBad = ListOfEquiposNoOk.Select(x => x.TEAM).Distinct().ToList();
foreach (var team in TeamBad)
{
Button newButtonBad = new Button();
if (newButtonBad.ID != newButtonBad.ID)
{
BotonesBad = Bad.Controls.OfType<Button>().ToList();
BotonesBad.Add(newButtonBad);
}
else
{
newButtonBad.CommandName = "Btn" + Convert.ToString(team);
newButtonBad.ID = "BtnB_" + Convert.ToString(team);
newButtonBad.Text = team;
newButtonBad.CommandArgument = "ENG";
newButtonBad.Click += new EventHandler(newButton_Click);
Bad.Controls.Add(newButtonBad);
newButtonBad.Click += new EventHandler(newButton_Click);
newButtonBad.CssClass = "btn-primary outline separate";
}
}
I need the ID's to fire an UpdatePanel
ADDED
public partial class Dashboard : System.Web.UI.Page
{
static bool enableGood = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DynamicButton();
}
else if(enableGood)
{
DynamicButton();
}
}
protected void DButton(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showAndHide();", true);
enableGood = true;
DynamicButton();
}
Assuming "team" is an object in this line:
newButtonBad.ID = "BtnB_" + Convert.ToString(team);
There could be your problem. ".ToString()" usually returns the type as string. So every button will get the same name (since they are all of type "team").
You could override ToString() in your team object, or use a specific team property (e.g team.ID). You could use the property like this:
newButtonBad.ID = "BtnB_" + team.ID.ToString();
Also as already pointed out in the comments, change your evaluation from
if (newButtonBad.ID != newButtonBad.ID)
to
if (team.ID != newButtonBad.ID)
That should do the trick.

Cookie fails to be created in vs express 2013 for web c#

First off, I'm on a Win7 laptop using Chrome in my IE.
I can store the data I have with a session variable but cannot get it to work with a cookie.
I attempt to set the cookie in a button click event and then attempt to read it in a textChanged event. I can see the cookie object get populated in the IE but it never seems to actually get created when I look for it with chrome://settings/cookies.
Here is my code:
protected void btnSubmitQuery_Click(object sender, EventArgs e)
{
List<string> geometryList = new List<string>();
try
{
using (SqlConnection conn = new SqlConnection(this.SqlQri.ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(this.tbxSQLQri.Text, conn))
{
object result = cmd.ExecuteScalar();
Session["polys"] = result.ToString();
HttpCookie myPolys = new HttpCookie("polys"); // This seems to work
myPolys.Value = result.ToString();
myPolys.Expires = DateTime.Now.AddDays(1);
Response.SetCookie(myPolys);
}
conn.Close();
}
}
catch (SqlException ex)
{
this.tbxSQLQri.Text = "Query Exception";
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
// Read Session val
string looky = (string)(Session["polys"]);
this.TextBox1.Text = looky;
string lookyHere = "";
if (HttpContext.Current.Request.Cookies["polys"] != null) // This is never true!
{
lookyHere = Request.Cookies["polys"].Value;
}
this.TextBox2.Text = lookyHere;
}
I declare a Private constant string with the name as follows
private const string cnstLoginCookieName = "Super-User";
inside of my Page_load I have the following declared
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie objCookie = new HttpCookie(cnstLoginCookieName);
objCookie.Values["Login"] = username.Trim();
objCookie.Values["Company"] = "CoolFirm";
objCookie.Expires = DateTime.MaxValue;
Response.Cookies.Add(objCookie);
}
OK, This part of my problem was solved. My string was too long to go in a cookie.
POLYGON ((-122.143636 47.257808, -122.143685 47.257807, -122.143877 47.257802, -122.143993 47.257799, -122.143989 47.257746,
If I changed the value to "hello world" the cookie was successful.
Thank you for your help on this.

Winform Simple Link

I use a LabelLink contorl in a WinForm.
On form load I set the desired link:
LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = "http://stackoverflow.com/questions/ask";
linkLabel1.Links.Add(link);
On click :
void LinkLabel1LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.Link.LinkData as string);
}
The link is gray and nothing happens when I click on it.
What is missing?
Try this
LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = "http://stackoverflow.com/questions/ask";
linkLabel1.Links.Add(link);
this.linkLabel1.Links[0].LinkData = "Ask a question";
linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.LinkLabel1LinkClicked‌​);
and
void LinkLabel1LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string url;
url = e.Link.LinkData.ToString();
if (!url.Contains("://"))
url = "http://" + url;
var myLink = new ProcessStartInfo(url);
Process.Start(myLink);
linkLabel1.LinkVisited = true;
}

session variables in an ASP.NET

hi guy i am trying to place my session in to a drop down, any help would be great.
at the moment it puts the data in to a label, i wish to put it into a dropdown with it adding a new string every time i click button without getting rid of the last
default page
protected void Button1_Click1(object sender, EventArgs e)
{
Session["Fruitname"] = TbxName.Text; // my session i have made
}
output page
protected void Page_Load(object sender, EventArgs e)
{
var fruitname = Session["Fruitname"] as String; // my session ive made
fruit.Text = fruitname; // session used in lable
}
Have Tried
var myFruits = Session["Fruitname"] as List<string>;
myFruits.Add(listbox1.Text);
but i get error when i try to run the program
Broken glass thanks for your help, it is still not doing what i need but its getting there.
var fruitname = Session["Fruitname"] as String; // my session ive made
fruit.Text = string.Join(",", fruitname); // session used in lable
this is what is working. i need a dropdown to display all the strings put into TbxName.Text; to output into fruit
Just use a List<string> instead of a string then.
var myFruits = Session["Fruitname"] as List<string>;
myFruits.Add(TbxName.Text);
Has been fixed using code found else where
button page code bellow
protected void Button1_Click1(object sender, EventArgs e)
{
// Session["Fruitname"] = TbxName.Text; // my session i have made
MyFruit = Session["Fruitname"] as List<string>;
//Create new, if null
if (MyFruit == null)
MyFruit = new List<string>();
MyFruit.Add(TbxName.Text);
Session["Fruitname"] = MyFruit;
{
public List<string> MyFruit { get; set; }
}
page where display
protected void Page_Load(object sender, EventArgs e)
{
MyFruit = Session["Fruitname"] as List<string>;
//Create new, if null
if (MyFruit == null)
MyFruit = new List<string>();
ListBox1.DataSource = MyFruit;
ListBox1.DataBind();
}
public List<string> MyFruit { get; set; }
}

Categories