locked
update table value by ListBox value RRS feed

  • Question

  • User231953124 posted

    Hi all, could someone help me with this..?

    I display items from SqlDatSource in DropDownList. In table apart from these items are values (numbers) for each item.

    In ListBox I display number - which I want to deduct from value in table when I click on Button. For example somethink like in this pseudo code

    Item1 .....  10 pcs  -> button click -> update table myitems  set value = value - ListBox.Value where name =DropDownList.text

    after that Item1 .... 9pcs if in ListBox was chosen 1 and in DropDownList was chosen Item1

    Thanks

    Saturday, December 10, 2011 11:21 AM

Answers

  • User-1516073966 posted

    Hi,

    What I understood from your post is your drop down fields will have both the items and corresponding values binded.

    Same values(i.e above drop down values) binded to ListBox control.

    When you select an item from drop down list and list box and click on button you want to update the table value column by subtracting the value selected from the list box.

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
      string storedProcName = "updateTable";
      SqlConnection con = new SqlConnection(connectionstring);
    con.Open();
     SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure. cmd.Connection = con; cmd.Parameters.AddWithValue("@value", ListBox.SelectedValue); cmd.Parameters.AddWithValue("@selectedItem", DropDownList1.SelectedItem.Text); cmd.ExecuteNonQuery(); //Binding the DropDown after updating. //Write the logic to bind the drop down again after you update the table. } Write the update query in the updateTable stored procedure. update table myitems set value = value - @value where name = @selectedItem

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 10, 2011 12:26 PM
  • User3866881 posted

    Hi, seems quite clear now, but don´t understand, why I should rebind DropDownList after update execution..?

    BTW I´m using standard SqlDataSource.

    If you are using SqlDataSource,Yes——I don't think there's a need for you to re-databind to the ListBox again。But you should call SqlDataSource.Update and set SqlDataSource's Update parameters to deal with the problem——something looks like this following:

    SqlDataSource.UpdateParameters["paraName1"].DefaultValue = value1;

    ………………………………

    SqlDataSource.UpdateParameters["paraNameN"].DefaultValue = valueN;

    SqlDataSource.Update();

    PS:You can never manually bind to your ListBox by setting DataSource when using SqlDataSource bound to it。Because there's always only one DataSource set for the data presentation control。

    Best regaurds!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 11, 2011 8:49 PM

All replies

  • User-1516073966 posted

    Hi,

    What I understood from your post is your drop down fields will have both the items and corresponding values binded.

    Same values(i.e above drop down values) binded to ListBox control.

    When you select an item from drop down list and list box and click on button you want to update the table value column by subtracting the value selected from the list box.

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
      string storedProcName = "updateTable";
      SqlConnection con = new SqlConnection(connectionstring);
    con.Open();
     SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure. cmd.Connection = con; cmd.Parameters.AddWithValue("@value", ListBox.SelectedValue); cmd.Parameters.AddWithValue("@selectedItem", DropDownList1.SelectedItem.Text); cmd.ExecuteNonQuery(); //Binding the DropDown after updating. //Write the logic to bind the drop down again after you update the table. } Write the update query in the updateTable stored procedure. update table myitems set value = value - @value where name = @selectedItem

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 10, 2011 12:26 PM
  • User231953124 posted

    Yes, you understood well.

    Could you be more specific about this

    //Binding the DropDown after updating.

    //Write the logic to bind the drop down again after you update the table.

    Saturday, December 10, 2011 3:31 PM
  • User-1516073966 posted

    Hi,

    In order to achieve this you have to rebind the drop down after you update the table with the subtract operation.

    i.e When you click on Update button use the update query using SqlCommand class. Once the update is done rebind the drop down again.

    What is the data source or table you used to bind the table and can you post the code for it?

    Sunday, December 11, 2011 10:18 AM
  • User231953124 posted

    Hi, seems quite clear now, but don´t understand, why I should rebind DropDownList after update execution..?

    BTW I´m using standard SqlDataSource.

    Sunday, December 11, 2011 7:12 PM
  • User3866881 posted

    Hi, seems quite clear now, but don´t understand, why I should rebind DropDownList after update execution..?

    BTW I´m using standard SqlDataSource.

    If you are using SqlDataSource,Yes——I don't think there's a need for you to re-databind to the ListBox again。But you should call SqlDataSource.Update and set SqlDataSource's Update parameters to deal with the problem——something looks like this following:

    SqlDataSource.UpdateParameters["paraName1"].DefaultValue = value1;

    ………………………………

    SqlDataSource.UpdateParameters["paraNameN"].DefaultValue = valueN;

    SqlDataSource.Update();

    PS:You can never manually bind to your ListBox by setting DataSource when using SqlDataSource bound to it。Because there's always only one DataSource set for the data presentation control。

    Best regaurds!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, December 11, 2011 8:49 PM