December 3, 2009

Nested Cursors

I was working on a problem at work to find items within a given property, within a given aspect, within a... (you get the point) But what to do in the case that @@fetch_status <> 0 would it break all levels of the nesting? It can if you do it wrong. There are several examples on the web, but here is the best way to conquer this relatively simple query.

First you need to create your variables and cursors. It is recommended to not create your cursors outside of the level you are using them. The best reason is so that you can use LOCAL with your Declare to auto deallocate your cursor. This is a nice feature for efficiency and resource reasons. I used both methods in my query and tested the server impact and saw a noticeable resource difference. The time differences are so minute that there is barely reason to mention it.
Second thing to do is use your fetch at the end of each nest and don't close the cursor till your iteration of the nesting level is completed.
Here is a mock up of a two level nesting. The concept is the same for multiple levels. Hope this helps you in your queries.

/* Global Variables */
Declare @maxlength int
Declare @Desc varchar(50)
Declare @property int
/* Global Cursors */
Declare pCursor cursor
for (select hmy from property where itype = 3 and isnull(binactive,0) = 0)
Open pCursor
Fetch Next FROM pCursor into @property
While @@fetch_status = 0
Begin
/* LOCAL Cursors */
Declare rCursor cursor LOCAL /* <-- Optional: Look at above comments */ For (Select distinct myAttribute2 from MyTable where property = @property) Open rCursor

Fetch Next From rCursor into @Desc
While @@fetch_status = 0
Begin
...
Fetch Next from rCursor into @Desc
END
Close rCursor
/* ALWAYS CLOSE BEFORE DEALLOCATION.
You still have to close with Declare Cursor Local */
Deallocate rCursor /* If you use LOCAL this line is not necessary */
Fetch Next From pCursor into @property
END
Close pCursor
Deallocate pCursor

As always use
Begin Tran
...
Rollback
Commit
in your code while testing.

November 21, 2009

How to set a default item to an datasource generated list in ASP.net

    While creating a site I created a DetailsView to insert and update some item.  While I created a view that allowed inserting and updating, changed a field item to a template view and added a dropdown to pull a list of options from another table within the database.  This was all well and good but I ran into a situation where there were some dropdowns that I would not utilize while adding or inserting items.  The current state would choose the first value of the dropdown which was not selected.  This would  insert the value into the database causing stored values that were not desired.
    The solution was quite simple. Set a default selection to a mull value.  To do this you need to set things up normally and insert two items inside the dropdown tag. 

   1:   Insert  AppendDataBoundItems="true" to the <dropdown...>tag.  This allows you to append listitems to a datasourced list.  </dropdown...>
  2:   Insert
<listitem text="[your default list item]" value="[value]">


   For my problem it was simple I changed the list item to "--Select Item--" and set [value] to NULL.  This would allow me to only insert values for items that I wished and insert a null value to unutilized selections.

You can use the code below to make the same changes to your site.  As this will work for other situations I have only used this on a dropdown with a datasource generated list.

<asp:dropdownlist id="MyDropDownList" AppendDataBoundItems="true" runat="server"></dropdownlist>

<Listitem text="--Please select one--" value=""></listitem>

November 10, 2009

To display a drop-down list while editing or inserting


*** This can be used for editing or inserting.  For inserting change the ItemTemplate section from EditItemTemplate to InsertItemTemplate. ***

From the Data node of the Toolbox, drag a SqlDataSource control onto the page.

Configure the datasource

Right-click the GridView control and select Show Smart Tag. In the GridView Tasks menu, select Edit Columns.

In the Fields dialog box, select City from the Selected fields list box.

Click Convert this field into a TemplateField link.

Click OK to close the Fields dialog box.

Right-click the GridView control and select Show Smart Tag. In the GridView Tasks menu, select Edit Templates.

Select EditItemTemplate in the Display drop-down list.

Right-click the default TextBox control in the template and select Delete to remove it.

From the Standard tab of the Toolbox, drag a DropDownList control onto the template.

Right-click the DropDownList control and select Show Smart Tag. In the DropDownList Tasks menu, select Choose Data Source.

Select SqlDataSource2.

Click OK.

In the DropDownList Tasks menu, choose Edit DataBindings. The SelectedValue property of the DropDownList control is selected in the DataBindings dialog box.

Click the Field Binding radio button and select City for Bound To.

Select the Two-way databinding check box.

Click OK.

Right-click the GridView control and select Show Smart Tag. In the GridView Tasks menu, click End Template Editing.

Security Note 


User input in an ASP.NET Web page can include potentially malicious client script. By default, ASP.NET Web pages validate user input to make sure input does not include script or HTML elements. As long as this validation is enabled, you do not need to explicitly check for script or HTML elements in user input. For more information, see Script Exploits Overview.
* Information derived from the MSDN website: Walkthrough