Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-05-01
6
Medium Priority
?
238 Views
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" 
        ConnectionString=''
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">
        <UpdateParameters>
                <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>
            </UpdateParameters>
 
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
        GRDCustomersEdit.DataBind()
 
        MyConn.Close()
    End Sub

Open in new window

0
Comment
Question by:SaraDob
  • 3
  • 3
6 Comments
 
LVL 1

Author Comment

by:SaraDob
ID: 24282073
Anyone?
0
 
LVL 2

Expert Comment

by:TeamEnova
ID: 24282088
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.
0
 
LVL 1

Author Comment

by:SaraDob
ID: 24282175
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" 
        DataSourceID="SQlCustomersEdit" 
            OnPageIndexChanging="GRDCustomersEdit_PageIndexChanging"  PageSize="10"
        emptydatatext="No Contacts in the database" Height="481px">
        <Columns>
         <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">
                <EditItemTemplate>
                    <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>
                </EditItemTemplate>

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:TeamEnova
ID: 24282254
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.
0
 
LVL 1

Author Comment

by:SaraDob
ID: 24282358
TeamEnova:
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?
Thanks

0
 
LVL 2

Accepted Solution

by:
TeamEnova earned 2000 total points
ID: 24284572
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.
*******************default.aspx***********************
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_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></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="btnSearch"
        runat="server" Text="Search" />
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" AutoGenerateDeleteButton="True" 
            AutoGenerateEditButton="True" DataKeyNames="ContactID" 
            DataSourceID="SqlDataSource1">
            <Columns>
                <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" />
            </Columns>
        </asp:GridView>
        <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)">
            <DeleteParameters>
                <asp:Parameter Name="ContactID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <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" />
            </UpdateParameters>
            <InsertParameters>
                <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" />
            </InsertParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>
 
*******************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
            GridView1.DataBind()
        End If
    End Sub
End Class

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

578 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