1

when i run my code than it send the error that Object reference not set to an instance of an object in dropdown list 4.

Dropdown Selection error

Object reference not set to an instance of an object.

When i select the item from dropdown list

thanks in advance kindly help plz

here is my code behnd

 protected void LoadOptions4()
            {
                DropDownList4.Items.Clear();
               // DropDownList4 dr = new DropDownList4();


                DataTable CardCode = new DataTable();
                // string name2;
               // string id, name, newName;

                SqlConnection connection = new SqlConnection("Data Source=1.2.3.4;Initial Catalog=TestData;Persist Security Info=True;User ID=abcd;Password=abcd12345");
                using (connection)
                {
                    // string selected4 = DropDownList3.SelectedItem.Value;
                    //SqlCommand theCommand = new SqlCommand("SELECT T1.CardCode , T1.CardName, T2.OpprId, T1.CntctPrsn,T2.CprCode,T3.CntctCode,T3.Name  FROM OCRD T1 left  join OOPR T2 on T1.CardCode=T2.CardCode left join OCPR T3 on T2.CprCode=T3.CntctCode where T1.CardCode=@CardCode ", connection);

                    SqlCommand theCommand = new SqlCommand("select SlpCode,SlpName from OSLP where SlpCode=@SlpCode  and SlpCode<>-1 ", connection);

                    //  SqlDataAdapter adapter = new SqlDataAdapter("SELECT T1.CardCode , T1.CardName, T2.OpprId,T1.CntctPrsn, T2.CprCode,T2.MaxSumLoc  FROM OCRD T1 left  join OOPR T2 on T1.CardCode=T2.CardCode where T1.CardCode=@selected", connection);

                    //   SqlDataAdapter adapter = new SqlDataAdapter("SELECT T1.CardCode , T1.CardName, T2.OpprId,T1.CntctPrsn, T2.CprCode,T2.MaxSumLoc  FROM OCRD T1 left  join OOPR T2 on T1.CardCode=T2.CardCode where T1.CardCode=@selected", connection);
                    string selected5;

                     selected5 = DropDownList4.SelectedItem.Value;

                   theCommand.Parameters.AddWithValue("@SlpCode", selected5);
                   SqlDataAdapter adapter = new SqlDataAdapter(theCommand);
                    //DataSet ds = new DataSet();
                    adapter.Fill(CardCode);
                //name2 = id;

                    if (CardCode.Rows.Count > 0)
                    {
                        for (int i = 0; i < CardCode.Rows.Count; i++)
                        {

                            string name3 = CardCode.Rows[i]["SlpName"].ToString();
                            string slpCode = CardCode.Rows[i]["SlpCode"].ToString();
                            // string newName2 = contcode + " ---- " + name2;

                            DropDownList3.Items.Add(new ListItem(name3, slpCode));


     } }}
    }
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
  • 1
    put code for fill dropdown4 ? – Kaushik Maheta Dec 01 '15 at 05:26
  • 1
    It because you are clearing the DropDownList and then accessing the value of the SelectedItem. I think you mean to clear the DropDownList3 instead of DropDownList4. – mbm Dec 01 '15 at 05:28
  • 1
    @Steve That's not a duplicate. Your link explains *what* null reference exception is. It can be caused N no of time under N no of different scenarios. Like it happened with OP's code. – Nikhil Vartak Dec 01 '15 at 05:31
  • 1
    @nvartak the problem with Null Reference Exception is it's very difficult to debug from text without physically stepping through the code. Generally it's out of scope for SO to answer. – Steve Dec 01 '15 at 05:41

3 Answers3

4

As per the code above, you were clearing the Items from dropdownlist4 as below

DropDownList4.Items.Clear();

Then after few lines of code, getting the selected value of dropdownlist

selected5 = DropDownList4.SelectedItem.Value;

The above line is causing the issue. There are no items to select, but you were selecting an item. Please remove Items.Clear() and check once.

T.S.
  • 18,195
  • 11
  • 58
  • 78
mgopi
  • 59
  • 1
  • 4
0
 DropDownList4.Items.Clear();  remove this line from the top ,  

as in the first line you are clearing the data in DropDownList4 and you are tying to call it in

selected5 = DropDownList4.SelectedItem.Value; 

so try to the DropDownList4 with its datasource on function load,

Akhil R J
  • 184
  • 2
  • 14
0

Here the function is named as LoadOptions4() and the DropDownList3 is populating. this make no sense. according to the snippet you have given, you are populating the DropDownList3 so you have to clear the DropDownList3 instead for DropDownList4.

So your method signature will be like this:

 protected void LoadOptions3()
  {
     DropDownList3.Items.Clear();
     string selected5;
     selected5 = DropDownList4.SelectedItem.Value;
     //perform db operations
     //loop through values
     DropDownList3.Items.Add(new ListItem(name3, slpCode));
 }
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88