I am trying to work on Chapter 15 exercise from Murach’s ASP.NET 4.5 web programming with C# 2012.
Here is my question.
The webpage has Grid View on the left and Details View on the right.
When I update Ahmed to Ahmad from DetailViews,
the update is not reflected in GridView.
I have to go to the next page and then come back to previous page
in order for the update to display.
I am wondering if there is any solution.
Thank you for your help!
By the way, the code is copied from the book.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Chapter 15: Customer Maintenance</title>
<link href="Main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<img src="Images/banner.jpg" "Halloween Store" />
</header>
<section>
<form id="form1" runat="server">
<div id="gridview">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
DataKeyNames="Email" DataSourceID="SqlDataSource1"
SelectedIndex="0"
AutoGenerateColumns="False" CellPadding="4" GridLines="None"
ForeColor="Black" Width="320px" >
<Columns>
<asp:BoundField DataField="Email" HeaderText="Email"
ReadOnly="True" Visible="False">
</asp:BoundField>
<asp:BoundField DataField="LastName" HeaderText="LastName">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="FirstName" HeaderText="FirstName">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="120px" />
</asp:BoundField>
<asp:CommandField ButtonType="Button" ShowSelectButton="True" />
</Columns>
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="White" ForeColor="Black" />
<AlternatingRowStyle BackColor="#E3EAEB" ForeColor="Black" />
<SelectedRowStyle BackColor="#F46D11" ForeColor="White" />
<PagerSettings Mode="NextPreviousFirstLast" />
<PagerStyle BackColor="#1C5E55" ForeColor="White"
HorizontalAlign="Center" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:HalloweenConnectionString %>"
OldValuesParameterFormatString ="original_{0}"
ConflictDetection="CompareAllValues"
DeleteCommand="DELETE FROM [Customers]
WHERE [Email] = #original_Email
AND [LastName] = #original_LastName
AND [FirstName] = #original_FirstName
AND [Address] = #original_Address
AND [City] = #original_City
AND [State] = #original_State
AND [ZipCode] = #original_ZipCode
AND [PhoneNumber] = #original_PhoneNumber"
InsertCommand="INSERT INTO [Customers] ([Email], [LastName], [FirstName], [Address], [City], [State], [ZipCode], [PhoneNumber])
VALUES (#Email, #LastName, #FirstName, #Address, #City, #State, #ZipCode, #PhoneNumber)"
SelectCommand="SELECT [Email], [LastName], [FirstName], [Address], [City], [State], [ZipCode], [PhoneNumber]
FROM [Customers] WHERE ([Email] = #Email)"
UpdateCommand="UPDATE [Customers] SET [Email] = #Email,
[LastName] = #LastName,
[FirstName]= #FirstName,
[Address] = #Address, [City] = #City, [State] = #State, [ZipCode] = #ZipCode,
[PhoneNumber] = #PhoneNumber
WHERE [Email] = #original_Email
AND [LastName] = #original_LastName
AND [FirstName] = #original_FirstName
AND [Address] = #original_Address AND [City] = #original_City AND [State] = #original_State AND [ZipCode] = #original_ZipCode
AND [PhoneNumber] = #original_PhoneNumber">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Email" PropertyName="SelectedValue" Type="String"/>
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="original_Email" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_Address" Type="String" />
<asp:Parameter Name="original_City" Type="String" />
<asp:Parameter Name="original_State" Type="String" />
<asp:Parameter Name="original_ZipCode" Type="String" />
<asp:Parameter Name="original_PhoneNumber" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="State" Type="String" />
<asp:Parameter Name="ZipCode" Type="String" />
<asp:Parameter Name="PhoneNumber" Type="String" />
<asp:Parameter Name="original_Email" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_Address" Type="String" />
<asp:Parameter Name="original_City" Type="String" />
<asp:Parameter Name="original_State" Type="String" />
<asp:Parameter Name="original_ZipCode" Type="String" />
<asp:Parameter Name="original_PhoneNumber" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="State" Type="String" />
<asp:Parameter Name="ZipCode" Type="String" />
<asp:Parameter Name="PhoneNumber" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:HalloweenConnectionString %>"
SelectCommand="SELECT [Email], [LastName], [FirstName]
FROM [Customers] ORDER BY [LastName]">
</asp:SqlDataSource>
</div>
<div id="detailsview">
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="363px" AutoGenerateRows="False" DataKeyNames="Email" DataSourceID="SqlDataSource2" style="margin-left: 0px">
<Fields>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lblEmailIT" runat="server" Text='<%# Bind("Email") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblEmailET" runat="server" Text='<%# Bind("Email") %>'>
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtEmailInT" runat="server" Text='<%# Bind("Email") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtEmailInT" ErrorMessage="Email is a required field.">
</asp:RequiredFieldValidator>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="100px" />
<ItemStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName">
<ItemTemplate>
<asp:Label ID="lblLastNameIT" runat="server" Text='<%# Bind("LastName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblLastNameET" runat="server" Text='<%# Bind("LastName") %>'>
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="lblLastNameInT" runat="server" Text='<%# Bind("LastName") %>'>
</asp:TextBox>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="100px" />
<ItemStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstNameIT" runat="server" Text='<%# Bind("FirstName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblFirstNameET" runat="server" Text='<%# Bind("FirstName") %>'>
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="lblFirstNameInT" runat="server" Text='<%# Bind("FirstName") %>'>
</asp:TextBox>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="100px" />
<ItemStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblAddressIT" runat="server" Text='<%# Bind("Address") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblAddressET" runat="server" Text='<%# Bind("Address") %>'>
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="lblAddressInT" runat="server" Text='<%# Bind("Address") %>'>
</asp:TextBox>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="100px" />
<ItemStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCityIT" runat="server" Text='<%# Bind("City") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblCityET" runat="server" Text='<%# Bind("City") %>'>
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="lblCityInT" runat="server" Text='<%# Bind("City") %>'>
</asp:TextBox>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="100px" />
<ItemStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<asp:Label ID="lblStateIT" runat="server" Text='<%# Bind("State") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblStateET" runat="server" Text='<%# Bind("State") %>'>
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="lblStateInT" runat="server" Text='<%# Bind("State") %>'>
</asp:TextBox>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="100px" />
<ItemStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="ZipCode">
<ItemTemplate>
<asp:Label ID="lblZipCodeIT" runat="server" Text='<%# Bind("ZipCode") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblZipCodeET" runat="server" Text='<%# Bind("ZipCode") %>'>
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="lblZipCodeInT" runat="server" Text='<%# Bind("ZipCode") %>'>
</asp:TextBox>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="100px" />
<ItemStyle Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="PhoneNumber">
<ItemTemplate>
<asp:Label ID="lblPhoneNumberIT" runat="server" Text='<%# Bind("PhoneNumber") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblPhoneNumberET" runat="server" Text='<%# Bind("PhoneNumber") %>'>
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="lblPhoneNumberInT" runat="server" Text='<%# Bind("PhoneNumber") %>'>
</asp:TextBox>
</InsertItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="100px" />
<ItemStyle Width="200px" />
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowDeleteButton="true" ShowEditButton="true" ShowInsertButton="true" />
</Fields>
</asp:DetailsView>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
HeaderText="Please correct the following errors:" CssClass="error" />
<p>
<asp:Label ID = "lblError" runat="server" EnableViewState="False" CssClass="error">
</asp:Label>
</p>
</div>
</form>
</section>
</body>
</html>
Yes, in your behind code under the Page_Load method insert
if(!Page.IsPostBack)
{
GridView1.DataBind();
}
You have to use this event into this event you can bind the Gridview and to redirect the the same page your data will be reflected on your Gridview
Hope this will help try this....
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
if (e.AffectedRows == 1)
{
GridView1.DataBind();
Response.Redirect("Yourpage.aspx");
}
}
Related
I am using a GridView bound to a list of objects rather than a direct SQL query. The population of this if working and so are the updates. I have manually generated the insert for this gridview and this is working as well.
When trying to run the delete command from the gridview, the correct method is being called however none of the parameters are being populated. The only required parameter for this is #ID however, reading other posts, the general fix has been to add all parameters, but still to no avail.
<asp:GridView ID="GridViewBands" runat="server" AutoGenerateColumns="False" DataSourceID="BandsDataSource" CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True" Width="1375px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField HeaderText="AgencyID" SortExpression="AgencyID">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Width="80px" Text='<%# Bind("AgencyID") %>' ReadOnly ="true"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBox1" runat="server" ErrorMessage="there is an error?" Text="*`"></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Insert" Text="Add New Line" />
<asp:TextBox ID="newAgencyID" runat="server" Width="80px"></asp:TextBox>
<br />
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("AgencyID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TypeID" SortExpression="TypeID">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Width="80px" Text='<%# Bind("TypeID") %>' ReadOnly ="true"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newTypeID" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Width="80px" Text='<%# Bind("TypeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CompanyID" SortExpression="CompanyID">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Width="80px" Text='<%# Bind("CompanyID") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newCompanyID" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("CompanyID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Office" SortExpression="Office">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Width="80px" Text='<%# Bind("Office") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newOffice" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Office") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SequenceID" SortExpression="SequenceID" >
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Width="80px" Text='<%# Bind("SequenceID") %>' ReadOnly ="true"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newSequenceID" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("SequenceID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Minimmum" SortExpression="Minimmum">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Width="80px" Text='<%# Bind("Minimmum") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newMinimum" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Minimmum") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Capped" SortExpression="Capped">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Width="80px" Text='<%# Bind("Capped") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newCapped" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Capped") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="fixed" SortExpression="fixed">
<EditItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Width="80px" Text='<%# Bind("fixed") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newFixed" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("fixed") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Flat" SortExpression="Flat">
<EditItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Width="80px" Text='<%# Bind("Flat") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newFlat" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Flat") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SequenceLimit" SortExpression="SequenceLimit">
<EditItemTemplate>
<asp:TextBox ID="TextBox10" runat="server" Width="80px" Text='<%# Bind("SequenceLimit") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newSequenceLimit" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("SequenceLimit") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rate" SortExpression="Rate">
<EditItemTemplate>
<asp:TextBox ID="TextBox11" runat="server" Width="80px" Text='<%# Bind("Rate") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newRate" runat="server" Width="80px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("Rate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ActiveFrom" SortExpression="ActiveFrom">
<EditItemTemplate>
<asp:TextBox ID="txtActiveFrom" runat="server" Width="200px" Text='<%# Bind("ActiveFrom") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newActiveFrom" runat="server" Width="200px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblActiveFrom" runat="server" Text='<%# Bind("ActiveFrom") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ActiveTo" SortExpression="ActiveTo">
<EditItemTemplate>
<asp:TextBox ID="txtActiveTo" runat="server" Width="200px" Text='<%# Bind("ActiveTo") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="newActiveTo" runat="server" Width="200px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblActiveTo" runat="server" Text='<%# Bind("ActiveTo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<EditItemTemplate>
<asp:TextBox ID="txtID" runat="server" Width="80px" Text='<%# Bind("ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
The data source is as below, with the other working methods. It is just the delete method that isn't populating any of the parameters.
<asp:ObjectDataSource ID="BandsDataSource" runat="server" SelectMethod="SelectBands" TypeName="App_Code.Billing+BillingBand" UpdateMethod="UpdateBands" DeleteMethod="RemoveBand" InsertMethod="AddNewBand">
<SelectParameters>
<asp:Parameter Name ="AgencyID" Type ="Int64" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="AgencyID" Type="Object" />
<asp:Parameter Name="TypeID" Type="Object" />
<asp:Parameter Name="CompanyID" Type="Object" />
<asp:Parameter Name="Office" Type="Object" />
<asp:Parameter Name="SequenceID" Type="Object" />
<asp:Parameter Name="Minimmum" Type="Object" />
<asp:Parameter Name="Capped" Type="Object" />
<asp:Parameter Name="fixed" Type="Object" />
<asp:Parameter Name="Flat" Type="Object" />
<asp:Parameter Name="SequenceLimit" Type="Object" />
<asp:Parameter Name="Rate" Type="Object" />
<asp:Parameter Name="ActiveFrom" Type="Object" />
<asp:Parameter Name="ActiveTo" Type="Object" />
<asp:Parameter Name="ID" Type="Object" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="AgencyID" Type="Object" />
<asp:Parameter Name="TypeID" Type="Object" />
<asp:Parameter Name="CompanyID" Type="Object" />
<asp:Parameter Name="Office" Type="Object" />
<asp:Parameter Name="SequenceID" Type="Object" />
<asp:Parameter Name="Minimmum" Type="Object" />
<asp:Parameter Name="Capped" Type="Object" />
<asp:Parameter Name="fixed" Type="Object" />
<asp:Parameter Name="Flat" Type="Object" />
<asp:Parameter Name="SequenceLimit" Type="Object" />
<asp:Parameter Name="Rate" Type="Object" />
<asp:Parameter Name="ActiveFrom" Type="Object" />
<asp:Parameter Name="ActiveTo" Type="Object" />
<asp:Parameter Name="ID" Type="Object" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="AgencyID" Type="Object"/>
<asp:Parameter Name="TypeID" Type="Object" />
<asp:Parameter Name="CompanyID" Type="Object" />
<asp:Parameter Name="Office" Type="Object" />
<asp:Parameter Name="SequenceID" Type="Object" />
<asp:Parameter Name="Minimmum" Type="Object" />
<asp:Parameter Name="Capped" Type="Object" />
<asp:Parameter Name="fixed" Type="Object" />
<asp:Parameter Name="Flat" Type="Object" />
<asp:Parameter Name="SequenceLimit" Type="Object" />
<asp:Parameter Name="Rate" Type="Object" />
<asp:Parameter Name="ActiveFrom" Type="Object" />
<asp:Parameter Name="ActiveTo" Type="Object" />
<asp:Parameter Name="ID" Type="Object" />
</UpdateParameters>
</asp:ObjectDataSource>
I have tried to manually populate the parameters being using similar to the below, however I have not been able to find a way to populate the parameter based on the 'selected' line for deletion.
Protected Sub BandsDataSource_Deleting(sender As Object, e As ObjectDataSourceMethodEventArgs) Handles BandsDataSource.Deleting
e.InputParameters("ID") = GridViewBands.SelectedRow.ID.ToString()
End Sub
But this is returning a NULL reference exception.
This is the delete method for example, the update and add methods are in the same format and are working correctly.
Public Sub RemoveBand(AgencyID, TypeID, CompanyID, Office, SequenceID, Minimmum, Capped, fixed, Flat, SequenceLimit, Rate, ActiveFrom, ActiveTo, ID)
Using lCon As SqlConnection = Database.DBConnection
Dim lSqlText As String = "update billing_bands set active_to = getdate() where id = #id"
Dim lSqlCmd As New SqlCommand(lSqlText, lCon)
With lSqlCmd.Parameters
.AddWithValue("#id", ID)
End With
lSqlCmd.ExecuteNonQuery()
End Using
End Sub
Had to add into the GridView the onRowDeleting param which has to set the default values for the variables used for the delete.
<asp:GridView ID="GridViewBands" runat="server" AutoGenerateColumns="False" DataSourceID="BandsDataSource" OnRowDeleting="test" CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True" Width="1375px">
<AlternatingRowStyle BackColor="White" />
<Columns>
Protected Sub test(sender As Object, e As GridViewDeleteEventArgs)
Dim thisID As Integer = e.Values(13)
BandsDataSource.DeleteParameters("ID").DefaultValue = thisID
BandsDataSource.Delete()
BandsDataSource.DataBind()
BandsDataSource.Dispose()
End Sub
I have a field named status in my gridview i want it like that so that when user clickes update on gridview in the place of textfield there will be an dropdown with published and unpublished option.here is the code
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource3">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="info" HeaderText="info" SortExpression="info" />
<asp:BoundField DataField="productcode" HeaderText="productcode" SortExpression="productcode" />
<asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" DeleteCommand="DELETE FROM [product_tbl] WHERE [Id] = #Id" InsertCommand="INSERT INTO [product_tbl] ([Id], [subcat], [name], [info], [brandid], [gender]) VALUES (#Id, #subcat, #name, #info, #brandid, #gender)" SelectCommand="SELECT * FROM [product_tbl]" UpdateCommand="UPDATE [product_tbl] SET [subcat] = #subcat, [name] = #name, [info] = #info, [brandid] = #brandid, [productcode] = #productcode, [gender] = #gender WHERE [Id] = #Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="subcat" Type="Int32" />
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="info" Type="String" />
<asp:Parameter Name="brandid" Type="Int32" />
<asp:Parameter Name="gender" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="subcat" Type="Int32" />
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="info" Type="String" />
<asp:Parameter Name="brandid" Type="Int32" />
<asp:Parameter Name="productcode" Type="Int32" />
<asp:Parameter Name="gender" Type="Int32" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
For dropdown list you need to place templatefield
<asp:TemplateField HeaderText="Status">
<EditItemTemplate>
<asp:DropDownList ID="ddlStautus" runat="server">
<asp:ListItem>published</asp:ListItem>
<asp:ListItem>unpublished</asp:ListItem>
</asp:DropDowList>
</asp:TempLateFeild>
and bind your gridview on code behind
for more reference use This link
OR
<asp:GridView ID="GridView1" runat="server" CellPadding="4" DataKeyNames="UserID"
ForeColor="#333333" GridLines="None"
AutoGenerateColumns="False" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating1"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdatabound="GridView1_RowDataBound1"
onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="id" Visible="False">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Width="75px"
Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server"
Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Info">
<EditItemTemplate>
<asp:TextBox ID="txtInfo" runat="server" Width="75px"
Text='<%# Bind("Info") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblInfo" runat="server"
Text='<%# Bind("Info") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="productcode">
<EditItemTemplate>
<asp:TextBox ID="txtproductcode" runat="server" Width="75px"
Text='<%# Bind("DomainID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblproductcode" runat="server"
Text='<%# Bind("productcode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "status">
<ItemTemplate>
<asp:Label ID="lblRole" runat="server" Text='<%# Bind("status") %>' Visible="false"/>
<asp:DropDownList ID="ddlRole" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True"/>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<span onclick="return confirm('Are you sure to Delete the record?')">
<asp:LinkButton ID="lnkB" runat="Server" Text="Delete" CommandName="Delete"></asp:LinkButton>
</span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and on code behind use code which is given in the link
i need help to look my code. It's seem not update the data. If default create it can successfuly update. When i put some code for auto value in detailview textbox it will go back to before update since the update not success i think.
ASP.NET code :
Please Scan Your Student Card<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1"
runat="server" Text="Enter" /><br />
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
DataKeyNames="IDCardNo" DataSourceID="SqlDataSource1" Height="50px" Width="541px" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
<Fields>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="IDCardNo" SortExpression="IDCardNo">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("IDCardNo") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("IDCardNo") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("IDCardNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StudentNo" SortExpression="StudentNo">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("StudentNo") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("StudentNo") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("StudentNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TimeInClass" SortExpression="TimeInClass">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("TimeInClass") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("TimeInClass") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("TimeInClass") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TimeOutClass" SortExpression="TimeOutClass">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("TimeOutClass") %>'
OnDataBinding="TextBox1_DataBinding">
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TimeOutClass") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("TimeOutClass") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="New"
Text="New"></asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Student] WHERE [IDCardNo] = #original_IDCardNo AND (([Name] = #original_Name) OR ([Name] IS NULL AND #original_Name IS NULL)) AND (([StudentNo] = #original_StudentNo) OR ([StudentNo] IS NULL AND #original_StudentNo IS NULL)) AND (([TimeInClass] = #original_TimeInClass) OR ([TimeInClass] IS NULL AND #original_TimeInClass IS NULL)) AND (([TimeOutClass] = #original_TimeOutClass) OR ([TimeOutClass] IS NULL AND #original_TimeOutClass IS NULL))"
InsertCommand="INSERT INTO [Student] ([Name], [IDCardNo], [StudentNo], [TimeInClass], [TimeOutClass]) VALUES (#Name, #IDCardNo, #StudentNo, #TimeInClass, #TimeOutClass)"
OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Student] WHERE ([IDCardNo] = #IDCardNo)"
UpdateCommand="UPDATE [Student] SET [Name] = #Name, [StudentNo] = #StudentNo, [TimeInClass] = #TimeInClass, [TimeOutClass] = #TimeOutClass WHERE [IDCardNo] = #original_IDCardNo AND (([Name] = #original_Name) OR ([Name] IS NULL AND #original_Name IS NULL)) AND (([StudentNo] = #original_StudentNo) OR ([StudentNo] IS NULL AND #original_StudentNo IS NULL)) AND (([TimeInClass] = #original_TimeInClass) OR ([TimeInClass] IS NULL AND #original_TimeInClass IS NULL)) AND (([TimeOutClass] = #original_TimeOutClass) OR ([TimeOutClass] IS NULL AND #original_TimeOutClass IS NULL))">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="IDCardNo" PropertyName="Text" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="original_IDCardNo" Type="String" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_StudentNo" Type="String" />
<asp:Parameter Name="original_TimeInClass" Type="String" />
<asp:Parameter Name="original_TimeOutClass" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="StudentNo" Type="String" />
<asp:Parameter Name="TimeInClass" Type="String" />
<asp:Parameter Name="TimeOutClass" Type="String" />
<asp:Parameter Name="original_IDCardNo" Type="String" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_StudentNo" Type="String" />
<asp:Parameter Name="original_TimeInClass" Type="String" />
<asp:Parameter Name="original_TimeOutClass" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="IDCardNo" Type="String" />
<asp:Parameter Name="StudentNo" Type="String" />
<asp:Parameter Name="TimeInClass" Type="String" />
<asp:Parameter Name="TimeOutClass" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
VB code
Protected Sub TextBox1_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim TextToTime As New TextBox
TextToTime = DetailsView1.FindControl("TextBox1")
TextToTime.Text = DateTime.Now
End Sub
It's starting not update when i use code (OnDataBinding="TextBox1_DataBinding") in EditItemTemplate Textbox1 on TimeOutClass databind.
Remove OnDataBinding="TextBox1_DataBinding">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("TimeOutClass") %>'
OnDataBinding="TextBox1_DataBinding">
</asp:TextBox>
</EditItemTemplate>
Change to OnPreRender ="TextBox1_DataBinding">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("TimeOutClass") %>'
OnPreRender ="TextBox1_DataBinding">
</asp:TextBox>
</EditItemTemplate>
That should help.
When I click Edit on a row in the gridview, it shows the data like it should. It shows the text boxes where I expect to see them and it allows me to modify the data in the box. When I click Update on that row it does the same thing as cancel; the page refills without saving any data changes. The database is also unchanged.
<%# Page Title="Employee Lookup"Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="EmployeeLookup.aspx.cs" Inherits="WSC.EmployeeLookup" %>
<p>
<asp:Table ID="Table1" runat="server" CellPadding="5" Width="442px">
<asp:TableRow>
<asp:TableCell><asp:Button ID="NewUserButton" PostBackUrl="~/EmployeeInfo.aspx" runat="server" Text="New Employee" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="eID"
AutoGenerateEditButton="True" DataSourceID="AccessDataSource2"
EmptyDataText="There are no data records to display." AllowSorting="True" AllowPaging="True">
<Columns>
<asp:BoundField DataField="eID" HeaderText="Employee ID" ReadOnly="True" SortExpression="eID" />
<asp:BoundField DataField="eFirstName" HeaderText="FirstName" SortExpression="eFirstName" />
<asp:BoundField DataField="eLastName" HeaderText="LastName" SortExpression="eLastName" />
<asp:TemplateField HeaderText="JobTitle" SortExpression="eJobTitle">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" width="50" runat="server" Text='<%# Bind("eJobTitle") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("eJobTitle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dept Code" SortExpression="deptNumber">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" width="50" runat="server" Text='<%# Bind("deptNumber") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("deptNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName" SortExpression="eUserName">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" width="50" runat="server" Text='<%# Bind("eUserName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("eUserName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" SortExpression="ePassword">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" width="70" runat="server" Text='<%# Bind("ePassword") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text="*********"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="eEmail">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("eEmail") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("eEmail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hire Date" SortExpression="eDateHire">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("eDateHire") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("eDateHire") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="securityID" SortExpression="securityID">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("securityID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("securityID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="ErrorMessageLabel" runat="server" Text="" ></asp:Label>
<asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="C:\WSC\WSC\WSC\CIS470_Database.accdb"
SelectCommand="SELECT `eID`, `eFirstName`, `eLastName`, `eJobTitle`, `deptNumber`, `eUserName`, `ePassword`, `eEmail`, `eDateHire`, `securityID` FROM `Employee`"
UpdateCommand="UPDATE `Employee` SET `eFirstName` = ?, `eLastName` = ?, `eJobTitle` = ?, `deptNumber` = ?, `eUserName` = ?, `ePassword` = ?, `eEmail` = ?, `eDateHire` = ?, `securityID` = ? WHERE `eID` = ?">
<DeleteParameters>
<asp:Parameter Name="eID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="eFirstName" Type="String" />
<asp:Parameter Name="eLastName" Type="String" />
<asp:Parameter Name="eJobTitle" Type="String" />
<asp:Parameter Name="deptNumber" Type="String" />
<asp:Parameter Name="eUserName" Type="String" />
<asp:Parameter Name="ePassword" Type="String" />
<asp:Parameter Name="eEmail" Type="String" />
<asp:Parameter Name="ePhone" Type="String" />
<asp:Parameter Name="eDateHire" Type="DateTime" />
<asp:Parameter Name="eDateTerm" Type="DateTime" />
<asp:Parameter Name="securityID" Type="Int32" />
</UpdateParameters>
</asp:AccessDataSource>
</p>
Add:
<asp:Parameter Name="eID" Type="Int32" />
To:
<UpdateParameters>
<asp:Parameter Name="eFirstName" Type="String" />
<asp:Parameter Name="eLastName" Type="String" />
<asp:Parameter Name="eJobTitle" Type="String" />
<asp:Parameter Name="deptNumber" Type="String" />
<asp:Parameter Name="eUserName" Type="String" />
<asp:Parameter Name="ePassword" Type="String" />
<asp:Parameter Name="eEmail" Type="String" />
<asp:Parameter Name="ePhone" Type="String" />
<asp:Parameter Name="eDateHire" Type="DateTime" />
<asp:Parameter Name="eDateTerm" Type="DateTime" />
<asp:Parameter Name="securityID" Type="Int32" />
</UpdateParameters>
And use:
eFirstName = #eFirstName, ... WHERE eID = #eID"
instead of what you are using in your UpdateCommand. The problem is you parameters must start with # in your update and deletecommand. Then they will much the parameters between
<DeleteParameters></DeleteParameters>
And
<UpdateParameters></UpdateParameters>
I have a gridview that uses accessdatasource which has select, insert, delete and update. It used to work before perfectly but all of a sudden the update stopped working while the rest are still working as they supposed to be. I haven't changed anything in the gridview or datasource, but I added new fields to the table but are not displayed in gridview. I tested by deleting the new fields from the table which did not work either. Refreshed the datasoure and gridview as well. Here is the code
asp:GridView ID="GridView1" runat="server" AllowSorting="True"
ShowFooter="True" EmptyDataText="There are no records for the selection"
AutoGenerateColumns="False" CellPadding="4" OnRowCommand="Insert_Click"
OnRowUpdating="beforeUpdate"
DataKeyNames="Lab Name,Target,Device Name"
ForeColor="#333333" HorizontalAlign="Center"
EmptyDataRowStyle-HorizontalAlign="Center"
EmptyDataRowStyle-VerticalAlign="Middle" Width="1333px"
DataSourceID="AccessDataSource8">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EmptyDataRowStyle Wrap="False" />
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure?');" ></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton" runat="server" CommandName="Insert" Text="Insert" ValidationGroup="validate" ></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lab Name" SortExpression="Lab Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("[Lab Name]") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[Lab Name]") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLabname" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="LabName" runat="server" ControlToValidate="txtLabname" Text="*" ValidationGroup="validate"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Target" SortExpression="Target">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Target") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Target") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTarget" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="Target" runat="server" ControlToValidate="txtTarget" Text="*" ValidationGroup="validate"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Device Name" SortExpression="Device Name">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("[Device Name]") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("[Device Name]") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDevicename" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="Devicename" runat="server" ControlToValidate="txtDevicename" Text="*" ValidationGroup="validate"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="IO Type Requirement"
SortExpression="IO Type Requirement">
<ItemTemplate>
<asp:Label ID="Label4" runat="server"
Text='<%# Bind("[IO Type Requirement]") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("[IO Type Requirement]") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtIotyperequirement" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="Iotyperequirement" runat="server" ControlToValidate="txtIotyperequirement" Text="*" ValidationGroup="validate"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Part Number" SortExpression="Part Number">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("[Part Number]") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("[Part Number]") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPartnumber" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="Partnumber" runat="server" ControlToValidate="txtPartnumber" Text="*" ValidationGroup="validate"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub Part" SortExpression="Sub Part">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("[Sub Part]") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("[Sub Part]") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSubpart" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="Subpart" runat="server" ControlToValidate="txtSubpart" Text="*" ValidationGroup="validate"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subpart Description"
SortExpression="Subpart Description">
<ItemTemplate>
<asp:Label ID="Label7" runat="server"
Text='<%# Bind("[Subpart Description]") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"
Text='<%# Bind("[Subpart Description]") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSubpartdesc" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="Subpartdesc" runat="server" ControlToValidate="txtSubpartdesc" Text="*" ValidationGroup="validate"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" VerticalAlign="Middle" Wrap="False" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775"
VerticalAlign="Middle" Wrap="False" />
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource8" runat="server" CancelSelectOnNullParameter="False"
DataFile="~/App_Data/Test.mdb"
SelectCommand="SELECT Lab1.[Lab Name], Lab1.[Target], Lab1.[Device Name], Lab1.[IO Type Requirement], Lab1.[Part Number], Lab1.[Sub Part], Lab1.[Subpart Description]
FROM Lab1
WHERE
(((Lab1.[Lab Name])= [WhichLab] OR [WhichLab] = '0') AND
((Lab1.[Target])= [WhichTarget] OR [WhichTarget] = '0') AND
((Lab1.[Device Name])= [WhichDevice] OR [WhichDevice] = '0') AND
((Lab1.[IO Type Requirement])= [Whichiotyperequirement] OR [Whichiotyperequirement] = '0') AND
((Lab1.[Part Number])= [WhichPartNumber] OR [WhichPartNumber] = '0') AND
((Lab1.[Sub Part])= [WhichSubPart] OR [WhichSubPart] = '0') AND
((Lab1.[Subpart Description])= [WhichSubPartDesc] OR [WhichSubPartDesc] = '0')
)" UpdateCommand= " UPDATE [Lab1] SET [IO Type Requirement]= ?, [Part Number]= ?, [Sub Part]= ?, [Subpart Description]= ?
WHERE ([Lab Name]= ?) AND ([Target]= ?) AND ([Device Name] = ?)"
InsertCommand= "INSERT INTO LAB1 ([Lab Name],[Target],[Device Name],[IO Type Requirement],[Part Number],[Sub Part],[Subpart Description]) VALUES
([WhichLab],[WhichTarget],[WhichDevice],[Whichiotyperequirement],[WhichPartNumber],[WhichSubPart],[WhichSubPartDesc]) "
DeleteCommand= " Delete from LAB1 WHERE LAB1.[Lab Name]= [WhichLab] AND Lab1.[Target]=[WhichTarget] AND Lab1.[Device Name]=[WhichDevice]">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Lab Name" ConvertEmptyStringToNull="true" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList2" Name="Target" ConvertEmptyStringToNull="true" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList3" Name="Device Name" ConvertEmptyStringToNull="true" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList4" Name="IO Type Requirement" ConvertEmptyStringToNull="true" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList5" Name="Part Number" ConvertEmptyStringToNull="true" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList6" Name="Sub Part" ConvertEmptyStringToNull="true" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList7" Name="Subpart Description" ConvertEmptyStringToNull="true" PropertyName="SelectedValue" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="IO Type Requirement" Type="String"/>
<asp:Parameter Name="Part Number" Type="String"/>
<asp:Parameter Name="Sub Part" Type="String" />
<asp:Parameter Name="Subpart Description" Type="String"/>
<asp:Parameter Name="Lab Name" Type="String"/>
<asp:Parameter Name="Target" Type="String"/>
<asp:Parameter Name="Device Name" Type="String"/>
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Lab Name" Type="String" />
<asp:Parameter Name="Target" Type="String" />
<asp:Parameter Name="Device Name" Type="String" />
<asp:Parameter Name="IO Type Requirement" Type="String" />
<asp:Parameter Name="Part Number" Type="String" />
<asp:Parameter Name="Sub Part" Type="String" />
<asp:Parameter Name="Subpart Description" Type="String" />
</InsertParameters>
<DeleteParameters>
<asp:Parameter Name="Lab Name" Type="String" />
<asp:Parameter Name="Target" Type="String" />
<asp:Parameter Name="Device Name" Type="String" />
</DeleteParameters>
</asp:AccessDataSource>
One of the fields in your WHERE clause:
WHERE ([Lab Name]= ?) AND ([Target]= ?) AND ([Device Name] = ?)"
...is not matching records. Maybe there are trailing spaces in one or more of the fields.
"ABC" will not match "ABC "