Problem setting class properties - c#

I keep getting a null exception at the ; below. The ApiUsername & ApiPassword have values so I don't undestand if I just set this up wrong or what. The Credentials property is a certain type which has the Username and Password properties that need to be set.
So I have the auto-propery defined:
public CustomSecurityHeaderType SoapCallCredentials { get; private set; }
Then whenever this is hit, I get a null exception and can't figure out why.
private void SetApiCredentials()
{
SoapCallCredentials = new CustomSecurityHeaderType
{
Credentials =
{
Username = PayPalConfig.CurrentConfiguration.ApiUserName,
Password = PayPalConfig.CurrentConfiguration.ApiPassword
}
};
UrlEndPoint = PayPalConfig.CurrentConfiguration.ExpressCheckoutSoapApiEndPoint;
}

I am thinking you need a new....
Credentials = new WhatEverThisTypeIs()
{
Username = PayPalConfig.CurrentConfiguration.ApiUserName,
Password = PayPalConfig.CurrentConfiguration.ApiPassword
}

From the eBay API Example
Credentials needs to be instantiated first, like:
Credentials = new UserIdPasswordType()

Related

Error fetching registered user data - C# MVC

Friends, I'm trying to do tests with payment. I need to get the address of the logged in user to register the payment. I have this method:
transaction.Shipping = new Shipping
{
Name = cliente.Name,
Fee = Mascara.ConverterValorPagarMe(fee),
Expedited = false,
AddressApi = new AddressApi()
{
Country = "br",
State = person.address.State,
City = person.address.City,
Neighborhood = person.address.Neighborhood,
Street = person.address.Street,
StreetNumber = person.address.StreetNumber,
Zipcode = person.address.Zipcode
}
};
I have a table of people and an address table. I can register the user correctly, but I get an error when trying to get the address to make the payment.
System.NullReferenceException: 'Object reference not set to an instance of an object.
LojaVirtual.Models.person.address.get retornou null.
The line
Name = cliente.Name
returns the user name, but
State = person.address.State
returns null.
My method for obtaining user information is as follows:
public person GetPerson()
{
// Deserializar
if ( _sessao.Existe(Key))
{
string personJSONString = _sessao.Consult(Key);
return JsonConvert.DeserializeObject<person>(personJSONString);
}
else
{
return null;
}
}
EDIT
public string Consult(string Key)
{
return _context.HttpContext.Session.GetString(Key);
}
I appreciate any comments.

Identity user after they have logged in .net 4.5

I need some help here, I am trying to identify a user after they have logged in. My code works ok apart from the where clause.
How do you identify a user, I am basically trying to say where UserName == loginName give me the full record.
Then from the record I can pull out the GarageID, any help or pointers much appreciated.
private void FindGarageID()
{
System.Security.Principal.WindowsIdentity identity = Context.Request.LogonUserIdentity;
string loginName = identity.Name;
using (tyrescannerdatabaseEntities dbcontext = new tyrescannerdatabaseEntities())
{
garage = (from r in dbcontext.AspNetUsers
where r.UserName == loginName
select r).FirstOrDefault();
if (!garage.GarageID.Equals(null))
{
garageID = (int)garage.GarageID;
}
else
{
garageID = 1;
}
}
So here is how I would do this. I would create a static class called Session. This just encapsulates the accessing of session variables for me.
public static class Session
{
public static string UserName
{
get { return (JsonWhereClause)HttpContext.Current.Session["UserName"]; }
set { HttpContext.Current.Session["UserName"] = value; }
}
}
Then when the user logs in, I would do this.
Session.UserName = ""//User inputed username
This would then make your code be this.
private void FindGarageID()
{
string loginName = Session.UserName;
using (tyrescannerdatabaseEntities dbcontext = new tyrescannerdatabaseEntities())
{
garage = (from r in dbcontext.AspNetUsers
where r.UserName == loginName
select r).FirstOrDefault();
if (!garage.GarageID.Equals(null))
{
garageID = (int)garage.GarageID;
}
else
{
garageID = 1;
}
}
Note that the Session will only be available on the webserver, so if you have a service you would need to pass the username to the service.

Homework find matching object in a list of objects and access properties of that object

I am trying to create a program that mimics an ATM. In my program, I need to check if the string that a user enters matches the Name property of any objects within a list of objects. If it does not match, then the account is automatically added with some other default values. If it does match, then I need to set the variables that are accessed on another form to the properties of that account object. Additionally, those properties will need to be updated from the other form, so that the object is kept current. I think that I can figure out how to update those properties, but I am having difficulty with trying to set the variables to the current account, more specifically, how to access the properties of the matching account. My class constructor is as follows:
class Account
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
private int acctNum = 0;
public int AcctNumber
{
get
{
return acctNum;
}
set
{
acctNum = value;
}
}
//initialize the CheckBalance value to 100.00
private decimal checkBalance = 100.00M;
public decimal CheckBalance
{
get
{
return checkBalance;
}
set
{
checkBalance = value;
}
}
public Account(string Name)
{
this.Name = Name;
}
private decimal saveBalance = 100.00M;
public decimal SaveBalance
{
get
{
return saveBalance;
}
set
{
saveBalance = value;
}
}
}
This works out just fine, as the only constructor that I need is the Name property, while the other properties are automatically set to a base value. The list and relevant code that I currently have are as follows:
//variable that will be used to check textbox1.Text
string stringToCheck;
//array of class Account
List<Account> accounts= new List<Account>();
public MainMenu()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//set value to user's input
stringToCheck = textBox1.Text;
//set a var that only returns a value if the .Name already exists
var matches = accounts.Where(p => p.Name == stringToCheck);
//check through each element of the array
if (!accounts.Any())
{
accounts.Add(new Account(stringToCheck));
}
else if (matches != null)
//set variables in another form. not sure if these are working
Variables1.selectedAccount = ;
//is this calling the CheckBalance of the instance?
Variables1.selectedCheckBalance = accounts[i].CheckBalance;
//same thing?
Variables1.selectedSaveBalance = accounts[i].SaveBalance;
//switch to form
AccountMenu acctMenu = new AccountMenu();
this.Hide();
acctMenu.Show();
}
In the above code, the "else if (matches != null)" is more of a filler, since I am not sure what to use. Of course, I also need to re-write the portion "if (!accounts.Any())" because once the list is populated with at least one object, this code will never occur again. So, really, I just need to know how to check for a matching account and how to access the properties of that account so that I can set the Variables1 properties to match. Thanks for any help!
If it works for your particular situation, var account = accounts.FirstOrDefault(p => p.Name == stringToCheck) will give you the first account in the collection that matches the expression or null if nothing exists.
check if account != null to ensure you do not get a null reference exception when trying to get property values.
Then, use account.CheckBalance to get the property value for that particular account.
I may not be fully understanding the question and cannot comment because I do not have a 50 reputation : (

How to get object attributes from a session

I have a class named "admin" in my asp.net C# project.
It is:
public class Admin
{
private int ID;
private string FirstName, LastName, IsMainAdmin, Email, Username,
Password,BirthDate, EntryDate;
public int id
{
get { return ID; }
set { ID = value; }
}
public string firstname
{
get { return FirstName; }
set { FirstName = value; }
}
public string lastname
{
get { return LastName; }
set { LastName = value; }
}
.
.
.
After login a session is created like this:
Admin admin = isAdmin(username, password);
if (admin != null)
{
**Session.Add("maskanAdmin", admin);**
Response.Redirect("panel.aspx");
}
In other page i need to get admin's ID from session in code behind section after page request via jquery ajax.
Please notice that my code behind Method is [WebMethod] that is not supporting Session Object.
Can i get it? How?
var adminObj = (Admin)Session["maskanAdmin"];
if(adminObj != null)
{
var id = adminObj.id;
var fname = adminObj.firstname;
}
Read more about Read Values from Session State
Update
I am not sure why the question is updated after one hour saying you are using the code in web methods.
However, have a look at Using ASP.NET Session State in a Web Service
You just need to cast it back to an Admin type object when you retrieve it from the Session:
Admin admin = (Admin)Session["maskanAdmin"];
Then you can use the properties of the object as normal:
if(admin.ID == someOtherID)
{
// do stuff
}
Admin variableName = (Admin)Session["maskanAdmin"];
var adminObj = Session["maskanAdmin"];
if(adminObj != null)
{
var admin = (Admin)adminObj;
}

C# store login session in session class

i want to store the login information {id,bagian} so i created Session.cs class.
here is the Session.cs code
class Session
{
public Session ()
{
}
public int idnya { get; set; }
public string bagiannya { get; set; }
public void saveSession(int id, string bagian)
{
idnya = id;
bagiannya = bagian;
}
public void destroySession()
{
idnya = 0;
bagiannya = "";
}
}
so the id will be generated automatically in the following form. however, why does the id return 0 ?
here is my Tambah constructor
public Tambah()
{
InitializeComponent();
textBox2.Text = session.idnya.ToString();
}
here is my Login code. iam using saveSession() method to store the id and bagian into Session.cs class
int nomornya = int.Parse(textBox1.Text);
string passwordnya = textBox2.Text;
string bagiannya = comboBox1.Text;
var data = from a in de.karyawan
where a.nomor_karyawan == nomornya &&
a.password == passwordnya &&
a.bagian == bagiannya
select a;
if (data.Any())
{
if (bagiannya.Equals("Admin"))
{
cmd.cetakSukses("Login sebagai admin", "Login");
loginAdmin();
}
else
{
cmd.cetakSukses("Login sebagai teller", "Login");
loginTeller();
}
main.Show();
this.Hide();
session.saveSession(nomornya, bagiannya);
//MessageBox.Show(session.idnya.ToString());
}
else
{
cmd.cetakGagal("Username atau password salah", "Login");
}
when i call the idnya and bagiannya value, they show the expected values. but, it went wrong when i call the Tambah form.
how to resolve this ?
or is there any alternative way without generating Session class manually ?
any help will be apprciated. thanks !
From the picture and the fact that you suggest that the code doesn't crash I guess that "textBox1" is the second TextBox control in the picture. This control contains text "0", so after you parse this text into an integer it will give you 0. After that, you don't modify this variable ("nomornya" whatever this means), so how do you expect it no to be 0? Besides all this, your question is cluttered and unclear.
EDIT: it is still unclear what do you expect to happen and why. What's the scenario? If the ID comes from the user, how is anyone suppose to guess what are you typing into the textBox1 what produces the unwanted results? When does the "id return 0" as you've stated?

Categories

Resources