Friday, July 18, 2008

Problem: Button In Datalist Has To Be Clicked Twice To Run

OK. For the past couple of days I have been working on a website for a client. I added an Update Panel to the page along with a datalist control with many buttons inside of it. Most of the buttons worked perfectly on the first click. However, I had a few stubborn buttons that were making me click on them twice before they would fire the ItemCommand event of the datalist. Just so you know, I bubble the events of all the buttons up to the ItemCommand event of the datalist using the CommandName and CommandArgument properties of the buttons.

In this case, these were all set correctly. So why did I still have to click twice on the button to make the ItemCommand event execute? I tried everything to figure out the answer. I switched events. I put the code into the Command event of the button - still had to click twice. I grabbed the index of the selected item from e.Item.ItemIndex in the ItemCommand event and did a rebinding of the datalist control in that event and then set the index back to the selected index - still had to click the button twice. I changed the type of button from a regular button to a link button - still had to click the button twice. I changed where I was binding the controls. Initially, I was checking if the page was a postback. If it wasn't a postback, I did a bind to set the datalist up. I removed the IsPostBack and just started binding all the time to see if that would help - still had to click those darn buttons twice!!!! I put the code in every event of the button and datalist control - still had to click the button twice. I was baffled beyond baffled. You know what? It's funny how we can walk away from something, go get something to eat, go get some rest and then come back and figure out the answer that was sitting right in front of us all along.

After not being able to get the button in the datalist to fire on the first click and trying everything but the right thing to fix it, I just started thinking through the logic. OK. So the user initially clicks on a button to select the item - this button uses the ItemCommand event of the datalist yet it works on the first click. OK.... Then, the user selects a radio button that I extended with a CommandName and CommandArgument property so I could control the events from it with ItemCommand too. The radio button works perfectly on the first click. Now, this button that the radio button enables after being clicked wants to be clicked twice.....why......why.....why??!?!?!?!?! Then it hit me like a ton of bricks. What was controlling the datalist control? AN UPDATE PANEL!!!!!! I had the UpdatePanel Trigger set to AsyncPostBack for the datalist control and the ItemCommand event. I changed the UpdatePanel trigger from AsyncPostBack to just PostBack on the datalist control and guess what boys and girls??????????????????????????????????? I didn't have to click my buttons twice anymore!!!! The button updates now work exactly as they were meant to without requiring an extra click! All is right with the world. Well, maybe not, but at least all is right with the buttons on my datalist control. They now respond to the single click they were meant to respond to. Happy Friday to me!!

Problem:
Buttons on a Datalist control required two clicks instead of one
(in case you are searching for the answer to button in datalist clicked twice, two clicks on button in datalist control, button in datalist not firing ItemCommand event)

Solution:
Change your UpdatePanel trigger on the datalist control to PostBack from AsyncPostBack. Your buttons should now the ItemCommand event when they are clicked once.

Note: This may not be the solution in all cases. If you aren't using an update panel, I would suggest making sure that you check where you are your datalist control. After removing the Updatepanel and testing this issue through, I noted that changing when I bind the datalist would also correct the issue.
Smooches!

Monday, June 23, 2008

System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints

System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints

Don't you just love messages like that when you are programming!! I sure as hell do! Whats not to love about an obscure message that has nothing to do with the problem you are experiencing? Recently, after adding a column to a stored procedure I created, my application started giving me this wonderfully helpful error message. First, I went to my stored procedure to see what was coming back from the database. Everything was coming back just fine. Then, I went to my dataset in Visual Studio and....everything looked fine!! Darn!!! Wasn't everything just working a minute before I added that line to my stored proc???

I went back to my dataset in Visual Studio and looked through it once more. The answer to this stupid error message came jumping out at me. I had changed the stored proc in SQL Server and then right clicked on the table adapter in the dataset and selected Configure to add the column. You have to use Configure in order to get the dataset to recognize the column you just added to the stored proc. The column was added perfectly, except there was ONE little detail that was wrong - the max length of the column had defaulted to 10. Changing this stopped the error!!!!

Here is a screenshot of the item that must be changed to stop this stupid error.





I love you Microsoft, but darn! Can't you give me accurate messages when there is something wrong? Something that says that I violated a primary key constraint is a lot different from needing to change the MaxLength of a column.....or at least I think it's different :-) (ha,ha...don't send me a message! I'm being sarcastic here!)

I don't know...maybe I'm just stupid!! :-)


Problem:

You add a new column to a stored proc or sql statement and when the data is pulled using the fill method of a strongly typed dataset you get an error message about a constraint issue.

Error Message:
System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints

Solution:
Change the MaxLength value of the column you just added.