how can i use to show the selected value from data set i think there is a problem of dataset use to take out value from data set

my code is

protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataAdapter adap = new SqlDataAdapter("SP_selectallbooks", con);

            adap.SelectCommand.CommandType = CommandType.StoredProcedure;

            //DataSet ds = new DataSet();
            adap.Fill(ds);
            ListBox1.DataSource = ds;
            ListBox1.DataTextField = "Books";
            ListBox1.DataValueField = "ID";
            ListBox1.DataBind();
           
            
            
            SqlDataAdapter adap1 = new SqlDataAdapter("SP_SelectAllStudent", con);

            adap1.SelectCommand.CommandType = CommandType.StoredProcedure;

            //DataSet ds = new DataSet();
            adap1.Fill(ds);
            ListBox2.DataSource = ds;
            ListBox2.DataTextField = "Firstname";
            ListBox2.DataValueField = "ID";
            ListBox2.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
           
        }

        protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }

Dani AI

Generated

Short answer: the NullReference comes from reading SelectedItem.Text when SelectedItem is null — most often because the list was rebound on postback (losing the selection) or because nothing is actually selected. was on the right track: bind once and use a per-list DataTable (or set DataMember when using a DataSet). Below are focused checks and small examples to apply immediately.

Checklist (do these first)

  • Only bind your lists when the page is first loaded. Rebinding on every postback clears selections.
  • Use a separate DataTable per ListBox (or explicitly pick the table from your DataSet). Reusing a single DataSet without DataMember makes the source ambiguous.
  • Verify the stored procedures return the exact column names you use for DataTextField/DataValueField.
  • In the button handler, always check SelectedIndex >= 0 (or SelectedItem != null) before using SelectedItem.Text.
  • If you expect selection changes to fire immediately, set ListBox2.AutoPostBack = true and handle SelectedIndexChanged.

Example patterns (adapt names to your controls and DB)

Page load bind (only once)

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindListBox(ListBox1, "SP_selectallbooks", "Title", "BookID");
        BindListBox(ListBox2, "SP_SelectAllStudent", "Firstname", "StudentID");
    }
}

Safe button handler

protected void Button2_Click(object sender, EventArgs e)
{
    if (ListBox2.SelectedIndex >= 0)
        Label1.Text = ListBox2.SelectedItem.Text;
    else
        Label1.Text = "Please select a student.";
}

Bind helper (keeps code DRY)

private void BindListBox(ListBox box, string storedProc, string textField, string valueField)
{
    var table = new System.Data.DataTable();
    using(var cmd = new System.Data.SqlClient.SqlCommand(storedProc, yourConnection))
    {
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        using(var da = new System.Data.SqlClient.SqlDataAdapter(cmd)) da.Fill(table);
    }
    box.DataSource = table;
    box.DataTextField = textField;
    box.DataValueField = valueField;
    box.DataBind();
}

Quick debug tips

  • Put a breakpoint in Button2_Click and inspect ListBox2.Items.Count, SelectedIndex, and SelectedItem.
  • If items exist but SelectedIndex is -1 after postback, you’re rebinding on postback or ViewState is off.

Following these steps will fix the NullReference and make selection handling reliable.

Recommended Answers

All 3 Replies

Welcome to the Daniweb.

You need to set DataMember property with the name of table when the Datasource is DataSet . You can create/populate/assign the DataTable instance to the ListBoxes. See the below code.

protected void Page_Load(object sender, EventArgs e)
        {

       if(!IsPostBack) 
         {
            SqlDataAdapter adap = new SqlDataAdapter("SP_selectallbooks", con);

            adap.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataTable dt1=new DataTable();
            adap.Fill(dt1);
            ListBox1.DataSource = ds1;
            ListBox1.DataTextField = "Books";
            ListBox1.DataValueField = "ID";
            ListBox1.DataBind();
           
            
            
            SqlDataAdapter adap1 = new SqlDataAdapter("SP_SelectAllStudent", con);

            adap1.SelectCommand.CommandType = CommandType.StoredProcedure;

            DataTable dt2=new DataTable();
            adap1.Fill(dt2);
            ListBox2.DataSource = dt2;
            ListBox2.DataTextField = "Firstname";
            ListBox2.DataValueField = "ID";
            ListBox2.DataBind();
          }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
           
        }

        protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }

PS: Use [code] .. [/code] tag to wrap up programming statements in the post.

it givew error of null exception it dosent shoe the selected value in label

protected void Button2_Click(object sender, EventArgs e)
        {
  Label1.Text = ListBox2.SelectedItem.Text;
        }

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.