0

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

        }
    }

Edited by __avd: Added [code] tags.

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by naraza
0

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.

Edited by __avd: correct [code] tags.

0

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

Edited by __avd: Added [code] tags.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.