Error when clicking edit in gridview - c#

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.

Related

why Gridview commonfield Update command is not working?

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>

Edit button and next page link button on same gridview row

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>

grid view how to hide detail of some special type of data when i bind data in grid view in asp.net?

i have a problem i am binding data in grid view from datasource and there i am geting information of different type of order status start form 1 to 7 . i do not want to display the status 7 data in my grid view . can you help me how we
<data:EntityGridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
DataSourceID="ActiveOrdersDataSource" Width="100%" DataKeyNames="OrderNo" AllowMultiColumnSorting="false"
OnRowCommand="GridView1_RowCommond" OnRowDataBound="GridView1_OnRowDataBound"
DefaultSortDirection="Descending" ExcelExportFileName="Export_ActiveOrders.xls">
<Columns>
<asp:CommandField ShowSelectButton="True" SelectText="Edit" />
<asp:BoundField DataField="OrderNo" ItemStyle-HorizontalAlign="Center" HeaderText="Order No" SortExpression="[OrderNo]" />
<asp:BoundField DataField="OrderDate" DataFormatString="{0:d}" HtmlEncode="False"
HeaderText="Order Date" SortExpression="[OrderDate]" />
<asp:BoundField DataField="PickupCompanyName" HeaderText="Pickup Company" SortExpression="[PickupCompanyName]" Visible="false" />
<asp:BoundField DataField="DeliveryCompanyName" HeaderText="Delivery Company" SortExpression="[DeliveryCompanyName]" Visible="false" />
<data:HyperLinkField HeaderText="Ac.No." ControlStyle-ForeColor="Black" DataContainer="AccountNumberSource"
DataTextField="AccountNumber" />
<data:HyperLinkField HeaderText="Client" DataContainer="AccountNumberSource"
DataTextField="Name" />
<asp:BoundField DataField="Caller" ItemStyle-HorizontalAlign="Center" HeaderText="Caller" SortExpression="[Caller]" />
<asp:BoundField DataField="Department" ItemStyle-HorizontalAlign="Center" HeaderText="Department" SortExpression="[Department]" />
<asp:BoundField DataField="Reference" HeaderText="Reference" SortExpression="[Reference]" />
<asp:TemplateField HeaderText="Driver" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblDriver" runat="server" Text='<%#Eval("CurrentDriverNumberSource.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<data:HyperLinkField HeaderText="Service" ControlStyle-ForeColor="Black" DataContainer="ServiceTypeIdSource"
DataTextField="Description" />
<data:HyperLinkField HeaderText="Vehicle" ControlStyle-ForeColor="Black" DataContainer="VehicleTypeIdSource"
DataTextField="Description" />
<asp:TemplateField HeaderText="Status" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%#Eval("StatusID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TotalAmount" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblAmount" runat="server" Text='<%# Eval("TotalAmount","{0:C2}")%>' ></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<b>No Active Orders Found!</b>
</EmptyDataTemplate>
</data:EntityGridView>
and for datasource i use this
<data:ActiveOrdersDataSource ID="ActiveOrdersDataSourcebyCompany" runat="server" SelectMethod="GetPaged" EnablePaging="True" EnableSorting="True" EnableDeepLoad="True">
<DeepLoadProperties Method="IncludeChildren" Recursive="False">
<Types>
<data:ActiveOrdersProperty Name="Clients" />
<data:ActiveOrdersProperty Name="Companies" />
<data:ActiveOrdersProperty Name="Drivers" />
<data:ActiveOrdersProperty Name="SalesPeople" />
<data:ActiveOrdersProperty Name="ServiceTypes" />
<data:ActiveOrdersProperty Name="VehicleTypes" />
<data:ActiveOrdersProperty Name="Zones" />
</Types>
</DeepLoadProperties>
<Parameters>
<asp:SessionParameter Name="CompanyId" SessionField="CompanyId" Type="String" />
<data:SqlParameter Name="WhereClause" UseParameterizedFilters ="false">
<Filters>
<%--<data:CustomParameter Name="WhereClause" Value="" ConvertEmptyStringToNull="false" />
<data:CustomParameter Name="OrderByClause" Value="" ConvertEmptyStringToNull="false" />
<asp:ControlParameter Name="PageIndex" ControlID="GridView1" PropertyName="PageIndex"
Type="Int32" />
<asp:ControlParameter Name="PageSize" ControlID="GridView1" PropertyName="PageSize"
Type="Int32" />
<data:CustomParameter Name="RecordCount" Value="0" Type="Int32" />--%>
</Parameters>
</data:ActiveOrdersDataSource>
Goutam,
you can use the Filter Tag to set the value you want like:
<Parameters>
<data:SqlParameter Name="WhereClause" UseParameterizedFilters="false">
<Filters>
<data:ChartOfAccountsFilter Column="Status" ComparisionType="NotContains" DefaultValue="7" />
</Filters>
</data:SqlParameter>
</Parameters>
you can make as per this code:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim x As String
x = e.Row.Cells(1).Text
Dim y As String
y = e.Row.Cells(8).Text
If Convert.ToInt32(x) <= Convert.ToInt32(y) Then
e.Row.ForeColor = System.Drawing.Color.Blue
End If
End If
End Sub

Master Detail Gridview asp.net C#

I am trying to make something like this:
Master detail gridview
But give me error in this line from cs file when i click Show details in the GridView:
gvRow.FindControl("GridView2").Visible = true;
I am trying when i click Show details to see the Names from other table where ID_Proba is = to ID from first Table.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField
DataField="id"
HeaderText="id"
ReadOnly="True"
SortExpression="id" InsertVisible="False" />
<asp:BoundField
DataField="name"
HeaderText="name"
SortExpression="name" />
<asp:CommandField ShowSelectButton="True"
SelectText="Show Details"/>
</Columns>
<EmptyDataTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource2" Visible="False">
<Columns>
<asp:BoundField DataField="id"
HeaderText="id"
SortExpression="id" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="id_proba"
HeaderText="id_proba"
SortExpression="id_proba" />
<asp:BoundField DataField="name"
HeaderText="name"
SortExpression="name" />
</Columns>
</asp:GridView>
</EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [Proba1]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [proba3] WHERE ([id_proba] = #id_proba)">
<SelectParameters>
<asp:Parameter Name="id_proba" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</form>
This is the cs file:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
gvRow.FindControl("GridView2").Visible = true;
}
SqlDataSource2.SelectParameters[0].DefaultValue = GridView1.SelectedDataKey[0].ToString();
GridView1.SelectedRow.FindControl("GridView2").Visible = true;
}
You need to take "GridView2" out of the EmptyDataTemplate and put it in a TemplateField. When you have data, the EmptyDataTemplate is not created at all, and also there is no grid in each row, because this template is for the whole grid.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField
DataField="id"
HeaderText="id"
ReadOnly="True"
SortExpression="id" InsertVisible="False" />
<asp:BoundField
DataField="name"
HeaderText="name"
SortExpression="name" />
<asp:CommandField ShowSelectButton="True"
SelectText="Show Details"/>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource2" Visible="False">
<Columns>
<asp:BoundField DataField="id"
HeaderText="id"
SortExpression="id" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="id_proba"
HeaderText="id_proba"
SortExpression="id_proba" />
<asp:BoundField DataField="name"
HeaderText="name"
SortExpression="name" />
</Columns>
</asp:GridView>
</ItemTemplate>
<asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [Proba1]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ProbaConnectionString %>"
SelectCommand="SELECT * FROM [proba3] WHERE ([id_proba] = #id_proba)">
<SelectParameters>
<asp:Parameter Name="id_proba" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</form>

How to get a refreshed table without refreshing the whole page?

I am developing an ASP.NET intranet web application which is a Quiz Engine. Since I am a new ASP.NET developer, I have a table that shows some feedback received from the users and the admin has the ability to accept or reject these feedback using the DropDownList.
Under this table, there is a JQuery Accordion that when the Admin clicks on it, he will see a table with the received feedback for the last three months. But when the admin accepts or rejects one of the feedback and then clicks on the Accordion, he will see the that table but without that feedback even if the feedback is one of the feedbacks submitted last month. However, when you refresh the page and then clicks on the Accordion, he will see it.
So is there any functionality that will help me to get an auto-refresh table with refreshing the whole page?
Any help please?
ASP.NET Code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
width="950px" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
DataSourceID="SqlDataSource1"
OnRowDataBound="GridView1_RowDataBound" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/>
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="DivisionShortcut" HeaderText="Division"
SortExpression="DivisionShortcut" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource2"
Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false"
DataTextField="Status" DataValueField="ID" AutoPostBack="true"
OnDataBound="DropDownList_DataBound" OnSelectedIndexChanged ="DropDownList_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, dbo.SafetySuggestionsLog.Username,
dbo.Divisions.DivisionShortcut
FROM dbo.employee INNER JOIN
dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode"
FilterExpression="[DivisionShortcut] like '{0}%'">
<FilterParameters>
<asp:ControlParameter ControlID="ddlDivision" Name="DivisionShortcut"
PropertyName="SelectedValue" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
<%--For the DropDownList--%>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [SafetySuggestionsStatus]">
</asp:SqlDataSource>
<%--Filtering by Division--%>
<asp:SqlDataSource ID="sqlDataSourceDivision" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [DivisionShortcut] FROM [Divisions]"></asp:SqlDataSource>
<dl id="jfaq">
<br />
<dt>Safety Suggestions List (for the last three months)</dt>
<dd>
<br />
<asp:Panel ID="Panel1" runat="server">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
AllowSorting="True" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
DataSourceID="SqlDataSource4">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/>
<Columns>
<asp:BoundField DataField="SubmittedMonth" HeaderText="Submitted Month"
SortExpression="SubmittedMonth" ReadOnly="True" />
<asp:BoundField DataField="DivisionShortcut" HeaderText="Division"
SortExpression="DivisionShortcut" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
</Columns>
<RowStyle HorizontalAlign="Center"></RowStyle>
</asp:GridView>
</asp:Panel>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT TOP (100) PERCENT LEFT(DATENAME(month, dbo.SafetySuggestionsLog.DateSubmitted), 3) + '-' + DATENAME(year, dbo.SafetySuggestionsLog.DateSubmitted)
AS SubmittedMonth, dbo.Divisions.DivisionShortcut, dbo.SafetySuggestionsLog.Username, dbo.employee.Name, dbo.SafetySuggestionsLog.Title,
dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsType.Type, dbo.SafetySuggestionsStatus.Status
FROM dbo.Divisions INNER JOIN
dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode INNER JOIN
dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID INNER JOIN
dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE (DATEDIFF(month, dbo.SafetySuggestionsLog.DateSubmitted, GETDATE()) < 3)
ORDER BY dbo.SafetySuggestionsLog.DateSubmitted DESC">
</asp:SqlDataSource>
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />
</dd>
</dl>
Code-Behind:
protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
int suggestionStatus = int.Parse(ddl.SelectedValue);
GridViewRow row = (GridViewRow)ddl.NamingContainer;
string strID = GridView1.DataKeys[row.RowIndex]["ID"].ToString();
int ID = Int32.Parse(strID);
//For inserting the status in the database
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
string updateCommand = "UPDATE SafetySuggestionsLog SET [StatusID] = #StatusID WHERE [ID] = #ID";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("#StatusID", suggestionStatus);
cmd.Parameters.AddWithValue("#ID", ID);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
UPDATE:
I used the UpdatePanel control to get a partial update, and I used <triggers>, but I got the following error and I don't know why:
Could not find an event named 'Click' on associated control
'GridView2' for the trigger in UpdatePanel 'UpdatePanel1'.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView2" EventName="Click" />
</Triggers>
<ContentTemplate>
<dl id="jfaq">
<br />
<dt>Safety Suggestions List (for the last three months)</dt>
<dd>
<br />
<asp:Panel ID="Panel1" runat="server">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
AllowSorting="True" CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
DataSourceID="SqlDataSource4">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/>
<Columns>
<asp:BoundField DataField="SubmittedMonth" HeaderText="Submitted Month"
SortExpression="SubmittedMonth" ReadOnly="True" />
<asp:BoundField DataField="DivisionShortcut" HeaderText="Division"
SortExpression="DivisionShortcut" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
</Columns>
<RowStyle HorizontalAlign="Center"></RowStyle>
</asp:GridView>
</asp:Panel>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT TOP (100) PERCENT LEFT(DATENAME(month, dbo.SafetySuggestionsLog.DateSubmitted), 3) + '-' + DATENAME(year, dbo.SafetySuggestionsLog.DateSubmitted)
AS SubmittedMonth, dbo.Divisions.DivisionShortcut, dbo.SafetySuggestionsLog.Username, dbo.employee.Name, dbo.SafetySuggestionsLog.Title,
dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsType.Type, dbo.SafetySuggestionsStatus.Status
FROM dbo.Divisions INNER JOIN
dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode INNER JOIN
dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID INNER JOIN
dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID
WHERE (DATEDIFF(month, dbo.SafetySuggestionsLog.DateSubmitted, GETDATE()) < 3)
ORDER BY dbo.SafetySuggestionsLog.DateSubmitted DESC">
</asp:SqlDataSource>
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />
</dd>
</dl>
</ContentTemplate>
</asp:UpdatePanel>
In the trigger you mentioned Click event, but in the code behind you have only DropDownList_SelectedIndexChanged event.
Instead you can trigger the SelectedIndexChanged event for the DropDownList as
<asp:AsyncPostBackTrigger ControlID="DropDownList" EventName="SelectedIndexChanged" />
You just need to trigger a valid event in the code behind.
Update: Since the SelectedIndexChanged updates the database, so you need to refresh the DataGrid to populate the updated data.
Add this line code at the end of SelectedIndexChanged method:
GridView2.DataBind();
use ajax update panel this may help http://ajax.net-tutorials.com/controls/updatepanel-control/

Categories

Resources