1

I am inserting values in listbox from database & retrieving text on a button click. but I'm getting the following error

Object reference not set to instance of object

so "selecteditem.text" does not get any value on item selected...

String selectemail = "select email_id from [property].[dbo].[user_membership]";
SqlCommand cmd = new SqlCommand(selectemail, con);
cmd.Connection.Open();

ListBox1.DataSource = cmd.ExecuteReader();
ListBox1.DataTextField = "Email_ID"; 
ListBox1.DataBind();  

//on button click//       
protected void Button1_Click1(object sender, EventArgs e)
{
   ListItem item = new ListItem();
   item.Text = ListBox1.SelectedItem.Text;(error comes here)
   ListBox2.Items.Add(item.Text);
   ListBox1.Items.Remove(item.Text);
   ...
}

2 Answers 2

3

This will stop the error for you:

//on button click//       
protected void Button1_Click1(object sender, EventArgs e)
{
        if (ListBox1.SelectedItem == null) return;

        ListItem item = new ListItem();
        item.Text = ListBox1.SelectedItem.Text;(error comes here)
        ListBox2.Items.Add(item.Text);
        ListBox1.Items.Remove(item.Text);
}

It looks like it was just a problem of the user not selecting anything in your ListBox1.

EDIT


I threw a test app together to check, and this works fine for me:

    var dt = New DataTable()
    dt.Columns.Add("email_id");

    dt.Rows.Add("first");
    dt.Rows.Add("second");
    dt.Rows.Add("thrid");
    dt.Rows.Add("fourth");

    var lst = New System.Web.UI.WebControls.ListBox;

    lst.DataSource = dt;
    lst.DataTextField = "Email_ID";
    lst.DataBind();

    //lst.SelectedItem is null here
    lst.SelectedIndex = 1;

    //lst.SelectedItem is NOT null here
Sign up to request clarification or add additional context in comments.

4 Comments

ListBox1.SelectedItem.Text is not getting value if data is inserted from database. but getting value if inserted manually..
+1 exactly - just basic "defensive programming" principles - before accessing the properties of an object, check for NOT NULL
I am checking null as: if (ListBox1.SelectedItem == null) return;
but how to selecttext from listbox
1

Debug the code, but it's more than likely that the object that doesn't exist will be the ListBox1 control, or the ListBox1 control doesn't actually have an item selected when the button has been pressed.

4 Comments

When you debug, which is the item that returns null? Place a debug marker and run through your code - is it the ListBox1.SelectedItem?
item.Text = ListBox1.SelectedItem.Text;
ListBox1.SelectedItem.Text is not getting if data is inserted from database. but getting value if insertyed manually...
Your data binding, is it being performed on every postback? Do you have (!IsPostBack) in your code? If so, remove it and try again - make sure the data is always being bound on postback too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.