null引用异常

时间:2011-01-19 07:41:42

标签: c# asp.net

我收到系统空引用异常错误,显示如下:alt text

我正在对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>";
            }

        }

    }

正在下面运行的页面图像 alt text

我将验证项目应用于所有下拉列表,除了带有浏览按钮的那个(其中offcourse不是下拉项目)。很明显,根据我的知识,我没有遗漏任何领域。那可能是什么原因?

2 个答案:

答案 0 :(得分:0)

似乎在至少一个下拉列表中您没有选择任何内容,导致SelectedItem属性返回null。在尝试访问该属性Text属性之前,请更改您的代码以检查该属性是否为null。

答案 1 :(得分:0)

调试您的代码,您将看到哪个对象或属性为null。检查下拉列表是否已选择项目的最简单方法是检查SelectedIndex属性,该属性应为&gt; -1