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)
        {

        }
    }

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.