C# 名称';currentUserId';在当前上下文中不存在
我试图从ASP.NET C将数据插入SQL Server,但得到错误代码: 当前上下文中不存在名称“currentUserId” 我在受保护的bankinfo上收到此错误。如何声明C# 名称';currentUserId';在当前上下文中不存在,c#,asp.net,C#,Asp.net,我试图从ASP.NET C将数据插入SQL Server,但得到错误代码: 当前上下文中不存在名称“currentUserId” 我在受保护的bankinfo上收到此错误。如何声明currentuserid,以便可以在整个页面中访问它。当我运行我的页面时,我得到上面的错误消息 public bool InsertRegistration() { // Determine the currently logged on user's UserId MembershipUser c
currentuserid
,以便可以在整个页面中访问它。当我运行我的页面时,我得到上面的错误消息
public bool InsertRegistration()
{
// Determine the currently logged on user's UserId
MembershipUser currentUser = Membership.GetUser();
Guid currentUserId = (Guid) currentUser.ProviderUserKey;
//Start of Upload 1
string filename1 = Path.GetFileName(AdmissionUpload.PostedFile.FileName);
string contentType1 = AdmissionUpload.PostedFile.ContentType;
using (Stream fs1 = AdmissionUpload.PostedFile.InputStream)
{
using (BinaryReader br1 = new BinaryReader(fs1))
{
byte[] bytes1 = br1.ReadBytes((Int32) fs1.Length);
string filename2 = Path.GetFileName(StudentIDUpload.PostedFile.FileName);
string contentType2 = StudentIDUpload.PostedFile.ContentType;
using (Stream fs2 = StudentIDUpload.PostedFile.InputStream)
{
using (BinaryReader br2 = new BinaryReader(fs2))
{
byte[] bytes2 = br2.ReadBytes((Int32) fs2.Length);
string filename3 = Path.GetFileName(TranscriptUpload.PostedFile.FileName);
string contentType3 = TranscriptUpload.PostedFile.ContentType;
using (Stream fs3 = TranscriptUpload.PostedFile.InputStream)
{
using (BinaryReader br3 = new BinaryReader(fs3))
{
byte[] bytes3 = br3.ReadBytes((Int32) fs3.Length);
string filename4 = Path.GetFileName(PassportUpload.PostedFile.FileName);
string contentType4 = PassportUpload.PostedFile.ContentType;
using (Stream fs4 = PassportUpload.PostedFile.InputStream)
{
using (BinaryReader br4 = new BinaryReader(fs4))
{
byte[] bytes4 = br4.ReadBytes((Int32) fs4.Length);
//SqlDateTime sqldatenull;
SqlCommand com = new SqlCommand(
"INSERT INTO DocInfoNationalCandidates2020edited(AdmissionLetter, AdmissionLetterFileName, AdmissionImageType, StudentID, StudentIDFileName, StudentImageType, Transcript, TranscriptFileName, TranscriptImageType, Passport, PassportFileName, PassportImageType, UserId) OUTPUT INSERTED.ApplicationNo VALUES (@AdmissionLetter, @AdmissionLetterFileName, @AdmissionImageType, @StudentID, @StudentIDFileName, @StudentImageType, @Transcript, @TranscriptFileName, @TranscriptImageType, @Passport, @PassportFileName, @PassportImageType, @UserId)",
con);
com.Parameters.AddWithValue("@AdmissionLetter", bytes1).Value =
System.Data.SqlTypes.SqlBinary.Null;
com.Parameters.AddWithValue("@AdmissionLetterFileName", filename1);
com.Parameters.AddWithValue("@AdmissionImageType", contentType1);
com.Parameters.AddWithValue("@StudentID", bytes2).Value =
System.Data.SqlTypes.SqlBinary.Null;
com.Parameters.AddWithValue("@StudentIDFileName", filename2);
com.Parameters.AddWithValue("@StudentImageType", contentType2);
com.Parameters.AddWithValue("@Transcript", bytes3).Value =
System.Data.SqlTypes.SqlBinary.Null;
com.Parameters.AddWithValue("@TranscriptFileName", filename3);
com.Parameters.AddWithValue("@TranscriptImageType", contentType2);
com.Parameters.AddWithValue("@Passport", bytes4).Value =
System.Data.SqlTypes.SqlBinary.Null;
com.Parameters.AddWithValue("@PassportFileName", filename4);
com.Parameters.AddWithValue("@PassportImageType", contentType4);
com.Parameters.AddWithValue("@UserId", currentUserId);
con.Open();
// open connection here, just before executing
// return the true/false for whether a row was inserted
int rows = com.ExecuteNonQuery();
if (rows > 0)
{
return true;
}
else
{
return false;
}
}
}
}
}
}
}
}
}
}
protected void btnbankinfosub_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand com =
new SqlCommand(
"INSERT INTO BankInfoNationalCandidates2020(Bank, AccountNo, SortCode, UserId) VALUES (@Bank, @AccountNo, @SortCode, @UserId )",con);
com.Parameters.AddWithValue("@Bank", ddlbankname.SelectedItem.Text);
com.Parameters.AddWithValue("@AccountNo", txtaccno.Text);
com.Parameters.AddWithValue("@SortCode", txtsortcode.Text);
com.Parameters.AddWithValue("@UserId", currentUserId);
com.ExecuteNonQuery();
lblbankinfo.Visible = true;
lblbankinfo.Text = "Saved successfully!";
lblbankinfo.ForeColor = System.Drawing.Color.White;
con.Close();
}
错误是因为您试图在btnbankinfosub_Click中使用currentUserId,但您仅在InsertRegistration中声明它 简单的解决方法是只需将这些行复制到btnbankinfosub_的顶部,然后单击:
// Determine the currently logged on user's UserId
MembershipUser currentUser = Membership.GetUser();
Guid currentUserId = (Guid) currentUser.ProviderUserKey;
但是,您也可以将它们重构为函数或属性,例如
private Guid CurrentUserId {
get {
MembershipUser currentUser = Membership.GetUser();
return (Guid) currentUser.ProviderUserKey;
}
}
然后页面上的所有代码都可以使用CurrentUserId
(这就是说,每次读取属性时都会运行此代码;如果计算成本相对较高,那么您可能希望确保在该控制器的生命周期内对其进行一次计算,然后缓存一次,但我认为在这种情况下,从成员身份获取数据很快,因此不值得在这里担心。)非常感谢@Rup。第一个选择成功了!!!!