<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>OOP - There It Is - SqlDataSource</title>
    <link>http://www.vpsw.com/blogbaby/</link>
    <description>A Very Practical Blog</description>
    <language>en-us</language>
    <copyright>Dean Fiala</copyright>
    <lastBuildDate>Thu, 08 Feb 2007 04:20:59 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.1.8102.813</generator>
    <managingEditor>dfiala@vpsw.com</managingEditor>
    <webMaster>dfiala@vpsw.com</webMaster>
    <item>
      <trackback:ping>http://www.vpsw.com/blogbaby/Trackback.aspx?guid=525bb927-f195-40af-8d0f-c160edd00d45</trackback:ping>
      <pingback:server>http://www.vpsw.com/blogbaby/pingback.aspx</pingback:server>
      <pingback:target>http://www.vpsw.com/blogbaby/PermaLink,guid,525bb927-f195-40af-8d0f-c160edd00d45.aspx</pingback:target>
      <dc:creator>Dean</dc:creator>
      <wfw:comment>http://www.vpsw.com/blogbaby/CommentView,guid,525bb927-f195-40af-8d0f-c160edd00d45.aspx</wfw:comment>
      <wfw:commentRss>http://www.vpsw.com/blogbaby/SyndicationService.asmx/GetEntryCommentsRss?guid=525bb927-f195-40af-8d0f-c160edd00d45</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In the <a href="http://www.vpsw.com/blogbaby/PermaLink,guid,33ff2980-8f72-432b-b214-8c0423d76168.aspx">previous
GridView post</a>, we figured out how to create a simple updating GridView without
a lick of code.  But it only updated a value from a TextBox, nothing
too difficult about that.  What would happen if one of the columns needed
a DropDownList for editing purposes?  This is something that required code in
ASP.NET 1.1. The DropDownList needed to be bound in the ItemDataBound event,
and that was only for display purposes.  Updating required even more code. 
Can the declarative code model handle this twist without resulting to actual -- gasp
-- coding?
</p>
        <p>
I'll save the suspense and admit that, in fact, it is possible to create an Editable,
Updating GridView with a DropDownList  without writing any code.  Here
it is.   We'll talk about the interesting bits afterwards.
</p>
        <p>
          <font color="#0000ff" face="Courier New">    &lt;div&gt;<br />
       &lt;asp:GridView ID="GridViewDropDown" runat="server"
DataSourceID="TestSqlSource" AutoGenerateEditButton="True" DataKeyNames="NameID" AutoGenerateColumns="False"
&gt;<br />
       &lt;Columns&gt;<br />
        &lt;asp:BoundField HeaderText="Name" DataField="Name" 
/&gt;</font>
        </p>
        <p>
          <font color="#0000ff" face="Courier New">          
&lt;asp:TemplateField HeaderText="Popularity Trend"&gt;<br />
              
&lt;EditItemTemplate&gt;<br />
                  
&lt;asp:DropDownList ID="PopularityTrendDropDown" runat="server"   SelectedValue='&lt;%#
Bind("PopularityID") %&gt;' DataSourceID="PopularitySqlSource" DataTextField="Description" 
DataValueField="PopularityID" &gt;&lt;/asp:DropDownList&gt;<br />
                                     
<br />
              
&lt;/EditItemTemplate&gt;<br />
              
&lt;ItemTemplate&gt;<br />
                  
&lt;asp:Label ID="PopularityTrendLabel" runat="server" Text='&lt;%# Eval("Description")
%&gt;'&gt;&lt;/asp:Label&gt;<br />
              
&lt;/ItemTemplate&gt;<br />
           &lt;/asp:TemplateField&gt;<br />
    
<br />
       &lt;/Columns&gt;<br />
        &lt;/asp:GridView&gt;<br />
        &lt;asp:SqlDataSource ID="TestSqlSource"
runat="server" ConnectionString="&lt;%$ ConnectionStrings:TestConnectionString %&gt;"
SelectCommand="SELECT Name, NameID, PopularityID, Description FROM [BabyName] bn Left
join Popularity p on bn.PopularityID = p.PopularityID" UpdateCommand="UPDATE BabyName
SET Name = @Name, PopularityID = @PopularityID WHERE NameID = @NameID"  &gt;<br />
                    
<br />
        &lt;/asp:SqlDataSource&gt;<br />
         &lt;asp:SqlDataSource ID="PopularitySqlSource"
runat="Server" ConnectionString="&lt;%$ ConnectionStrings:TestConnectionString %&gt;"
SelectCommand="SELECT PopularityID, [Description] FROM [Popularity]" &gt;</font>
        </p>
        <p>
          <font color="#0000ff" face="Courier New">        
&lt;/asp:SqlDataSource&gt;<br />
  
<br />
    &lt;/div&gt;</font>
        </p>
        <p>
There are now 2 data sources, one for the GridView, and the <font color="#0000ff" face="Courier New">PopularitySqlSource </font>for
the PopularityTrendDropDown. The PopularitySqlSource just provides a simple list
of the possible values to display in the dropdown. <font color="#0000ff" face="Courier New">TestSqlSource'</font>s
SelectCommand has a new join and and an extra column, Description.  This is so
that the Item template can display the user friendly text value in the label. 
I was searching for a sexier way of doing this, so I didn't have to include the join
and column in the statement, but heck, as long as we're polluting the aspx with SQL
statements might as well use all the SQL we can.
</p>
        <p>
In order to use a DropDown in the GridView, we need to stop autogenerating the columns
and create a TemplateField for the column that has the DropDown.   The TemplateField
has two parts: the ItemTemplate and the EditItemTemplate.  The ItemTemplate simply
shows the user friendly description of the PopularityTrend.  The EditItemTemplate
holds the DropDownList for selecting the proper PopularityID. Whatever is in the EditItemTemplate
is what shows up when the user clicks on the Edit button.
</p>
        <p>
There is a little bit of <a href="http://msdn2.microsoft.com/en-us/library/ms178366.aspx">data
binding script syntax </a>needed to properly bind the underlying values into the controls. 
For the DropDownList we bind the SelectedValue to the PopularityID...
</p>
        <p>
          <font color="#0000ff" face="Courier New">SelectedValue='&lt;%# Bind("PopularityID")
%&gt;'</font>
        </p>
        <p>
The bit bewteen &lt;%# and %&gt; is actually a call to the Bind method which
sets up the control to do two-way binding so that it can keep track of updates. 
This is used behind the scenes for the BoundField too.  Technically this is coding,
but since we are not creating a separate code file or breaking out the &lt;script&gt;
tags, we'll ignore it.
</p>
        <p>
Similarly the Text property of the label is bound using the Eval method.  
</p>
        <p>
          <font color="#0000ff" face="Courier New">Text='&lt;%# Eval("Description") %&gt;'</font>
        </p>
        <p>
The difference between Eval and Bind, is that Eval is a read-only binding, it can't
be used to update the underlying datasource.
</p>
        <p>
So all that is needed to create a GridView that uses a DropDownList to edit a column
value is:
</p>
        <ol>
          <li>
A TemplateField to hold the DropDown when a row is being edited, and a label when
it is not 
</li>
          <li>
A DataSource for the list portion of the DropDown control 
</li>
          <li>
A little extra SQL to get the friendly description for the label 
</li>
          <li>
A wee bit of binding <strike>code</strike> syntax</li>
        </ol>
        <p>
So, still no code, but our GridView and DataSource declarations are getting
heftier.  Probably still slimmer than the analogous code, but not nearly as compact
as the simple GridView from the previous example. 
</p>
        <p>
Actually, if there were a DropDownField available the declarative syntax could be
almost as slim as it was for the simple GridView.  I'm surprised that there wasn't
a DropDownField class created for ASP.NET 2.0 given their frequent use for editing
grid columns.  I'm sure someone has created one (or is is <a href="http://blogs.msdn.com/mattdotson/articles/490868.aspx">creating
one</a>) that could be added in. Could be a fun little project.
</p>
        <p>
Next time, we'll see if we can add in multi-row editing without code.  I have
my doubts.
</p>
        <p>
 
</p>
        <p>
 
</p>
        <img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=525bb927-f195-40af-8d0f-c160edd00d45" />
      </body>
      <title>Looking For More Trouble</title>
      <guid isPermaLink="false">http://www.vpsw.com/blogbaby/PermaLink,guid,525bb927-f195-40af-8d0f-c160edd00d45.aspx</guid>
      <link>http://www.vpsw.com/blogbaby/PermaLink,guid,525bb927-f195-40af-8d0f-c160edd00d45.aspx</link>
      <pubDate>Thu, 08 Feb 2007 04:20:59 GMT</pubDate>
      <description>&lt;p&gt;
In the&amp;nbsp;&lt;a href="http://www.vpsw.com/blogbaby/PermaLink,guid,33ff2980-8f72-432b-b214-8c0423d76168.aspx"&gt;previous
GridView post&lt;/a&gt;, we figured out how to create a simple updating GridView without
a lick of code.&amp;nbsp; But&amp;nbsp;it only updated a&amp;nbsp;value from a TextBox, nothing
too difficult about that.&amp;nbsp; What would happen if one of the columns&amp;nbsp;needed
a DropDownList for editing purposes?&amp;nbsp; This is something that required code in
ASP.NET 1.1.&amp;nbsp;The DropDownList needed to be bound in the ItemDataBound event,
and that was only for display purposes.&amp;nbsp; Updating required even more code.&amp;nbsp;
Can the declarative code model handle this twist without resulting to actual -- gasp
-- coding?
&lt;/p&gt;
&lt;p&gt;
I'll save the suspense and admit that, in fact, it is possible to create an&amp;nbsp;Editable,
Updating&amp;nbsp;GridView with a DropDownList&amp;nbsp; without writing any code.&amp;nbsp; Here
it is.&amp;nbsp;&amp;nbsp; We'll talk about the interesting bits afterwards.
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#0000ff" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;div&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:GridView ID="GridViewDropDown" runat="server"
DataSourceID="TestSqlSource" AutoGenerateEditButton="True" DataKeyNames="NameID" AutoGenerateColumns="False"
&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Columns&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:BoundField HeaderText="Name" DataField="Name"&amp;nbsp;
/&amp;gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#0000ff" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;asp:TemplateField HeaderText="Popularity Trend"&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;EditItemTemplate&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;asp:DropDownList ID="PopularityTrendDropDown" runat="server"&amp;nbsp;&amp;nbsp; SelectedValue='&amp;lt;%#
Bind("PopularityID") %&amp;gt;' DataSourceID="PopularitySqlSource" DataTextField="Description"&amp;nbsp;
DataValueField="PopularityID" &amp;gt;&amp;lt;/asp:DropDownList&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;/EditItemTemplate&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;ItemTemplate&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;asp:Label ID="PopularityTrendLabel" runat="server" Text='&amp;lt;%# Eval("Description")
%&amp;gt;'&amp;gt;&amp;lt;/asp:Label&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;/ItemTemplate&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/asp:TemplateField&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/Columns&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/asp:GridView&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:SqlDataSource ID="TestSqlSource"
runat="server" ConnectionString="&amp;lt;%$ ConnectionStrings:TestConnectionString %&amp;gt;"
SelectCommand="SELECT Name, NameID, PopularityID, Description FROM [BabyName] bn Left
join Popularity p on bn.PopularityID = p.PopularityID" UpdateCommand="UPDATE BabyName
SET Name = @Name, PopularityID = @PopularityID WHERE NameID = @NameID"&amp;nbsp; &amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/asp:SqlDataSource&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:SqlDataSource ID="PopularitySqlSource"
runat="Server" ConnectionString="&amp;lt;%$ ConnectionStrings:TestConnectionString %&amp;gt;"
SelectCommand="SELECT PopularityID, [Description] FROM [Popularity]" &amp;gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#0000ff" face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;/asp:SqlDataSource&amp;gt;&lt;br&gt;
&amp;nbsp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/div&amp;gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
There are now 2 data sources, one for the GridView, and the &lt;font color="#0000ff" face="Courier New"&gt;PopularitySqlSource &lt;/font&gt;for
the PopularityTrendDropDown.&amp;nbsp;The PopularitySqlSource just provides a simple list
of the possible values&amp;nbsp;to display in the dropdown.&amp;nbsp;&lt;font color="#0000ff" face="Courier New"&gt;TestSqlSource'&lt;/font&gt;s
SelectCommand has a new join and and an extra column, Description.&amp;nbsp; This is so
that the Item template can display the user friendly text value in the label.&amp;nbsp;
I was searching for a sexier way of doing this, so I didn't have to include the join
and column in the statement, but heck, as long as we're polluting the aspx with SQL
statements might as well use all the SQL we can.
&lt;/p&gt;
&lt;p&gt;
In order to use a DropDown in the GridView, we need to stop autogenerating the columns
and create a TemplateField for the column that has the DropDown.&amp;nbsp;&amp;nbsp; The TemplateField
has two parts: the ItemTemplate and the EditItemTemplate.&amp;nbsp; The ItemTemplate simply
shows the user friendly description of the PopularityTrend.&amp;nbsp; The EditItemTemplate
holds the DropDownList for selecting the proper PopularityID. Whatever is in the EditItemTemplate
is what shows up when the user clicks on the Edit button.
&lt;/p&gt;
&lt;p&gt;
There is a little bit of &lt;a href="http://msdn2.microsoft.com/en-us/library/ms178366.aspx"&gt;data
binding script syntax &lt;/a&gt;needed to properly bind the underlying values into the controls.&amp;nbsp;
For the DropDownList we bind the SelectedValue to the PopularityID...
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#0000ff" face="Courier New"&gt;SelectedValue='&amp;lt;%# Bind("PopularityID")
%&amp;gt;'&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
The bit bewteen &amp;lt;%# and&amp;nbsp;%&amp;gt; is actually a call to the Bind method which
sets up the control to do two-way binding so that it can keep track of updates.&amp;nbsp;
This is used behind the scenes for the BoundField too.&amp;nbsp; Technically this is coding,
but since we are not creating a separate code file or breaking out the &amp;lt;script&amp;gt;
tags, we'll ignore it.
&lt;/p&gt;
&lt;p&gt;
Similarly the&amp;nbsp;Text property of the label is bound using the Eval method.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#0000ff" face="Courier New"&gt;Text='&amp;lt;%# Eval("Description") %&amp;gt;'&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
The difference between Eval and Bind, is that Eval is a read-only binding, it can't
be used to update the underlying datasource.
&lt;/p&gt;
&lt;p&gt;
So all that is needed to create a GridView that uses a DropDownList to edit a column
value is:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
A TemplateField to hold the DropDown when a row is being edited, and a label when
it is not 
&lt;/li&gt;
&lt;li&gt;
A DataSource for the list portion of the DropDown control 
&lt;/li&gt;
&lt;li&gt;
A little extra SQL to get the friendly description for the label 
&lt;/li&gt;
&lt;li&gt;
A wee bit of binding &lt;strike&gt;code&lt;/strike&gt; syntax&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
So, still no code, but our GridView and DataSource declarations&amp;nbsp;are&amp;nbsp;getting
heftier.&amp;nbsp; Probably still slimmer than the analogous code, but not nearly as compact
as the simple GridView from the previous example. 
&lt;/p&gt;
&lt;p&gt;
Actually, if there were a DropDownField available the declarative syntax could be
almost as slim as it was for the simple GridView.&amp;nbsp; I'm surprised that there wasn't
a DropDownField class created for ASP.NET 2.0 given their frequent use for editing
grid columns.&amp;nbsp; I'm sure someone has created one (or is is &lt;a href="http://blogs.msdn.com/mattdotson/articles/490868.aspx"&gt;creating
one&lt;/a&gt;) that could be added in. Could be a fun little project.
&lt;/p&gt;
&lt;p&gt;
Next time, we'll see if we can add in multi-row editing without code.&amp;nbsp; I have
my doubts.
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=525bb927-f195-40af-8d0f-c160edd00d45" /&gt;</description>
      <comments>http://www.vpsw.com/blogbaby/CommentView,guid,525bb927-f195-40af-8d0f-c160edd00d45.aspx</comments>
      <category>2.0</category>
      <category>GridView</category>
      <category>SqlDataSource</category>
    </item>
    <item>
      <trackback:ping>http://www.vpsw.com/blogbaby/Trackback.aspx?guid=33ff2980-8f72-432b-b214-8c0423d76168</trackback:ping>
      <pingback:server>http://www.vpsw.com/blogbaby/pingback.aspx</pingback:server>
      <pingback:target>http://www.vpsw.com/blogbaby/PermaLink,guid,33ff2980-8f72-432b-b214-8c0423d76168.aspx</pingback:target>
      <dc:creator>Dean</dc:creator>
      <wfw:comment>http://www.vpsw.com/blogbaby/CommentView,guid,33ff2980-8f72-432b-b214-8c0423d76168.aspx</wfw:comment>
      <wfw:commentRss>http://www.vpsw.com/blogbaby/SyndicationService.asmx/GetEntryCommentsRss?guid=33ff2980-8f72-432b-b214-8c0423d76168</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Starting to get more questions about <a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx">GridViews</a> and <a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx">SqlDataSources</a> on
the forums I haunt.  On the surface, these two components seem to offer a quick
path to application development with no coding.  
</p>
        <p>
And sure enough, once you figure them out, you don't have to do much to get an updatable
GridView on the page.
</p>
        <p>
Here's an example of a fully updating GridView...
</p>
        <blockquote dir="ltr" style="margin-right: 0px;">
          <p dir="ltr" style="margin-right: 0px;">
            <font color="#0000ff" face="Courier New">&lt;asp:GridView ID="NoCodeGridView" runat="server"
DataSourceID="TestSqlSource" AutoGenerateEditButton="True"   DataKeyNames="NameID"&gt;<br />
&lt;/asp:GridView&gt;<br /></font>
          </p>
          <p>
            <font color="#0000ff" face="Courier New">&lt;asp:SqlDataSource ID="TestSqlSource"
runat="server" ConnectionString="&lt;%$ ConnectionStrings:TestConnectionString %&gt;"
SelectCommand="SELECT * FROM [BabyName]" </font>
            <font color="#0000ff" face="Courier New">UpdateCommand="UPDATE
BabyName SET Name = @Name WHERE NameID = @NameID"&gt;<br />
            &lt;UpdateParameters&gt;<br />
               
&lt;asp:Parameter Name="Name" Size="50" Type="String" /&gt;<br />
               
&lt;asp:Parameter Name="NameID" Type="Int32" /&gt;<br />
            &lt;/UpdateParameters&gt;<br />
 &lt;/asp:SqlDataSource&gt;</font>
          </p>
        </blockquote>
        <p>
The code behind file is strangely empty, yet clicking on a row's edit button
brings up an editable row with an update and cancel button.  And clicking on
the update button saves the altered baby name.  This scares and frightens
me, because I know it won't be this easy once I need to do something real. While
I appreciate that the details of implementation have been hidden from me, the
fact that I can't see them also worries me. I'll leave that aside for the moment. I
will also ignore the fact that I have embedded SQL statements in an ASPX page, and
we'll move on and examine what's needed so the snippet above works.
</p>
        <p>
The key in the GridView is this attribute:
</p>
        <font color="#ff0000" size="2">
        </font>
        <blockquote dir="ltr" style="margin-right: 0px;">
          <p>
            <font color="#ff0000" size="2">
              <font color="#0000ff" face="Courier New">AutoGenerateEditButton="True" </font>
            </font>
          </p>
        </blockquote>
        <p>
This tells the GridView to generate the Edit button and Update/Cancel button. There
are other ways to do this, but we're looking for the quickest, dirtiest way at the
moment.
</p>
        <p>
The real keys are in the SqlDataSource.  Without a select command nothing
would be bound to the grid. And it obviously needs an update command, otherwise you
get this exception...
</p>
        <blockquote dir="ltr" style="margin-right: 0px;">
          <h2>
            <i>
              <font size="3">Updating is not supported by data source 'TestSqlSource' unless
UpdateCommand is specified.</font>
            </i>
          </h2>
        </blockquote>
        <p>
Reading the stack trace for the exception shows you what ASP.NET is doing behind the
scenes. 
</p>
        <blockquote dir="ltr" style="margin-right: 0px;">
          <p>
            <font color="#008000">
              <font face="Courier New">
                <font color="#ff1493">
                  <font color="#ff0000">  
System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary
values, IDictionary oldValues) +1828259<br />
   System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values,
IDictionary oldValues,    DataSourceViewOperationCallback callback)
+78<br />
   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32
rowIndex, Boolean causesValidation) +1215<br />
   </font>
                  <font color="#008000">System.Web.UI.WebControls.GridView.HandleEvent(EventArgs
e, Boolean causesValidation, String validationGroup) +858</font>
                  <br />
                </font>   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source,
EventArgs e) +95<br />
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
+35<br />
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs
e) +117<br />
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
+35<br />
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115<br />
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
+163<br />
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +7<br />
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument) +11<br />
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +174<br />
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,
Boolean includeStagesAfterAsyncPoint) +5102</font>
              <br />
            </font>
          </p>
        </blockquote>
        <p>
The last three are the interesting ones (the stack reads down from the last method
called to the first).  The GridView's HandleEvent figures out an Update is going
on and calls HandleUpdate, which in turn calls the Update method on the DataSourceView,
which in turn calls the ExecuteUpdate on the SqlDataSourceView which does the real
work.  
</p>
        <p>
Let's take a little side trip and see what we can find out about these methods.
</p>
        <p>
GridView.HandleUpdate is not a public method and hence does not appear in the documentation.
</p>
        <p>
DataSourceView.Update is <a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.datasourceview.update.aspx">public</a>. 
DataSourceView is an abstract base class that alll the DataSourceView implementations
are derived from.  This method calls the protected ExecuteUpdate method on that
has been implemented by the inheriting class, in this case <a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasourceview.aspx">SqlDataSourceView</a>.<a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasourceview.aspx">ExecuteUpdate</a>.
All pretty straightforward, and the SqlDataSourceView has some event handlers so it
looks like there are places to hook in when actual programming is required.
</p>
        <p>
Let's end our little side trip and get back into seeing what's needed for the little
snippet to work.
</p>
        <p>
One thing I noted was the documentation frequently mentions parameters, so they must
be needed to make this work.  Let's pull out the &lt;UpdateParameters&gt; element
and see what happens.
</p>
        <p>
          <a href="http://www.m-w.com/cgi-bin/dictionary?va=begorra">Begorra</a>! The GridView does
not throw an error and still updates.  It seems everything will be peachy if
the update command parameter names correspond to columns in the select command. Let's
run a wee test and change a parameter name in the update command. @Name will become
@Name1
</p>
        <p>
Didn't like that all
</p>
        <blockquote dir="ltr" style="margin-right: 0px;">
          <h2>
            <font size="3">
              <em>Must declare the variable '@Name1'.</em>
            </font>
          </h2>
        </blockquote>
        <p>
So this is all you actually need...
</p>
        <p>
    <font color="#0000ff" face="Courier New"> &lt;asp:GridView
ID="NoCodeGridView" runat="server" DataSourceID="TestSqlSource" AutoGenerateEditButton="True"
DataKeyNames="NameID"&gt;<br />
     &lt;/asp:GridView&gt;<br />
     &lt;asp:SqlDataSource ID="TestSqlSource" runat="server" ConnectionString="&lt;%$
ConnectionStrings:TestConnectionString %&gt;" SelectCommand="SELECT * FROM [BabyName]"
UpdateCommand="UPDATE BabyName SET Name = @Name WHERE NameID = @NameID"&gt;<br />
         
<br />
     &lt;/asp:SqlDataSource&gt;</font></p>
        <p>
As long as the SqlDataSource's SelectCommand returns column names that correspond
to the parameter names used in the UpdateCommand it all works.
</p>
        <p>
Next time we'll see what happens when you need to do something a little fancier.
</p>
        <img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=33ff2980-8f72-432b-b214-8c0423d76168" />
      </body>
      <title>Here Comes Trouble</title>
      <guid isPermaLink="false">http://www.vpsw.com/blogbaby/PermaLink,guid,33ff2980-8f72-432b-b214-8c0423d76168.aspx</guid>
      <link>http://www.vpsw.com/blogbaby/PermaLink,guid,33ff2980-8f72-432b-b214-8c0423d76168.aspx</link>
      <pubDate>Wed, 31 Jan 2007 03:18:17 GMT</pubDate>
      <description>&lt;p&gt;
Starting to get more questions about &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx"&gt;GridViews&lt;/a&gt; and &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx"&gt;SqlDataSources&lt;/a&gt;&amp;nbsp;on
the forums I haunt.&amp;nbsp; On the surface, these two components seem to offer a quick
path to application development with no coding.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
And sure enough, once you figure them out, you don't have to do much to get an updatable
GridView on the page.
&lt;/p&gt;
&lt;p&gt;
Here's an example of a fully updating GridView...
&lt;/p&gt;
&lt;blockquote dir="ltr" style="margin-right: 0px;"&gt; 
&lt;p dir="ltr" style="margin-right: 0px;"&gt;
&lt;font color="#0000ff" face="Courier New"&gt;&amp;lt;asp:GridView ID="NoCodeGridView" runat="server"
DataSourceID="TestSqlSource" AutoGenerateEditButton="True"&amp;nbsp;&amp;nbsp; DataKeyNames="NameID"&amp;gt;&lt;br&gt;
&amp;lt;/asp:GridView&amp;gt;&lt;br&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#0000ff" face="Courier New"&gt;&amp;lt;asp:SqlDataSource ID="TestSqlSource"
runat="server" ConnectionString="&amp;lt;%$ ConnectionStrings:TestConnectionString %&amp;gt;"
SelectCommand="SELECT * FROM [BabyName]" &lt;/font&gt;&lt;font color="#0000ff" face="Courier New"&gt;UpdateCommand="UPDATE
BabyName SET Name = @Name WHERE NameID = @NameID"&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;UpdateParameters&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;asp:Parameter Name="Name" Size="50" Type="String" /&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;asp:Parameter Name="NameID" Type="Int32" /&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/UpdateParameters&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;lt;/asp:SqlDataSource&amp;gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The code behind file is strangely empty, yet clicking on&amp;nbsp;a row's&amp;nbsp;edit button
brings up an editable row with an update and cancel button.&amp;nbsp; And clicking on
the update button saves the&amp;nbsp;altered baby name.&amp;nbsp; This scares and frightens
me, because I know it won't&amp;nbsp;be this easy once I need to do something real. While
I appreciate that the details of implementation have been hidden from me,&amp;nbsp;the
fact that I can't see them&amp;nbsp;also worries me. I'll leave that aside for the moment.&amp;nbsp;I
will also ignore the fact that I have embedded SQL statements in an ASPX page, and
we'll move on and examine what's needed so the snippet above works.
&lt;/p&gt;
&lt;p&gt;
The key in the GridView is this attribute:
&lt;/p&gt;
&lt;font color="#ff0000" size="2"&gt; &lt;/font&gt;&lt;blockquote dir="ltr" style="margin-right: 0px;"&gt; 
&lt;p&gt;
&lt;font color="#ff0000" size="2"&gt;&lt;font color="#0000ff" face="Courier New"&gt;AutoGenerateEditButton="True"&amp;nbsp;&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
This tells the GridView to generate the Edit button and Update/Cancel button. There
are other ways to do this, but we're looking for the quickest, dirtiest way at the
moment.
&lt;/p&gt;
&lt;p&gt;
The real keys&amp;nbsp;are in the SqlDataSource.&amp;nbsp; Without a select command nothing
would be bound to the grid. And it obviously needs an update command, otherwise you
get this exception...
&lt;/p&gt;
&lt;blockquote dir="ltr" style="margin-right: 0px;"&gt; 
&lt;h2&gt;&lt;i&gt;&lt;font size="3"&gt;Updating is not supported by data source 'TestSqlSource' unless
UpdateCommand is specified.&lt;/font&gt;&lt;/i&gt; 
&lt;/h2&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Reading the stack trace for the exception shows you what ASP.NET is doing behind the
scenes.&amp;nbsp;
&lt;/p&gt;
&lt;blockquote dir="ltr" style="margin-right: 0px;"&gt; 
&lt;p&gt;
&lt;font color="#008000"&gt;&lt;font face="Courier New"&gt;&lt;font color="#ff1493"&gt;&lt;font color="#ff0000"&gt;&amp;nbsp;&amp;nbsp;
System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary
values, IDictionary oldValues) +1828259&lt;br&gt;
&amp;nbsp;&amp;nbsp; System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values,
IDictionary oldValues,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DataSourceViewOperationCallback callback)
+78&lt;br&gt;
&amp;nbsp;&amp;nbsp; System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32
rowIndex, Boolean causesValidation) +1215&lt;br&gt;
&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#008000"&gt;System.Web.UI.WebControls.GridView.HandleEvent(EventArgs
e, Boolean causesValidation, String validationGroup) +858&lt;/font&gt;
&lt;br&gt;
&lt;/font&gt;&amp;nbsp;&amp;nbsp; System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source,
EventArgs e) +95&lt;br&gt;
&amp;nbsp;&amp;nbsp; System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
+35&lt;br&gt;
&amp;nbsp;&amp;nbsp; System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs
e) +117&lt;br&gt;
&amp;nbsp;&amp;nbsp; System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
+35&lt;br&gt;
&amp;nbsp;&amp;nbsp; System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115&lt;br&gt;
&amp;nbsp;&amp;nbsp; System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
+163&lt;br&gt;
&amp;nbsp;&amp;nbsp; System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +7&lt;br&gt;
&amp;nbsp;&amp;nbsp; System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,
String eventArgument) +11&lt;br&gt;
&amp;nbsp;&amp;nbsp; System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +174&lt;br&gt;
&amp;nbsp;&amp;nbsp; System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,
Boolean includeStagesAfterAsyncPoint) +5102&lt;/font&gt;
&lt;br&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The last three are the interesting ones (the stack reads down from the last method
called to the first).&amp;nbsp; The GridView's HandleEvent figures out an Update is going
on and calls HandleUpdate, which in turn calls the Update method on the DataSourceView,
which in turn calls the ExecuteUpdate on the SqlDataSourceView which does the real
work.&amp;nbsp; 
&lt;/p&gt;
&lt;p&gt;
Let's take a little side trip and see what we can find out about these methods.
&lt;/p&gt;
&lt;p&gt;
GridView.HandleUpdate is not a public method and hence does not appear in the documentation.
&lt;/p&gt;
&lt;p&gt;
DataSourceView.Update&amp;nbsp;is &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.datasourceview.update.aspx"&gt;public&lt;/a&gt;.&amp;nbsp;
DataSourceView is an abstract base class that alll the DataSourceView implementations
are derived from.&amp;nbsp; This method calls the protected ExecuteUpdate method on that
has been implemented by the inheriting class, in this case &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasourceview.aspx"&gt;SqlDataSourceView&lt;/a&gt;.&lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasourceview.aspx"&gt;ExecuteUpdate&lt;/a&gt;.
All pretty straightforward, and the SqlDataSourceView has some event handlers so it
looks like there are places to hook in when actual programming is required.
&lt;/p&gt;
&lt;p&gt;
Let's end our little side trip and get back into seeing what's needed for the little
snippet to work.
&lt;/p&gt;
&lt;p&gt;
One thing I noted was the documentation frequently mentions parameters, so they must
be needed to make this work.&amp;nbsp; Let's pull out the &amp;lt;UpdateParameters&amp;gt; element
and see what happens.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.m-w.com/cgi-bin/dictionary?va=begorra"&gt;Begorra&lt;/a&gt;! The&amp;nbsp;GridView&amp;nbsp;does
not throw an error and still updates.&amp;nbsp; It seems everything will be peachy if
the update command parameter names correspond to columns in the select command. Let's
run a wee test and change a parameter name in the update command. @Name will become
@Name1
&lt;/p&gt;
&lt;p&gt;
Didn't like that all
&lt;/p&gt;
&lt;blockquote dir="ltr" style="margin-right: 0px;"&gt; 
&lt;h2&gt;&lt;font size="3"&gt;&lt;em&gt;Must declare the variable '@Name1'.&lt;/em&gt;&lt;/font&gt; 
&lt;/h2&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
So this is all you actually need...
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color="#0000ff" face="Courier New"&gt; &amp;lt;asp:GridView
ID="NoCodeGridView" runat="server" DataSourceID="TestSqlSource" AutoGenerateEditButton="True"
DataKeyNames="NameID"&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/asp:GridView&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:SqlDataSource ID="TestSqlSource" runat="server" ConnectionString="&amp;lt;%$
ConnectionStrings:TestConnectionString %&amp;gt;" SelectCommand="SELECT * FROM [BabyName]"
UpdateCommand="UPDATE BabyName SET Name = @Name WHERE NameID = @NameID"&amp;gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/asp:SqlDataSource&amp;gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
As long as the SqlDataSource's SelectCommand returns column names that correspond
to the parameter names used in the UpdateCommand it all works.
&lt;/p&gt;
&lt;p&gt;
Next time we'll see what happens when you need to do something a little fancier.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=33ff2980-8f72-432b-b214-8c0423d76168" /&gt;</description>
      <comments>http://www.vpsw.com/blogbaby/CommentView,guid,33ff2980-8f72-432b-b214-8c0423d76168.aspx</comments>
      <category>2.0</category>
      <category>GridView</category>
      <category>SqlDataSource</category>
    </item>
  </channel>
</rss>