2011-01-19 7 views
0

みなさん、こんにちは:は、写真をアップロードすることはできません:私は日のために、このエラーメッセージを表示して苦労してきたとNullReferenceException

ここでは、エラーメッセージです:

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 7: 
Line 8:  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
Line 9:   UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString() 
Line 10:   cannotUploadImageMessage.Visible = False 
Line 11:  End Sub 

Source File: C:\Users\Collins\Documents\Visual Studio 2005\WebSites\living to please god world\PhotoAdmin\Default.aspx.vb Line: 9 

Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.] 
    PhotoAdmin_Default.Page_Load(Object sender, EventArgs e) in C:\Users\Collins\Documents\Visual Studio 2005\WebSites\living to please god world\PhotoAdmin\Default.aspx.vb:9 
    System.Web.UI.Control.OnLoad(EventArgs e) +99 
    System.Web.UI.Control.LoadRecursive() +50 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627 

これは、写真をアップロードするために、私の完全なコードです。誰かが私を助けることができますか?

Imports System.Data 
Imports System.IO 
Imports System.Data.SqlClient 

Partial Class PhotoAdmin_Default 
    Inherits System.Web.UI.Page 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString() 
     cannotUploadImageMessage.Visible = False 
    End Sub 

    Protected Sub dvPictureInsert_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvPictureInsert.ItemInserted 
     'If the record was successfully inserted, save the picture 
     If e.AffectedRows > 0 Then 
      'Determine the maximum pictureID for this user 
      Dim results As DataView = CType(maxPictureIDDataSource.Select(DataSourceSelectArguments.Empty), DataView) 
      Dim pictureIDJustAdded As Integer = CType(results(0)(0), Integer) 
      'Reference the FileUpload control 
      Dim imageUpload As FileUpload = CType(dvPictureInsert.FindControl("imageUpload"), FileUpload) 
      If imageUpload.HasFile Then 
       Dim baseDirectory As String = Server.MapPath("~/UploadedImages/") 
       imageUpload.SaveAs(baseDirectory & pictureIDJustAdded & ".jpg") 
      End If 
     End If 
     If e.Exception Is Nothing Then 
      ' Use the AffectedRows property to determine whether the 
      ' record was inserted. Sometimes an error might occur that 
      ' does not raise an exception, but prevents the insert 
      ' operation from completing. 
      If e.AffectedRows = 1 Then 
       MessageLabel.Text = "Record inserted successfully." 
      Else 
       MessageLabel.Text = "An error occurred during the insert operation." 
       ' Use the KeepInInsertMode property to remain in insert mode 
       ' when an error occurs during the insert operation. 
       e.KeepInInsertMode = True 
      End If 
     Else 
      ' Insert the code to handle the exception. 
      MessageLabel.Text = e.Exception.Message 
      ' Use the ExceptionHandled property to indicate that the 
      ' exception has already been handled. 
      e.ExceptionHandled = True 
      e.KeepInInsertMode = True 
     End If 
    End Sub 


    Protected Sub dvPictureInsert_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles dvPictureInsert.ItemInserting 
     Dim cancelInsert As Boolean = False 

     Dim imageUpload As FileUpload =CType(dvPictureInsert.FindControl("imageUpload"), FileUpload) 
     If Not imageUpload.HasFile Then 
      cancelInsert = True 

      Dim acceptedExtensions = New String() {".jpg", ".png", ".gif"} 
      If Not acceptedExtensions.Contains(imageUpload.FileName, StringComparer.OrdinalIgnoreCase) Then 
       cancelInsert = True 'Invalid image file! 
      End If 

     Else 
      Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(imageUpload.PostedFile.InputStream) 
      If image.Width > 1300 Or image.Height > 950 Then 
       cancelInsert = True 
      End If 
     End If 

     If cancelInsert Then 
      e.Cancel = True 
      cannotUploadImageMessage.Visible = True 
     End If 
     'Set the UserId value to the currently logged on user's ID 
     e.Values("UserId") = Membership.GetUser().ProviderUserKey 
     'Set the UploadedOn value to the current date/time 
     e.Values("UploadedOn") = DateTime.Now 
    End Sub 
    Protected Sub gvPictures_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles gvPictures.RowDeleted 
     Dim baseDirectory As String = Server.MapPath("~/UploadedImages/") 
     Dim fileName As String = baseDirectory & e.Keys("PictureID") & ".jpg" 
     File.Delete(fileName) 
    End Sub 
    Protected Sub gvPictures_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvPictures.RowUpdating 
     e.NewValues("UserId") = Membership.GetUser().ProviderUserKey 
    End Sub 


End Class 
+0

「NullReferenceException」のほとんどすべてのケースが同じです。いくつかのヒントについては、「[.NETのNullReferenceExceptionは何ですか?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」を参照してください。 –

答えて

1

実際にMembership.GetUser()さんがユーザーを返していますか?

あなたは認証されており、匿名ユーザーではありません。ヌルチェックまたはチェックを追加して、最初に認証されていることを確認する必要があります。私はあなたが使用することができると信じてUser.Identity.IsAuthenticated

+0

はいmemebership.GetUserは実際にユーザーを返しています。 – onfire4JesusCollins

+0

@onfireでは、ProviderUserKeyはどうですか? – Brandon

+0

私は、asp.netを初めて妖精です。 Scott Mitchellsの本から写真のアルバムを作成する方法を知りました – onfire4JesusCollins

0

ちょうど私が行9がメンバーシップとコントロールと関係している参照してください。腸の本能は、あなたがログインしていない、またはコントロールが存在しないということです。

9行目にブレークポイントを置き、デバッグでサイトを実行し、Membership.GetUser()またはUserIdValueがヌルかどうかを確認します。

関連する問題