Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Edit Gridview Bound to dataset??

Posted on 2007-10-04
9
Medium Priority
?
1,033 Views
Last Modified: 2013-11-26
Trying to use the AutGenerate Edit feature in VS05.
It appears on the grid, but when I EDIT a column and press UPDATE it just returns to the state it was in.
The Delete works, but not Edit/Update.

CODE:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="AddGoal.aspx.vb" Inherits="AddGoal" %>

<!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>
        &nbsp;</div>
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
            AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" BackColor="White"
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1"
            GridLines="Vertical" HorizontalAlign="Center">
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <Columns>
                <asp:BoundField DataField="CurrentMonth" HeaderText="CurrentMonth" SortExpression="CurrentMonth" />
                <asp:BoundField DataField="StoreNum" HeaderText="StoreNum" SortExpression="StoreNum" />
                <asp:BoundField DataField="StoreName" HeaderText="StoreName" SortExpression="StoreName" />
                <asp:BoundField DataField="SalesGoal" HeaderText="SalesGoal" SortExpression="SalesGoal" DataFormatString="{0:F2}" HtmlEncode="False" />
                <asp:BoundField DataField="EFTGoal" HeaderText="EFTGoal" SortExpression="EFTGoal" DataFormatString="{0:F2}" HtmlEncode="False" />
            </Columns>
            <RowStyle BackColor="#EEEEEE" Font-Names="Tahoma" Font-Size="Smaller" ForeColor="Black"
                Wrap="False" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" Font-Names="Tahoma" Font-Size="Small"
                ForeColor="White" Wrap="False" />
            <AlternatingRowStyle BackColor="Gainsboro" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CorpWebConnectionString %>"
            SelectCommand="SELECT * FROM [NumbersGoals]" DeleteCommand="DELETE FROM [NumbersGoals] WHERE [CurrentMonth] = @original_CurrentMonth AND [StoreNum] = @original_StoreNum" InsertCommand="INSERT INTO [NumbersGoals] ([CurrentMonth], [StoreNum], [StoreName], [SalesGoal], [EFTGoal]) VALUES (@CurrentMonth, @StoreNum, @StoreName, @SalesGoal, @EFTGoal)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [NumbersGoals] SET [StoreName] = @StoreName, [SalesGoal] = @SalesGoal, [EFTGoal] = @EFTGoal WHERE [CurrentMonth] = @original_CurrentMonth AND [StoreNum] = @original_StoreNum">
            <DeleteParameters>
                <asp:Parameter Name="original_CurrentMonth" Type="String" />
                <asp:Parameter Name="original_StoreNum" Type="String" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="StoreName" Type="String" />
                <asp:Parameter Name="SalesGoal" Type="Decimal" />
                <asp:Parameter Name="EFTGoal" Type="Decimal" />
                <asp:Parameter Name="original_CurrentMonth" Type="String" />
                <asp:Parameter Name="original_StoreNum" Type="String" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="CurrentMonth" Type="String" />
                <asp:Parameter Name="StoreNum" Type="String" />
                <asp:Parameter Name="StoreName" Type="String" />
                <asp:Parameter Name="SalesGoal" Type="Decimal" />
                <asp:Parameter Name="EFTGoal" Type="Decimal" />
            </InsertParameters>
        </asp:SqlDataSource>
    </form>
</body>
</html>

TIA,
Andrew
0
Comment
Question by:Ahelbling
  • 5
  • 4
9 Comments
 
LVL 12

Accepted Solution

by:
madhevan_pillai earned 2000 total points
ID: 20014313
Hi,

 You have not set DataKeyNames attribute of the grid. DataKeyNames  is the primary key field
0
 
LVL 12

Expert Comment

by:madhevan_pillai
ID: 20014320
Hi

see the sample below where Id is the primary key

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="EditDeleteGrid.aspx.vb" Inherits="Database_EditDeleteGrid" %>

<!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" AutoGenerateDeleteButton="True"
            AutoGenerateEditButton="True" AutoGenerateSelectButton="True" DataKeyNames="Id" DataSourceID="SqlDataSource1" >
            <Columns>
           <asp:BoundField DataField="Id" />
                <asp:BoundField DataField="Name" />
                <asp:BoundField DataField="Age" />
            </Columns>
        </asp:GridView>
   
    </div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString3 %>"
            SelectCommand="SELECT * FROM [Table3]" UpdateCommand="UPDATE Table3 SET Name =@Name, Age =@Age WHERE Id=@Id" InsertCommand="INSERT INTO Table3(Name, Age) VALUES (@Name,@Age)" DeleteCommand="DELETE FROM Table3 WHERE (Id = @Id)">
           
        </asp:SqlDataSource>
    </form></body>
</html>
0
 

Author Comment

by:Ahelbling
ID: 20014333
i have multiple keys - CurrentMonth AND StoreNum
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:Ahelbling
ID: 20014355
ok I added both as DatakeyNames... it will let me edit all fields except the two I added as DataKeyNames?  How can I Edit those as well?
0
 

Author Comment

by:Ahelbling
ID: 20014399
nevermind - I dont need to change the keyed fields....How can I have EDIT and DELETE in 2 seperate columns instead of both of them in one as they are now??
0
 
LVL 12

Expert Comment

by:madhevan_pillai
ID: 20014420
In that case use a autogenerated column.
0
 

Author Comment

by:Ahelbling
ID: 20014451
I set that to True and it doubled up the current columns and left EDIT and DELETE in 1 column??

        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="True"
            AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" BackColor="White"
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1"
            GridLines="Vertical" HorizontalAlign="Center" DataKeyNames="CurrentMonth,StoreNum">
0
 
LVL 12

Expert Comment

by:madhevan_pillai
ID: 20014729
what i said is a unique single column from the database. Autoseed column from the database.if so u can edit CurrentMonth,StoreNum
0
 
LVL 12

Expert Comment

by:madhevan_pillai
ID: 20014741
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
            AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" BackColor="White"
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1"
            GridLines="Vertical" HorizontalAlign="Center" DataKeyNames="Id"> where Id is a unique column from th database
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

864 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