我正在尝试通过网络方法将用户的帖子存储到访问数据库中。我想存储登录用户的用户名、用户的帖子和发布日期时间。
到目前为止,我可以通过硬编码存储现有的用户帖子。但我想存储任何登录用户的帖子。我被告知我需要获取用户名的用户 ID。
因此,我找到并尝试添加以下代码:
//GetUser() returns current user information
MembershipUser user = Membership.GetUser();
//Returns the UserID and converts to a string
string UserID = user.ProviderUserKey.ToString();
当我尝试使用断点进行调试时,第一个还可以。但对于第二个,VS 2010 表示“对象引用未设置为对象的实例”。我如何解决它?
VS 建议添加“新”,但这不起作用。它还建议捕获 NullReferenceException,但我不知道如何使用他们提供的代码:
public class EHClass
{
void ReadFile(int index)
{
// To run this code, substitute a valid path from your local machine
string path = @"UsersDB_in_App_Data";
System.IO.StreamReader file = new System.IO.StreamReader(path);
char[] buffer = new char[10];
try
{
file.ReadBlock(buffer, index, buffer.Length);
}
catch (System.IO.IOException e)
{
Console.WriteLine("Error reading from {0}. Message = {1}", path, e.Message);
}
finally
{
if (file != null)
{
file.Close();
}
}
// Do something with buffer...
}
}
你能给我建议我需要做什么,或者另一种方法来获取用户名的用户ID吗?