Gridview Edit template not working, when assigned with 2 datasouces.

Posted on 2009-05-01
Last Modified: 2012-05-06
Hi all,
Thanks for looking into my problem.Here it is
I have a Grid View, Where i have o populated Customers using SQLDatasource.The users can edit the customer information and update it..Editing works fine here.

I have now  included a search Text box. So when users type their Search criteria(alphabet), i populate the Grid view, in Code behind as i have written in code snippet.Thats working fine.

But when Users Click Edit, It says row_editing event of grid view not found.
Please help me with this problem
" this is the way i update the parameters

<asp:SqlDataSource ID="SQlCustomersEdit" runat="server" 


Select Command ='select * from customers'

UpdateCommand="UPDATE [Customers] 

                                SET [AddressLine1] =@AddressLine1, 

                                 [AddressLine2] = @AddressLine2, 

                                 [City] = @City, 

                                 [State] = @State, 

                                 [ZipCode] = @ZipCode, 

                                 [ZipExt] = @ZipExt,

                                 [Abbr] = @Abbr

                                 WHERE Convert(varchar(100),Customers.CustomerId) = @CustomerGUID">


                <asp:Parameter Type="string" Name="CustomerId"></asp:Parameter>

                <asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>

                <asp:Parameter Type="String" Name="AddressLine1"></asp:Parameter>

                <asp:Parameter Type="String" Name="AddressLine2"></asp:Parameter>

                <asp:Parameter Type="String" Name="City"></asp:Parameter>

                <asp:Parameter Type="String" Name="State"></asp:Parameter>

                <asp:Parameter Type="String" Name="ZipCode"></asp:Parameter>

                <asp:Parameter Type="String" Name="ZipExt"></asp:Parameter>

                <asp:Parameter Type="String" Name="Abbr"></asp:Parameter>


And When Users serch for a customer, thi is how i populate the gridview:

Protected Sub btnSearchCust_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearchCust.Click

        Dim SearchValue As String = txtSearchCust.Text

        Dim strCustQuery As String

        strCustQuery = String.Format("SELECT Convert(varchar(100),Customers.CustomerId) As CustomerGUID, Customers.CompanyName,Customers.AddressLine1,Customers.AddressLine2, Customers.City, Customers.State, Customers.ZipCode, Customers.ZipExt, Customers.Abbr,Customers.StateProvinceId, StateProvince.CountryRegionName FROM Customers INNER JOIN StateProvince ON Customers.StateProvinceId = StateProvince.StateProvinceId Where  Customers.CompanyName Like '" & SearchValue & "%' order by Customers.CompanyName")

        SQlCustomersEdit.SelectCommand = strCustQuery

        'SQlCustomersEdit.SelectParameters(0).DefaultValue = SearchValue


        GRDCustomersEdit.DataSource = SQlCustomersEdit.Select(DataSourceSelectArguments.Empty)

        GRDCustomersEdit.DataSourceID = Nothing



    End Sub

Open in new window

Question by:SaraDob
    LVL 1

    Author Comment

    LVL 2

    Expert Comment

    You have to specify the edit command in the gridview.  Notice how you specify the select statement? You have to do the same for the update statement.

    I think that is where the issue is anyway.
    LVL 1

    Author Comment

    Thanks for your reply TeamEnova,

    I have specified the edit command in the gridview.I have pasted a part of my gridveiw (that is only one columns for example).
    You mean to say, on GridRow editing, i have specify Update command also.

     <asp:GridView ID="GRDCustomersEdit" runat="server" AllowPaging="True"  CssClass ="tabulardata"
            AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerGUID" 
                OnPageIndexChanging="GRDCustomersEdit_PageIndexChanging"  PageSize="10"
            emptydatatext="No Contacts in the database" Height="481px">
             <asp:CommandField ShowEditButton="True"></asp:CommandField>
                <asp:BoundField DataField="CustomerGUID" HeaderText="CustomerGUID" ReadOnly="True" 
                    SortExpression="CustomerGUID" />
                <asp:BoundField  DataField="CompanyName" HeaderText="Company" 
                    SortExpression="Company Name" ReadOnly="True"  InsertVisible="False"/>
                <asp:TemplateField HeaderText="Address Line1" SortExpression="Address Line1">
                        <asp:TextBox ID="txtAdd1" runat="server" Text='<%# Bind("AddressLine1") %>' Width="100px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="ReqAdd1" runat="server" 
                            ControlToValidate="txtAdd1" ErrorMessage="Enter Address 1" ValidationGroup="ValSumCustomersEdit"></asp:RequiredFieldValidator>

    Open in new window

    LVL 2

    Expert Comment

    Yes.  When you click edit, it assumes that when you are done, you will update the record that was passed in.  So if you have not specified the update command in the second gridview, you will have to do that.  The first update command in the first gridview does  not transfer over to the second one.

    Actually, unless you are using gridview1 to actually do the updating, I am not sure you need the update command at all on gridview1.  You are basically doing a "grab the selected row and pass it to something else where it will be updated" sort of thing.  But the gridview that will actually be doing the update will have to have its update command specified.
    LVL 1

    Author Comment

    Let me more  clear.
    I'm using only one grid view.This grid view has all customers On that page i have a search button.When the user enters alphabet in the Search textbox, and clicks the search button.I populate the SAME gridview as i had mentioned earlier in, btn_onlick codebehind .

    Then when i say edit, it is giving problems.
    I also assumed that it should take care of editing, since its on the same gridview, but its not editing.
    How to go about that please?

    LVL 2

    Accepted Solution

    Ok, I mocked up some stuff for you.  The queries are  not as involved as yours, but hopefully, you will get the idea.  It worked fine for me.

    If you look at the code behind, you see that I am not reassigning the gridviews datasource, but rather just changing its select statement and then calling gridview1.databind().  I think that when you re-assign the same sqldatasource to the gridview, something is happening.
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <head runat="server">
        <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="btnSearch"
            runat="server" Text="Search" />
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                AutoGenerateColumns="False" AutoGenerateDeleteButton="True" 
                AutoGenerateEditButton="True" DataKeyNames="ContactID" 
                    <asp:BoundField DataField="ContactID" HeaderText="ContactID" 
                        InsertVisible="False" ReadOnly="True" SortExpression="ContactID" />
                    <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                    <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                        SortExpression="FirstName" />
                    <asp:BoundField DataField="MiddleName" HeaderText="MiddleName" 
                        SortExpression="MiddleName" />
                    <asp:BoundField DataField="LastName" HeaderText="LastName" 
                        SortExpression="LastName" />
                    <asp:BoundField DataField="Suffix" HeaderText="Suffix" 
                        SortExpression="Suffix" />
                    <asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" 
                        SortExpression="EmailAddress" />
                    <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>" 
                DeleteCommand="DELETE FROM Person.Contact WHERE (ContactID = @ContactID)" 
                InsertCommand="INSERT INTO Person.Contact(Title, FirstName, MiddleName, LastName, Suffix, EmailAddress, Phone) VALUES (@Title, @FirstName, @MiddleName, @LastName, @Suffix, @EmailAddress, @Phone)" 
                SelectCommand="SELECT ContactID, Title, FirstName, MiddleName, LastName, Suffix, EmailAddress, Phone FROM Person.Contact" 
                UpdateCommand="UPDATE Person.Contact SET Title = @Title, FirstName = @FirstName, MiddleName = @MiddleName, LastName = @LastName, Suffix = @Suffix, EmailAddress = @EmailAddress, Phone = @Phone WHERE (ContactID = @ContactID)">
                    <asp:Parameter Name="ContactID" Type="Int32" />
                    <asp:Parameter Name="Title" Type="String" />
                    <asp:Parameter Name="FirstName" Type="String" />
                    <asp:Parameter Name="MiddleName" Type="String" />
                    <asp:Parameter Name="LastName" Type="String" />
                    <asp:Parameter Name="Suffix" Type="String" />
                    <asp:Parameter Name="EmailAddress" Type="String" />
                    <asp:Parameter Name="Phone" Type="String" />
                    <asp:Parameter Name="ContactID" Type="Int32" />
                    <asp:Parameter Name="Title" Type="String" />
                    <asp:Parameter Name="FirstName" Type="String" />
                    <asp:Parameter Name="MiddleName" Type="String" />
                    <asp:Parameter Name="LastName" Type="String" />
                    <asp:Parameter Name="Suffix" Type="String" />
                    <asp:Parameter Name="EmailAddress" Type="String" />
                    <asp:Parameter Name="Phone" Type="String" />
    *******************code behind***********************
    Partial Class _Default
        Inherits System.Web.UI.Page
        Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
            If Not TextBox1.Text Is Nothing Then
                Dim sSearchString As String = TextBox1.Text
                Dim sCustQuery As String
                sCustQuery = "SELECT ContactID, Title, FirstName, MiddleName, LastName, Suffix, EmailAddress, Phone FROM Person.Contact WHERE LastName Like '" & sSearchString & "%'"
                SqlDataSource1.SelectCommand = sCustQuery
            End If
        End Sub
    End Class

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now