Link to home
Start Free TrialLog in
Avatar of nickbailey111
nickbailey111

asked on

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!
Avatar of Munawar Hussain
Munawar Hussain
Flag of Pakistan image

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

thanks
Avatar of nickbailey111
nickbailey111

ASKER

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="guid" DataSourceID="SDS_Cases" 
            onrowupdating="GridView1_RowUpdating">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="guid" HeaderText="guid" 
                    SortExpression="guid" ReadOnly="True" Visible="False" />
                <asp:TemplateField HeaderText="rm_guid" SortExpression="rm_guid">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" 
                            DataSourceID="SqlDataSourceRM" DataTextField="rm_name" DataValueField="guid">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSourceRM" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:IConn %>" 
                            SelectCommand="select * from id_rm"></asp:SqlDataSource>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("rm_guid") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <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">
            <UpdateParameters>
                <asp:Parameter Name="rm_guid" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </div>
  </form>
</body>
</html>
 
 
CODE BEHIND:
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)
            (GridView1.Rows[e.RowIndex].FindControl("DDL_RM"));
            e.NewValues.Add("guid", rm.SelectedValue);
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nickbailey111
nickbailey111

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial