2011-01-19 24 views
1

次のように表示されるシステムnull参照例外エラーが発生します。 alt textnull参照例外

4つのドロップダウンリストで検証を実行しています。何が私は間違っていますか?

コードは以下のとおりです。ページの

protected void addExhibitButton_Click(object sender, EventArgs e) 
    { 

     if (Page.IsValid) 
     { 
      DateTime exhibitDate = DateTime.Now; 
      int caseid = Convert.ToInt32(DropDownListcaseid.SelectedItem.Text); 
      string exhibittype = exhibitTypeDropDownList.Text.ToString(); 
      string storedloc = storedLocationDropDownList.Text.ToString(); 
      string offid = DropDownList1.SelectedItem.Text.ToString(); 
      string status = "Pending"; 
      Stream imgStream = exhibitImageFileUpload.PostedFile.InputStream; 
      int imgLen = exhibitImageFileUpload.PostedFile.ContentLength;     
      byte[] imgBinaryData = new byte[imgLen]; 
      int n = imgStream.Read(imgBinaryData,0,imgLen); 
      try 
      { 
       SqlConnection connections = new SqlConnection(strConn); 
       SqlCommand command = new SqlCommand("INSERT INTO Exhibits (CaseID, ExhibitType, ExhibitImage, DateReceived, StoredLocation, InvestigationStatus, OfficerID, SuspectID, InvestigatorID, ManagerID, AdminID) VALUES (@CaseID, @ExhibitType, @ExhibitImage, @DateReceived, @StoredLocation, @InvestigationStatus, @OfficerID, @SuspectID, @InvestigatorID, @ManagerID, @AdminID)", connections); 


       SqlParameter param0 = new SqlParameter("@CaseID", SqlDbType.Int); 
       param0.Value = caseid; 
       command.Parameters.Add(param0); 

       SqlParameter param1 = new SqlParameter("@ExhibitType", SqlDbType.NText); 
       param1.Value = exhibittype; 
       command.Parameters.Add(param1); 

       SqlParameter param2 = new SqlParameter("@ExhibitImage", SqlDbType.Image); 
       param2.Value = imgBinaryData; 
       command.Parameters.Add(param2); 

       SqlParameter param3 = new SqlParameter("@DateReceived", SqlDbType.SmallDateTime); 
       param3.Value = exhibitDate; 
       command.Parameters.Add(param3); 

       SqlParameter param4 = new SqlParameter("@StoredLocation", SqlDbType.NText); 
       param4.Value = storedloc; 
       command.Parameters.Add(param4); 

       SqlParameter param5 = new SqlParameter("@InvestigationStatus", SqlDbType.VarChar, 50); 
       param5.Value = status; 
       command.Parameters.Add(param5); 

       SqlParameter param6 = new SqlParameter("@OfficerID", SqlDbType.NChar, 10); 
       param6.Value = offid; 
       command.Parameters.Add(param6); 

       SqlParameter param7 = new SqlParameter("@SuspectID", SqlDbType.NChar, 10); 
       param7.Value = DBNull.Value; 
       command.Parameters.Add(param7); 

       SqlParameter param8 = new SqlParameter("@InvestigatorID", SqlDbType.NChar, 10); 
       param8.Value = DBNull.Value; 
       command.Parameters.Add(param8); 

       SqlParameter param9 = new SqlParameter("@ManagerID", SqlDbType.NChar, 10); 
       param9.Value = DBNull.Value; 
       command.Parameters.Add(param9); 

       SqlParameter param10 = new SqlParameter("@AdminID", SqlDbType.NChar, 10); 
       param10.Value = adminID; 
       command.Parameters.Add(param10); 

       connections.Open(); 
       int numRowsAffected = command.ExecuteNonQuery(); 
       connections.Close(); 

       if (numRowsAffected != 0) 
       { 

        DropDownListcaseid.ClearSelection(); 
        exhibitTypeDropDownList.Text = null; 
        storedLocationDropDownList.Text = null; 
        DropDownList1.ClearSelection(); 
        messageLabel.Text = "Rows Inserted successfully"; 
        messageLabel.Visible = true; 
       } 
       else 
       { 
        messageLabel.Text = "An error occured while inserting columns."; 
        messageLabel.Visible = true; 

       } 
      } 
      catch (Exception ex) 
      { 
       string script = "<script>alert('" + ex.Message + "');</script>"; 
      } 

     } 

    } 

画像は、私は(offcourseは、ドロップダウン項目ではありません)ブラウズボタンと1以外のすべてのドロップダウンリストに、検証項目を適用 alt text

の下で実行されています。明らかに私の知識によると、私はフィールドを外していません。何が原因である可能性がありますか?

+0

まあ、DropDownListcaseidまたはSelectedItemプロパティがnullです。誰もそのオブジェクトに関連するコードを見ずに理由を推測する以上のことをすることはできません。 –

+0

これらのドロップダウンリストはどのように作成されますか?それらは '.aspx'に存在しますか? –

+0

はい、そうです。ブレークポイントでエラーが見つかりました。ジョエルに感謝します。それはセッションからピックアップしなければならなかったフィールドが欠落していて、その値を有効にするためにページを正しく実行しました...問題は今修正されました...もう一度あなたに会いたいですね... :) – Selase

答えて

0

少なくとも1つのドロップダウンリストでは、SelectedItemプロパティがnullを返す原因となったものを選択していないようです。プロパティにアクセスしようとする前にプロパティがnullであるかどうかを確認するコードを変更してTextプロパティを変更します。

+0

thatsの権利があります。私はセッションから選択しなければならない1つの値が不足していて、欠落していたと思います。ブレークポイントを使用しているかどうかを判断できました。 – Selase

+0

あなたの問題を解決することができたことを聞いてよかった:) –

0

コードをデバッグし、どのオブジェクトまたはプロパティがnullであるかを確認します。ドロップダウンリストがアイテムを選択したかどうかを確認する最も簡単な方法は、SelectedIndexプロパティ(> -1)をチェックすることです。

関連する問題