i have attached my gridview with id= "project_update_GridView" and Datasource. delete command works good but update command is not working.
some additional information:
This gridview is placed in a Modal Popup
All edit,update and delete command works well in empty webform but it doesn't works in my main solution webform
<div id="project_update_panel" class="popup " style="margin-left: 40px">
<label runat="server" id="project_overview"><i class='bx bx-sort-up'></i>Update Project<i id="close_popup_projectUP" class='bx bx-x-circle update-project-popup'></i></label>
<asp:Panel ID="project_update_popup_panel" runat="server" ScrollBars="Auto" CssClass="update-project-panel">
<%--update project Data source--%>
<asp:SqlDataSource ID="project_update_DS" runat="server" ConnectionString="<%$ ConnectionStrings:Timesheet_DBConnectionString %>" SelectCommand="SELECT [id], [employee_id], [project_id], [project_name], [product_name], [client_name], [project_team], [start_date], [project_team_email_id], [project_status] FROM [project_details] WHERE (([employee_id] = #employee_id) AND ([project_type] = #project_type))" DeleteCommand="DELETE FROM [project_details] WHERE [id] = #original_id" InsertCommand="INSERT INTO [project_details] ([employee_id], [project_id], [project_name], [product_name], [client_name], [project_team], [start_date], [project_team_email_id], [project_status]) VALUES (#employee_id, #project_id, #project_name, #product_name, #client_name, #project_team, #start_date, #project_team_email_id, #project_status)" UpdateCommand="UPDATE [project_details] SET [id]=#id [employee_id] = #employee_id, [project_id] = #project_id, [project_name] = #project_name, [product_name] = #product_name, [client_name] = #client_name, [project_team] = #project_team, [start_date] = #start_date, [project_team_email_id] = #project_team_email_id, [project_status] = #project_status WHERE [id] = #original_id">
<SelectParameters>
<asp:ControlParameter ControlID="employee_id" PropertyName="Text" Name="employee_id" Type="String"></asp:ControlParameter>
<asp:Parameter DefaultValue="individual" Name="project_type" Type="String"></asp:Parameter>
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="String"></asp:Parameter>
<asp:Parameter Name="project_id" Type="String"></asp:Parameter>
<asp:Parameter Name="project_name" Type="String"></asp:Parameter>
<asp:Parameter Name="product_name" Type="String"></asp:Parameter>
<asp:Parameter Name="client_name" Type="String"></asp:Parameter>
<asp:Parameter Name="project_team" Type="String"></asp:Parameter>
<asp:Parameter DbType="DateTime2" Name="start_date"></asp:Parameter>
<asp:Parameter Name="project_team_email_id" Type="String"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<%--update project main content--%>
<asp:UpdatePanel runat="server" UpdateMode= "Conditional">
<ContentTemplate>
<asp:GridView ID="project_update_GridView" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="project_update_DS" CssClass="main-grid-table" HeaderStyle-CssClass="main-grid-header" RowStyle-CssClass="main-grid-row" AutoGenerateColumns="False" DataKeyNames="id" OnRowUpdating="project_update_GridView_RowUpdating" ViewStateMode="Inherit" SortedAscendingHeaderStyle-Wrap="True" EnableViewState="False" EnableModelValidation="False">
<Columns>
<asp:TemplateField HeaderText="<i class='bx bxs-sticker'></i>">
<ItemTemplate>
<input type="checkbox" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="ID" ReadOnly="True" InsertVisible="False" SortExpression="id"></asp:BoundField>
<asp:BoundField DataField="employee_id" HeaderText="Employee ID" SortExpression="employee_id" ReadOnly="true" />
<asp:BoundField DataField="project_id" HeaderText="Project ID" SortExpression="project_id" ReadOnly="true" />
<asp:BoundField DataField="project_name" HeaderText="Project Name" SortExpression="project_name" />
<asp:BoundField DataField="product_name" HeaderText="Product Name" SortExpression="product_name" />
<asp:BoundField DataField="client_name" HeaderText="Client Name" SortExpression="client_name" />
<asp:BoundField DataField="project_team" HeaderText="Team" SortExpression="project_team" />
<asp:BoundField DataField="start_date" HeaderText="Date Started" SortExpression="start_date" />
<asp:BoundField DataField="project_team_email_id" HeaderText="Team Email" SortExpression="project_team_email_id" />
<asp:TemplateField HeaderText="Status" SortExpression="Status" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<i class='bx bxs-circle' style='color: #01bf00; display: contents; font-size: x-small;'></i></span><asp:Label ID="status" Style="margin-left: 3px;" runat="server" Text='<%#Bind("project_status")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Action" ShowSelectButton="false" ShowEditButton="True" EditText="<i class='bx bx-edit'></i>" UpdateText="<i class='bx bx-upload'></i>" CancelText="<i class='bx bx-x-circle'></i>"/>
</Columns>
<EmptyDataTemplate>
<div class="update-default-content" id="update_default_content" runat="server">
<asp:ImageButton runat="server" src="IMG/Create_project_icon.png" ID="update_create_icon" Style="filter: contrast(0.1%);" OnClientClick="javascript:$find('ModalPopupExtender2').hide();$find('create_project_icon').show(); return false;" /><br />
<span>Create Project</span>
</div>
</EmptyDataTemplate>
<HeaderStyle CssClass="main-grid-header" />
<RowStyle CssClass="main-grid-row" />
<SortedDescendingCellStyle />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</div>
Related
I have a gridview control on page data is showing perfectly fine. I add edit button in row to update record its also working fine until I add another link in next column which redirect me to new page.
Here is my Gridview code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" DataSourceID="SqlDataSource1" GridLines="None" DataKeyNames="issue_id,pro_id" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="Key#" InsertVisible="False" SortExpression="issue_id">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("issue_id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="view" CommandArgument ='<%# Eval("issue_id") %>' Text='<%# Eval("issue_id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="pro_id" HeaderText="pro_id" SortExpression="pro_id" ReadOnly="True" Visible="False" />
<asp:BoundField DataField="name" HeaderText="Project Name" SortExpression="name" />
<asp:BoundField DataField="type" HeaderText="Issue Type" SortExpression="type" />
<asp:BoundField DataField="summary" HeaderText="Summary" SortExpression="summary" />
<asp:BoundField DataField="mem_id" HeaderText="Member Name" SortExpression="mem_id" />
<asp:TemplateField HeaderText="Priority" SortExpression="priority">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Major</asp:ListItem>
<asp:ListItem>Critical</asp:ListItem>
<asp:ListItem>Minor</asp:ListItem>
<asp:ListItem>Cosmetic</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("priority") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="status" HeaderText="Status" SortExpression="status" />
<asp:BoundField DataField="impact" HeaderText="Impact" SortExpression="impact" />
<asp:BoundField DataField="Expr1" HeaderText="Submit Date" ReadOnly="True" SortExpression="Expr1" />
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:IHDConnectionString %>"
SelectCommand="SELECT issue.issue_id, issue.pro_id, project.name, issue.type, issue.summary, issue.mem_id, issue.priority, issue.status, issue.impact, CONVERT (date, GETDATE()) AS Expr1 FROM issue INNER JOIN project ON issue.pro_id = project.pro_id"
UpdateCommand="UPDATE [issue] SET [mem_id] = #mem_id, [priority] = #priority, [status] = #status WHERE [issue_id] = #issue_id AND [pro_id] = #pro_id"
<UpdateParameters>
<asp:Parameter Name="mem_id" Type="String" />
<asp:Parameter Name="priority" Type="String" />
<asp:Parameter Name="status" Type="String" />
<asp:Parameter Name="issue_id" Type="Int32" />
<asp:Parameter Name="pro_id" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
here is my redirect page control
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Response.Redirect("~/redirect.aspx?view=" + e.CommandArgument);
}
Now problem is when I click edit it redirect me to another while it is suppose to edit the record in same page.
Change your Row_Command to this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName.ToString() == "view")
{
Response.Redirect("~/redirect.aspx?view=" + e.CommandArgument);
}
}
I would recommend using a link when you need a link, you don't need to do postback to "redirect" the user:
<asp:TemplateField HeaderText="Key#" InsertVisible="False" SortExpression="issue_id">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("issue_id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<a href='redirect.aspx?view=<%# Eval("issue_id") %>'><%# Eval("issue_id") %></a>
</ItemTemplate>
</asp:TemplateField>
I'm new to Asp.net and Telerik Control.
I'm using Radgrid to bind a View with schemabinding in SQL Server. And I want to use Telerik Radgrid AllowAutomaticInserts/updates/deletes functions. but there is no result return to database and no error occured. When should I use Insertcommand in code-behind? How to debug aspx file as following at runtime.
Thanks in advance.
Here is the code.
<asp:Panel ID="Project" runat="server" GroupingText="Project" CssClass="inlineBlock" Width="1600px" Font-Names="Microsoft YaHei UI"
BackColor ="#99ff99" BorderStyle="Double">
<asp:Label Text="CompanyName" runat="server" Font-Size="Small"></asp:Label>
<telerik:RadDropDownList ID="RadDropDownListCompany" runat="server" Skin="Windows7" Font-Size="Small" AutoPostBack="true" OnSelectedIndexChanged="RadDropDownListCompany_SelectedIndexChanged"
></telerik:RadDropDownList>
<asp:Label Text="Projectname" runat="server" Font-Size="Small"></asp:Label>
<telerik:RadDropDownList ID="RadDropDownListProject" runat="server" Skin="Windows7" Font-Size="Small" ></telerik:RadDropDownList>
<telerik:RadButton ID="btnQuery" runat="server" Text="Confirm" Skin="Windows7" Font-Size="Small" OnClick="btnQuery_Click" ></telerik:RadButton>
</asp:Panel>
<asp:Panel ID="PanelOp" runat="server" Visible="false" >
<asp:Label Text="LandBlock" runat="server" Font-Size="Medium" Font-Names="Microsoft YaHei UI"></asp:Label>
<telerik:RadDropDownList ID="RadDropDownListLandBlock" runat="server" Skin="Windows7" Font-Size="Small" Font-Names="Microsoft YaHei UI"></telerik:RadDropDownList>
<telerik:RadButton ID="radbtnLandBlockQuery" runat="server" Text="Confirm" Skin="Windows7" Font-Size="Small" OnClick="radbtnLandBlockQuery_Click" ></telerik:RadButton>
<telerik:RadGrid ID="RadGridLandBlockVoucher" runat="server" Skin="Windows7" Font-Size="Small" Font-Names="Microsoft YaHei UI" Culture="zh-CN"
Height="600px" Width="1600px" GroupPanelPosition="Top" ShowFooter ="True" EnableViewState="true" AutoGenerateEditColumn="true" AutoGenerateDeleteColumn="true"
AllowAutomaticInserts="true" AllowAutomaticUpdates="true" AllowAutomaticDeletes="true" OnNeedDataSource="RadGridLandBlockVoucher_NeedDataSource" >
<ClientSettings>
<Scrolling AllowScroll="True" UseStaticHeaders="True" />
</ClientSettings>
<MasterTableView AutoGenerateColumns="False" DataKeyNames="PK_Landdetail" CommandItemDisplay="Top"
CommandItemSettings-AddNewRecordText="Add" >
<Columns>
<telerik:GridBoundColumn DataField="PK_Landdetail" DataType="System.Guid" FilterControlAltText="Filter PK_Landdetail column" HeaderText="PK_Landdetail" ReadOnly="True" SortExpression="PK_Landdetail" UniqueName="PK_Landdetail" Visible="false" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="PK_location" DataType="System.Int32" FilterControlAltText="Filter PK_location column" HeaderText="PK_location" SortExpression="PK_location" UniqueName="PK_location" Visible="false" ReadOnly="true" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ProjectName" FilterControlAltText="Filter ProjectName column" HeaderText="ProjectName" SortExpression="ProjectName" UniqueName="ProjectName" ReadOnly="true" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="PK_land" DataType="System.Guid" FilterControlAltText="Filter PK_land column" HeaderText="PK_land" SortExpression="PK_land" UniqueName="PK_land" Visible="false" ReadOnly="true" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="LandBlockNumber" FilterControlAltText="Filter LandBlockNumber column" HeaderText="LandBlockNumber" SortExpression="LandBlockNumber" UniqueName="LandBlockNumber" ReadOnly="true" >
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="PK_ccode" DataType="System.Int32" FilterControlAltText="Filter PK_ccode column" HeaderText="PK_ccode" SortExpression="PK_ccode" UniqueName="PK_ccode" Visible="false" ReadOnly="true" >
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn FilterControlAltText="Filter ccodenamefull column" HeaderText="科目" SortExpression="ccodenamefull" UniqueName="ccodenamefull">
<ItemTemplate>
<%# Eval("ccodenamefull") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadDropDownList runat="server" ID="RadDropDownListCcode" DataSourceID="SqlDataSourceccode" DataValueField="PK_ccode" DataTextField="ccodenamefull" SelectedValue='<%# Eval ("ccodenamefull") %>' Width="200px" ></telerik:RadDropDownList>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="PayableAmount" DataType="System.Decimal" FilterControlAltText="Filter PayableAmount column" HeaderText="PayableAmount" SortExpression="PayableAmount" UniqueName="PayableAmount">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ActualAmount" DataType="System.Decimal" FilterControlAltText="Filter ActualAmount column" HeaderText="ActualAmount" SortExpression="ActualAmount" UniqueName="ActualAmount">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ContractNo" FilterControlAltText="Filter ContractNo column" HeaderText="ContractNo" SortExpression="ContractNo" UniqueName="ContractNo">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ContractName" FilterControlAltText="Filter ContractName column" HeaderText="ContractName" SortExpression="ContractName" UniqueName="ContractName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="LandTransferArea" DataType="System.Decimal" FilterControlAltText="Filter LandTransferArea column" HeaderText="LandTransferArea" SortExpression="LandTransferArea" UniqueName="LandTransferArea">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="LandLisenceArea" DataType="System.Decimal" FilterControlAltText="Filter LandLisenceArea column" HeaderText="LandLisenceArea" SortExpression="LandLisenceArea" UniqueName="LandLisenceArea">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="LisenceNo" FilterControlAltText="Filter LisenceNo column" HeaderText="LisenceNo" SortExpression="LisenceNo" UniqueName="LisenceNo">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="InvoiceAmount" DataType="System.Decimal" FilterControlAltText="Filter InvoiceAmount column" HeaderText="InvoiceAmount" SortExpression="InvoiceAmount" UniqueName="InvoiceAmount">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn DataField="LisenceStartDate" DataType="System.DateTime" FilterControlAltText="Filter LisenceStartDate column" HeaderText="证照起始日" SortExpression="LisenceStartDate" UniqueName="LisenceStartDate">
<ItemTemplate>
<%# Eval("LisenceStartDate") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadDatePicker ID="RadDatePickerLisenceStartDate" runat="server" DbSelectedDate= '<%# Bind("LisenceStartDate") %>' MinDate="1900-1-1" ></telerik:RadDatePicker>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="LisenceEndDate" DataType="System.DateTime" FilterControlAltText="Filter LisenceEndDate column" HeaderText="LisenceEndDate" SortExpression="LisenceEndDate" UniqueName="LisenceEndDate">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Note" FilterControlAltText="Filter Note column" HeaderText="Note" SortExpression="Note" UniqueName="Note">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSourceLandblockCostView" runat="server" ConnectionString="<%$ ConnectionStrings:myAsystemstring %>"
SelectCommand="SELECT * FROM [LD_LandblockCostView] WHERE [PK_location] = #PK_location AND [PK_land] = #PK_land"
DeleteCommand="delete * from [LD_LandblockCostView] WHERE PK_Landdetail=#PK_Landdetail"
InsertCommand ="INSERT INTO [LD_LandblockCostView] ([PK_location], [PK_land], [PK_ccode], [PayableAmount], [ActualAmount], [ContractNo], [ContractName], [LandTransferArea], [LandLisenceArea], [LisenceNo], [invoiceAmount], [Note], [LisenceStartDate], [LisenceEndDate])
VALUES (#PK_location,#PK_land, #PK_ccode,#PayableAmount, #ActualAmount, #ContractNo,#ContractName,#LandTransferArea,#LandLisenceArea,#LisenceNo,#invoiceAmount,#Note,#LisenceStartDate,#LisenceEndDate)"
UpdateCommand="UPDATE [LD_LandblockCostView] SET [PK_ccode] = #PK_ccode, [PayableAmount] = #PayableAmount, [ActualAmount]=#ActualAmount,[ContractNo]=#ContractNo,[ContractName]=#ContractName,[LandTransferArea]=#LandTransferArea,[LandLisenceArea]=#LandLisenceArea,[LisenceNo]=#LisenceNo,[invoiceAmount]=#invoiceAmount,[Note]=#Note,[LisenceStartDate]=#LisenceStartDate,[LisenceEndDate]=#LisenceEndDate
WHERE [PK_Landdetail] = #PK_Landdetail" >
<SelectParameters>
<asp:ControlParameter ControlID="RadDropDownListProject" Name="PK_location" PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="RadDropDownListLandBlock" Name="PK_land" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name ="PK_Landdetail" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:ControlParameter Name="PK_location" Type="Int32" ControlID="RadDropDownListCompany" PropertyName="SelectedValue" />
<asp:ControlParameter Name="PK_land" Type="String" ControlID="RadDropDownListLandBlock" PropertyName="SelectedValue" />
<asp:ControlParameter Name="PK_ccode" Type="Int32" ControlID="RadDropDownListCcode" PropertyName ="SelectedValue" />
<asp:Parameter Name="PayableAmount" Type="Decimal" />
<asp:Parameter Name="ActualAmount" Type="Decimal" />
<asp:Parameter Name="ContractNo" Type="String" />
<asp:Parameter Name="ContractName" Type="String" />
<asp:Parameter Name="LandTransferArea" Type="Decimal" />
<asp:Parameter Name="LandLisenceArea" Type="Decimal" />
<asp:Parameter Name="LisenceNo" Type="String" />
<asp:Parameter Name="invoiceAmount" Type="Decimal" />
<asp:Parameter Name="Note" Type="String" />
<asp:Parameter Name="LisenceStartDate" Type ="DateTime" />
<asp:Parameter Name="LisenceEndDate" Type="DateTime" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name ="PK_ccode" Type="Int32" />
<asp:Parameter Name="PayableAmount" Type="Decimal" />
<asp:Parameter Name="ActualAmount" Type="Decimal" />
<asp:Parameter Name="ContractNo" Type="String" />
<asp:Parameter Name="ContractName" Type="String" />
<asp:Parameter Name="LandTransferArea" Type="Decimal" />
<asp:Parameter Name="LandLisenceArea" Type="Decimal" />
<asp:Parameter Name="LisenceNo" Type="String" />
<asp:Parameter Name="invoiceAmount" Type="Decimal" />
<asp:Parameter Name="Note" Type="String" />
<asp:Parameter Name="LisenceStartDate" Type ="DateTime" />
<asp:Parameter Name="LisenceEndDate" Type="DateTime" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourceccode" runat="server" ConnectionString="<%$ ConnectionStrings:myAsystemstring %>" SelectCommand="SELECT [PK_ccode], [ccodenamefull] FROM [Ref_ccodenameLandBlockCost]" ></asp:SqlDataSource>
</asp:Panel>
Code-behind
protected void RadGridLandBlockVoucher_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
RadGridLandBlockVoucher.DataSource = SqlDataSourceLandblockCostView;
}
You have the SQL queries defined in a declarative data source. Use that for the DataSourceID of the grid and remove the NeedDataSource handler. The SELECT query will handler your data retrieval.
If you want to use the NeedDataSource event to use some custom data access layer/logic, you should then disable the automatic operations and use the events to write your own code according to your logic/DAL.
Error Message:
The variable name '#Bitnet' has already been declared. Variable names must be unique within a query batch or stored procedure. When trying to edit gridview.
I want to be able to update the checkbox field. This gridview is based on a table join.
<asp:Content ID="Content4" runat="server" contentplaceholderid="ContentPlaceHolder2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="INST_ID,BITNET" DataSourceID="SqlDataSource1" AllowPaging="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="INST_ID" HeaderText="INST_ID" ReadOnly="True" SortExpression="INST_ID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Prefix" HeaderText="Prefix" SortExpression="Prefix" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Address1" HeaderText="Address1" SortExpression="Address1" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
<asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
<asp:BoundField DataField="BITNET" HeaderText="BITNET" ReadOnly="True" SortExpression="BITNET" />
<asp:TemplateField HeaderText="Active">
<ItemTemplate>
<asp:CheckBox ID="Active" runat="server" Checked='<%# Eval("Active").ToString() == "1" ? true:false %>' Enabled="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="Active" runat="server" Checked="true" Enabled="false" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPLOGINConnectionString %>" SelectCommand="SELECT IALO.INST_ID, IALO.FirstName, IALO.LastName, IALO.Prefix, IALO.Title, IALO.Address1, IALO.City, IALO.State, IALO.Zip, IALO.Country, IALO.Phone, IALO.Fax, IALO.BITNET, IALO.Active FROM CEOTable INNER JOIN IALO ON CEOTable.Inst_ID = IALO.INST_ID WHERE (CEOTable.Bitnet = #Bitnet)">
</asp:SqlDataSource>
You have to declare the parameter first. Then only you can use that. It should be something like this
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPLOGINConnectionString %>" SelectCommand="SELECT IALO.INST_ID, IALO.FirstName, IALO.LastName, IALO.Prefix, IALO.Title, IALO.Address1, IALO.City, IALO.State, IALO.Zip, IALO.Country, IALO.Phone, IALO.Fax, IALO.BITNET, IALO.Active FROM CEOTable INNER JOIN IALO ON CEOTable.Inst_ID = IALO.INST_ID WHERE (CEOTable.Bitnet = #Bitnet)">
<SelectParameters>
<asp:Parameter Name="Bitnet" Type="Int32" DefaultValue="0" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Bitnet" Type="Int32" DefaultValue="0" />
</UpdateParameters>
</asp:SqlDataSource>
I solved my own question issue was that I was not clearing my parameters first
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters.Clear();
}
That fixed the issue it works fine now.
I was following this so far everything is fine with a textbox but when try to modify it to checkbox it gives an error: Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.Web.UI.WebControls.CheckBox'.
Its working when I use TextBox but how can I make all the CheckBox editable in bulk updates?
Here's the sample code behind
private bool tableCopied = false;
private DataTable originalDataTable;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
if (!tableCopied)
{
originalDataTable = ((DataRowView)e.Row.DataItem).Row.Table.Copy();
ViewState["originalValueTable"] = originalDataTable;
tableCopied = true;
}
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
originalDataTable = (DataTable)ViewState["originalValueTable"];
foreach (GridViewRow r in GridView1.Rows)
if (IsRowModified(r))
{
GridView1.UpdateRow(r.RowIndex, false);
}
tableCopied = false;
GridView1.DataBind();
}
protected bool IsRowModified(GridViewRow r)
{
int currentID;
string currentreservedate;
string currentisapproved;
currentID = Convert.ToInt32(GridView1.DataKeys[0].Value);
currentreservedate = ((TextBox)r.FindControl("reservedateTextBox")).Text;
currentisapproved = ((CheckBox)r.FindControl("isapprovedCheckBox")).Text;
DataRow row = originalDataTable.Select(String.Format("reservationid = {0}", currentID))[0];
if (!currentreservedate.Equals(row["reservedate"].ToString()))
if (!currentisapproved.Equals(row["isapproved"].ToString()))
{
return true;
}
return false;
}
}
}
Here's the aspx markup
Pending Reservation<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT dbo.BookReservation.reservationid, dbo.BookReservation.bookid, dbo.BookReservation.EmployeeID, dbo.BookReservation.reservedate, dbo.BookReservation.isapproved, dbo.BookReservation.reschedule, dbo.BookReservation.isdeleted, dbo.TblBooks.booktitle FROM dbo.BookReservation INNER JOIN dbo.TblBooks ON dbo.BookReservation.bookid = dbo.TblBooks.bookid"
DeleteCommand="DELETE FROM [BookReservation] WHERE [reservationid] = #reservationid"
InsertCommand="INSERT INTO [BookReservation] ([bookid], [EmployeeID], [reservedate], [isapproved], [reschedule], [isdeleted]) VALUES (#bookid, #EmployeeID, #reservedate, #isapproved, #reschedule, #isdeleted)"
UpdateCommand="UPDATE [BookReservation] SET [bookid] = #bookid, [EmployeeID] = #EmployeeID, [reservedate] = #reservedate, [isapproved] = #isapproved, [reschedule] = #reschedule, [isdeleted] = #isdeleted WHERE [reservationid] = #reservationid">
<DeleteParameters>
<asp:Parameter Name="reservationid" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="EmployeeID" Type="String" />
<asp:Parameter Name="reservedate" Type="DateTime" />
<asp:Parameter Name="isapproved" Type="Boolean" />
<asp:Parameter Name="reschedule" Type="Boolean" />
<asp:Parameter Name="isdeleted" Type="Boolean" />
<asp:Parameter Name="reservationid" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="EmployeeID" Type="String" />
<asp:Parameter Name="reservedate" Type="DateTime" />
<asp:Parameter Name="isapproved" Type="Boolean" />
<asp:Parameter Name="reschedule" Type="Boolean" />
<asp:Parameter Name="isdeleted" Type="Boolean" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true"
DataKeyNames="reservationid, bookid, EmployeeID, reservedate " DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" >
<Columns>
<asp:BoundField DataField="reservationid" HeaderText="reservationid"
InsertVisible="False" ReadOnly="True"
SortExpression="reservationid" Visible="False" />
<asp:BoundField DataField="bookid" HeaderText="bookid"
SortExpression="bookid" Visible="False" />
<asp:BoundField DataField="booktitle" HeaderText="Title"
SortExpression="booktitle" />
<asp:BoundField DataField="EmployeeID" HeaderText="Reserved by"
SortExpression="EmployeeID" />
<%--<asp:BoundField DataField="reservedate" HeaderText="Date reserved"
SortExpression="reservedate" />--%>
<asp:TemplateField HeaderText="Reserve Date"
SortExpression="reservedate">
<EditItemTemplate>
<asp:TextBox ID="reservedateTextBox" runat="server" Text='<%# Bind("reservedate") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="reservedateTextBox" runat="server" Text='<%# Bind("reservedate") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:CheckBoxField DataField="isapproved" HeaderText="Approved"
SortExpression="isapproved" />--%>
<asp:TemplateField HeaderText="Apprvoed"
SortExpression="isapproved">
<EditItemTemplate>
<asp:TextBox ID="isapprovedCheckBox" runat="server" Text='<%# Bind("isapproved") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="isapprovedCheckBox" runat="server" Text='<%# Bind("isapproved") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CheckBoxField DataField="reschedule" HeaderText="Reschedule"
SortExpression="reschedule" />
<asp:CheckBoxField DataField="isdeleted" HeaderText="Deleted"
SortExpression="isdeleted" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button ID="UpdateButton" runat="server" Text="Update" OnClick="UpdateButton_Click" />
<br />
Help would be much appreciated! Thanks in advance!
I think that to answer this question, the contents of the aspx file are required, but I'll try to help. More than likely the element in your form, isapprovedCheckBox, is still set to the the tag asp:Text. As a result the system can't convert the textbox to a checkbox.
Could you include your aspx content as well?
I want to put the current date in my formview 'dateadded' but its not showing when I load it in the browser. I'm using code behind but how can I display it with date and time?
Here's a my code behind.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LibrarySystem.AdminPage
{
public partial class ManageBooks : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(FormView1.CurrentMode == FormViewMode.Insert)
{
TextBox dateadded = FormView1.FindControl("dateaddedTextBox") as TextBox;
dateadded.Text = DateTime.Now.ToString("d");
}
}
}
}
Here's my complete design
<%# Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Lending.aspx.cs" Inherits="LibrarySystem.Test" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<h3>
Lending of Books</h3>
<p>
<asp:FormView ID="FormView1" runat="server" DataSourceID="lendDataSource" DefaultMode="Insert" OnDataBound="FormView1_DataBound">
<EditItemTemplate>
Book ID/ISBN:
<asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>' />
<br />
Book Title:
<asp:TextBox ID="booktitleTextBox" runat="server"
Text='<%# Bind("booktitle") %>' />
<br />
Employee ID:
<asp:TextBox ID="employeeidTextBox" runat="server"
Text='<%# Bind("employeeid") %>' />
<br />
Department:
<asp:TextBox ID="departmentTextBox" runat="server"
Text='<%# Bind("department") %>' />
<br />
Date borrowed:
<asp:TextBox ID="dateborrowedTextBox" runat="server"
Text='<%# Bind("dateborrowed") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
Book:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="booktitleDataSource" DataTextField="booktitle"
DataValueField="bookid" SelectedValue='<%# Bind("bookid", "{0}") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="booktitleDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [bookid], [booktitle] FROM [TblBooks]">
</asp:SqlDataSource>
<br />
Employee ID:
<asp:TextBox ID="employeeidTextBox" runat="server"
Text='<%# Bind("employeeid") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="* Required" ControlToValidate="employeeidTextBox" ValidationGroup="lendbook">
</asp:RequiredFieldValidator>
<br />
Department:
<asp:TextBox ID="departmentTextBox" runat="server"
Text='<%# Bind("department") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="* Required" ControlToValidate="departmentTextBox" ValidationGroup="lendbook">
</asp:RequiredFieldValidator>
<br />
Date borrowed:
<asp:TextBox ID="dateborrowedTextBox" runat="server"
Text='<%# Bind("dateborrowed") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="* Required" ControlToValidate="dateborrowedTextBox" ValidationGroup="lendbook">
</asp:RequiredFieldValidator>
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" ValidationGroup="lendbook"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
<EmptyDataTemplate>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</EmptyDataTemplate>
</asp:FormView>
</p>
<p>
<asp:SqlDataSource ID="lendDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
DeleteCommand="DELETE FROM [LendTable] WHERE [lenid] = #lenid"
InsertCommand="INSERT INTO [LendTable] ([bookid], [employeeid], [department], [dateborrowed], [datereturned]) VALUES (#bookid, #employeeid, #department, #dateborrowed, #datereturned)"
SelectCommand="SELECT dbo.LendTable.bookid, dbo.TblBooks.booktitle, dbo.LendTable.employeeid, dbo.LendTable.department, dbo.LendTable.dateborrowed FROM dbo.LendTable INNER JOIN dbo.TblBooks ON dbo.LendTable.bookid = dbo.TblBooks.bookid"
UpdateCommand="UPDATE [LendTable] SET [bookid] = #bookid, [employeeid] = #employeeid, [department] = #department, [dateborrowed] = #dateborrowed, [datereturned] = #datereturned WHERE [lenid] = #lenid">
<DeleteParameters>
<asp:Parameter Name="lenid" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="employeeid" Type="string" />
<asp:Parameter Name="department" Type="String" />
<asp:Parameter Name="dateborrowed" Type="DateTime" />
<asp:Parameter Name="datereturned" Type="DateTime" />
<asp:Parameter Name="lenid" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="employeeid" Type="string" />
<asp:Parameter Name="department" Type="String" />
<asp:Parameter Name="dateborrowed" Type="DateTime" />
<asp:Parameter Name="datereturned" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="lenid" DataSourceID="lendgridviewDataSource" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN"
SortExpression="bookid" />
<asp:BoundField DataField="booktitle" HeaderText="Book Title"
SortExpression="booktitle" />
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID"
SortExpression="EmployeeID" />
<asp:BoundField DataField="department" HeaderText="Department"
SortExpression="department" />
<asp:BoundField DataField="dateborrowed" HeaderText="Date Borrowed"
SortExpression="dateborrowed" />
<asp:BoundField DataField="datereturned" HeaderText="Date Returned"
NullDisplayText="-- not yet returned --" SortExpression="datereturned" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="lendgridviewDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
DeleteCommand="DELETE FROM [LendTable] WHERE [lenid] = #lenid"
InsertCommand="INSERT INTO [LendTable] ([bookid], [department], [EmployeeID], [dateborrowed], [datereturned]) VALUES (#bookid, #department, #EmployeeID, #dateborrowed, #datereturned)"
SelectCommand="SELECT dbo.LendTable.lenid, dbo.LendTable.bookid, dbo.LendTable.department, dbo.LendTable.EmployeeID, dbo.LendTable.dateborrowed, dbo.LendTable.datereturned, dbo.TblBooks.booktitle FROM dbo.LendTable INNER JOIN dbo.TblBooks ON dbo.LendTable.bookid = dbo.TblBooks.bookid"
UpdateCommand="UPDATE [LendTable] SET [bookid] = #bookid, [department] = #department, [EmployeeID] = #EmployeeID, [dateborrowed] = #dateborrowed, [datereturned] = #datereturned WHERE [lenid] = #lenid">
<DeleteParameters>
<asp:Parameter Name="lenid" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="department" Type="String" />
<asp:Parameter Name="EmployeeID" Type="String" />
<asp:Parameter Name="dateborrowed" Type="DateTime" />
<asp:Parameter Name="datereturned" Type="DateTime" />
<asp:Parameter Name="lenid" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="department" Type="String" />
<asp:Parameter Name="EmployeeID" Type="String" />
<asp:Parameter Name="dateborrowed" Type="DateTime" />
<asp:Parameter Name="datereturned" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>
</p>
<p>
<asp:HyperLink ID="HyperLink4" runat="server"
NavigateUrl="~/Admin/Returning.aspx">Returning</asp:HyperLink>
</p>
<asp:HyperLink ID="HyperLink5" runat="server"
NavigateUrl="~/Admin/AdminPage.aspx">Back to Admin Page</asp:HyperLink>
<p>
</p>
</asp:Content>
You have to use the Databound event for that...
protected void FormView1_DataBound(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Insert)
{
TextBox dateadded = FormView1.FindControl("dateaddedTextBox") as TextBox;
dateadded.Text = DateTime.Now.ToString("d");
}
}
Edit: You need to pass your Date Column in the inserting method of the DetailsView
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
e.Values["DateColumnName"] = ((TextBox)DetailsView1.FindControl("dateaddedTextBox")).Text;
}
Instead of doing that in your Page_Load() method, try doing it in the Page_PreRender() method.