1

i get a system null reference exception error that displays as follows.alt text

Am performing validation on 4 "dropdownlists". what could i have done wrong?

code behind below.

  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>";
            }

        }

    }

Image of page being run below alt text

I applied validation items to all drop down lists except the one with the browse button(which offcourse is not a dropdown item). clearly according to my knowledge i havent left out any field.so what could be the cause?

Selase
  • 389
  • 4
  • 8
  • 21
  • Well, DropDownListcaseid or its SelectedItem property is null. Nobody can do more than speculate as to why without seeing the code relevant to that object. – Joel Mueller Jan 19 '11 at 07:44
  • How are those drop down lists being created? Do they exist in the `.aspx`? – Shadow The GPT Wizard Jan 19 '11 at 07:51
  • Yes they do..i found the error with a break point. thanks Joel. it was a missing field i had to pick up from session and i did run the page properly to enable that value...Problem fixed now...Good to see you again Shadow...:) – Selase Jan 19 '11 at 08:02
  • Joel didn't see your comment and I saw it only because I *follow* this question. When posting comment on someone else comment use `@` to notify the person, first 3 letters of the name are enough like I did here. – Shadow The GPT Wizard Jan 19 '11 at 08:10

2 Answers2

0

It seems that in at least one dropdownlist you have not selected anything, causing the SelectedItem property to return null. Change your code to check if the property is null before trying to access it's Text property.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
0

Debug Your code and You will see which object or property is null. The simplest way to check if dropdownlist has selected item is to check SelectedIndex property, which should be > -1

zavaz
  • 745
  • 4
  • 13