using the update feature included with GridView edit, drop down list value being lost

I've got a GridView populated with data from datasource A. One of the columns is a user name, stored in table A. There's also a list of user names in table b. What I'd like to accomplish is to enter into edit view on the GridView, select a new user name from a drop down list, click update on the GridView row and HAVE IT ACTUALLY UPDATE! The examples I have been able to find seem to stop the example without clicking update! When I click update, the data field on table A is blanked out. I know it has something to do with finding the drop down list control (since it doesn't exist until runtime) in the code behind, but coming from classic asp, C# is still very new to me. Could someone help me out? Thanks!
Who is Participating?
nickbailey111Connect With a Mentor Author Commented:
simple problems- I spotted my error once I posted the code.

1) my drop down list id wasn't DDL_RM as I specified in the findcontrol action
2) mixed up my sql data source names

Thanks all anyway,.
Munawar HussainPrincipal Software EngineerCommented:
you need to add parameter to for new value you want to update
for that go to properties of SqlDataSource and select UpdateQuery .. it will open another window . from there add parameter name and tell from where to get value of parameter.. ie control . form or ... others options.

actually you have not set the parameters for update query hence it just update the table with spaces. which is default for update query

nickbailey111Author Commented:
take a look at the code, try to run it - I get a failure on "e.NewValues.Add("guid", rm.SelectedValue) not being set to an instance of an object.

aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="IDNY_web._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="guid" DataSourceID="SDS_Cases" 
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="guid" HeaderText="guid" 
                    SortExpression="guid" ReadOnly="True" Visible="False" />
                <asp:TemplateField HeaderText="rm_guid" SortExpression="rm_guid">
                        <asp:DropDownList ID="DropDownList1" runat="server" 
                            DataSourceID="SqlDataSourceRM" DataTextField="rm_name" DataValueField="guid">
                        <asp:SqlDataSource ID="SqlDataSourceRM" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:IConn %>" 
                            SelectCommand="select * from id_rm"></asp:SqlDataSource>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("rm_guid") %>'></asp:Label>
        <asp:SqlDataSource ID="SDS_RMs" runat="server" 
            ConnectionString="<%$ ConnectionStrings:Conn %>" 
            SelectCommand="select * from id_cases" 
            UpdateCommand="update id_Cases set rm_guid=@rm_guid where guid=@guid">
                <asp:Parameter Name="rm_guid" />
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace IDNY_web
    public partial class _Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            DropDownList rm = (DropDownList)
            e.NewValues.Add("guid", rm.SelectedValue);

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.