Note that there are some explanatory texts on larger screens.

plurals
  1. POLimit GridView edit to only one column
    primarykey
    data
    text
    <p>I have a gridview set up like this:</p> <pre><code>&lt;asp:GridView ID="GridViewUsers" runat="server" DataSourceID="AccessDataSourceUsers" AllowSorting="True" AutoGenerateColumns="False" AutoGenerateEditButton="True"&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="username" HeaderText="username" SortExpression="username" /&gt; &lt;asp:TemplateField HeaderText="role" SortExpression="role"&gt; &lt;EditItemTemplate&gt; &lt;asp:DropDownList ID="DropDownListRoles" runat="server" DataSourceID="AccessDataSourceRoles" DataTextField="role" DataValueField="role" SelectedValue='&lt;%# Bind("role") %&gt;'&gt; &lt;/asp:DropDownList&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Bind("role") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; </code></pre> <p>So the gridview contains a list of users and their roles, upon selecting edit the roles column cells turn into dropdown lists containing roles (currently only Admin and Member).</p> <p>The issue I'm currently tackling is getting the appropriate values from the gridview (username and selected value from the dropdown list) into the parameters of the update query.</p> <p>Current codebehind:</p> <pre><code>protected void AccessDataSourceUsers_Updating(object sender, SqlDataSourceCommandEventArgs e) { GridViewRow row = GridViewUsers.Rows[GridViewUsers.EditIndex]; DropDownList ddl = (DropDownList)row.FindControl("DropDownListRoles"); AccessDataSourceUsers.UpdateParameters.Add("@role", ddl.SelectedValue); AccessDataSourceUsers.UpdateParameters.Add("@username", row.Cells[0].Text); } </code></pre> <p>I managed to get the selected value of the dropdownlist just fine, however I can't get the username, instead I only get an empty string. I'm assuming that's because upon clicking edit the username field turns into a textbox.</p> <p>How could I prevent editing the username column so that the cells don't turn into a textbox (I only wish for the roles column to be editable). And also will that automatically fix the problem of getting the value through <code>row.Cells[0].Text</code> or is there something else I'm missing here?</p> <p><strong>EDIT:</strong> Issue 2. : Parameters and query</p> <p>My update command looks like this:</p> <pre><code>UpdateCommand="UPDATE users SET users.roleID = DLookup( &amp;quot;[id]&amp;quot; , &amp;quot;roles&amp;quot; , &amp;quot;[role] = '?'&amp;quot;) WHERE ((username = '?')); </code></pre> <p>Codebehind parameters:</p> <pre><code>protected void GridViewUsers_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = GridViewUsers.Rows[GridViewUsers.EditIndex]; DropDownList ddl = (DropDownList)row.FindControl("DropDownListRoles"); Label lbl = (Label)row.FindControl("LabelItemEditUsername"); if (ddl != null &amp;&amp; ddl.SelectedValue != null &amp;&amp; lbl != null) { AccessDataSourceUsers.UpdateParameters.Add("?", System.Data.DbType.String, ddl.SelectedValue); // I've also tried this without specifying the DbType AccessDataSourceUsers.UpdateParameters.Add("?", System.Data.DbType.String, lbl.Text); int result = AccessDataSourceUsers.Update(); System.Diagnostics.Debug.WriteLine(AccessDataSourceUsers.UpdateCommand); System.Diagnostics.Debug.WriteLine(AccessDataSourceUsers.UpdateParameters[0]); System.Diagnostics.Debug.WriteLine(AccessDataSourceUsers.UpdateParameters[1]); System.Diagnostics.Debug.WriteLine(result); } else { e.Cancel = true; } } </code></pre> <p>After clicking update on gridview the debug values in output are:</p> <pre><code>UPDATE users SET users.roleID = DLookup( "[id]" , "roles" , "[role] = '?'") WHERE ((username = '?')); ? ? 0 </code></pre> <p>I'm using <code>?</code> due to this line in <a href="http://msdn.microsoft.com/en-us/library/8e5545e1%28v=vs.100%29.aspx" rel="nofollow">Retrieving Data Using the AccessDataSource Web Server Control</a> : <code>Because the AccessDataSource control extends the SqlDataSource class and uses the System.Data.OleDb provider, you specify parameter placeholders using the "?" placeholder character. The System.Data.OleDb provider does not support named parameters;</code></p> <p>What is weird is that there are two empty lines and then the two placeholder names. Isn't <code>AccessDataSourceUsers.UpdateParameters[0]</code> supposed to return the value of the parameter, and <code>AccessDataSourceUsers.UpdateParameters[0].Name</code> return the name? Are the two empty lines supposed to be values? If so why are they empty?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload