Last post Mar 14, 2012 10:02 PM by Decker Dong - MSFT
Mar 13, 2012 07:32 PM|thsman|LINK
I'm sure I can work around this but it is a mystery. I have a page with a muliview control. At the top of the page are buttons that set the views. This code is from one of those buttons.
adsPriceCreation.SelectCommand = "UPDATE Prices INNER JOIN Prices_Default_table ON Prices_Default_table.id = Prices.id SET Prices.pd1 = [Prices_Default_table].[pd1], Prices.pd2 = [Prices_Default_table].[pd2], Prices.pd3 = [Prices_Default_table].[pd3], Prices.pd4 = [Prices_Default_table].[pd4], Prices.pd5 = [Prices_Default_table].[pd5], Prices.pd6 = [Prices_Default_table].[pd6], Prices.pd7 = [Prices_Default_table].[pd7], Prices.overnight = [Prices_Default_table].[overnight] WHERE Prices.price_index = 0;"
adsPriceCreation.SelectCommand = "SELECT [id], [item_type], [type], [pd1], [pd2], [pd3], [pd4], [pd5], [pd6], [pd7], [overnight], [startdate], [enddate], [prod_id] FROM [Prices] WHERE ([active] = ?)"
Two lines of code both of which work.... but
If the second is commented out the first one works. If none are commented out only the second works.
Try it yourself. I'm using an Accessdatasource with an mdb.
Mar 13, 2012 07:35 PM|Careed|LINK
That's because they're both setting the SelectCommand property. The first one needs to be the UpdateCommand property.
Mar 13, 2012 07:53 PM|thsman|LINK
It is not my intention to change the update command but merely to execute this one whenever this button is clicked. It resets the gridview to the default values I want the user to see.
Changing it to an updatecommand does not run the command during this event.
I have a nasty feeling I'll end up looking like a donkey here.
Mar 13, 2012 08:04 PM|MetalAsp.Net|LINK
Mar 13, 2012 08:36 PM|thsman|LINK
That makes sense but isn't backed by my experience in other situations. In many scenarios I set the selectcommand and it immediately executes. Here I have now taken the second line and put it under a button the user will click. The first button now executes
the update statement. The second one also fires correctly when the button is clicked. So what does determine when the command executes?
Mar 14, 2012 11:40 AM|Careed|LINK
In your code, you are essentially setting/defining the SelectCommand twice. Thus, the second version will be only available as you are overwriting the first version. Along with what MetalAsp.Net stated, you still need an action to make this happen because
all you're really doing in setting the same property twice.
You could create a stored procedure in your database that will execute both the UPDATE and SELECT in sequence and set the SelectCommand to the name of that stored procedure.
Mar 14, 2012 12:15 PM|thsman|LINK
I have solved my development problem by putting those two lines in different places. I totally get the idea that I am setting the value of the selectcommand each time. What I don't get is why when I do nothing more than set that value everywhere else in
code the data source is triggered to execute the select command. So what is it that fires that?
Mar 14, 2012 10:02 PM|Decker Dong - MSFT|LINK
What I don't get is why when I do nothing more than set that value everywhere else in code the data source is triggered to execute the select command. So what is it that fires that?
If you are setting the value to the same instance of a class duplicatedly，obviously it will only get the LATEST one as the answer。
And you have to use two or more SqlDataSource for different kinds of instances and dynamically setting them as the sample and solution：）