Edit command erroring out in gridview - asp.net

I'm unable to identify the problem which is causing the edit command to fail
GridView Code
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="Requestid" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None" Height="321px" Width="604px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" UpdateText="Submit" />
<asp:BoundField DataField="Requestid" HeaderText="Requestid" ReadOnly="True"
SortExpression="Requestid" />
<asp:BoundField DataField="Receiveddate" HeaderText="Receiveddate"
SortExpression="Receiveddate" />
<asp:BoundField DataField="Ctname" HeaderText="Ctname"
SortExpression="Ctname" />
<asp:TemplateField HeaderText="Requestor" SortExpression="Requestor">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="analystname"
DataValueField="contact_id" SelectedValue='<%# Bind("Requestor") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:Customers %>" SelectCommand="SELECT [contact_id], [first_name]+ space(1)+ [last_name] as analystname FROM [contact]
order by contact_id"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Requestor") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#CC3300" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#CBB06D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Customers %>" SelectCommand="SELECT RR.Requestid,Convert(varchar(20),RR.Receiveddate,100) as Receiveddate,RR.Ctname,C.First_Name +Space(1) + C.Last_NAME as Requestor from report_request_draft RR inner join Contact C on RR.requestor = C.contact_id
where clonedfromid is not null"
UpdateCommand="UPDATE [REPORT_REQUEST_DRAFT] SET [REQUESTOR] = #Requestor WHERE [requestid] = #requestid">
</asp:SqlDataSource>
When i click on the edit command button it gives me the below error
Error
'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

The DropDownList can't be bound in this way. By using the SelectedValue binding as you have it, it requires that the DropDownList is bound before the GridView is bound, but that is a logical impossibility since the DropDownList can't be bound until the GridView has completed its binding. You should put this logic in the code behind with the RowDataBound event. At that point, you should manually bind your DropDownList and then set its SelectedValue property.

Consider the following link which will help you to solve your problem and it also shows you how to bind EditItemTemplet perfectly in grid View. I condenser you to use this link for better understand grid view.

Related

How to update gridview through design code/ aspx page code

I'm trying to update GridView every time I update, delete or insert any record. Now I have provided sql data source code for GridView in Design Code not form code. Now how can I update it from there ? When I write GridView1.databind() on form code, it says
> Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
Can someone tell me how to DataBind in design view, to update GridView every time I insert/update/delete record ?
Here is GridView1 Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" Width="284px">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="surname" HeaderText="surname" SortExpression="surname" />
<asp:BoundField DataField="amount" HeaderText="amount" SortExpression="amount" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myDbConnectionString %>" SelectCommand="SELECT [name], [surname], [amount] FROM [Table1]"></asp:SqlDataSource>
Set the DataSourceID to null.
GridView1.DataSourceID = null;
GridView1.DataSource = dt;
GridView1.DataBind();
That error was coming because data was being binded on both Design view code as well as web form backend code.
I simply added GridView1.DataSourceID = null;
and that did the job.

Make an ASP.NET GridView change based on a DropDownList selection

I've tried a thousand ways to make this work and I can't. I have a DropDownList bound to the data in a column from an .mdf database. I also have a GridView below it that is bound using the Query Builder to columns from 4 different tables in that same database.
I know to do this you have to add a WHERE clause to the query string that looks something like this -- WHERE ([column] = #column) -- And then you have to put a SelectParameters in the HTML code. But I don't know how to write it correctly.
I keep getting errors when I run the page in the browser. Errors like "Column Ambiguous", "Column doesn't exist", "Must declare scalar variable #column", etc. I think it may have something to do with the fact that three of the tables have a column named description and 2 of them have aliases.
Here's the HTML:
<%# Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="ByCategory.aspx.vb" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="Label2" runat="server" Text="Select a category:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="description" DataValueField="description"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [description] FROM [category]"></asp:SqlDataSource>
<asp:Button ID="btnGo" runat="server" Text="Go" />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource3" ForeColor="#333333" GridLines="None" Visible="False">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="whenCreated" HeaderText="whenCreated" SortExpression="whenCreated" />
<asp:BoundField DataField="description" HeaderText="description" SortExpression="description" />
<asp:BoundField DataField="firstName" HeaderText="firstName" SortExpression="firstName" />
<asp:BoundField DataField="lastName" HeaderText="lastName" SortExpression="lastName" />
<asp:BoundField DataField="Expr1" HeaderText="Expr1" SortExpression="Expr1" />
<asp:BoundField DataField="Expr2" HeaderText="Expr2" SortExpression="Expr2" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT bugEntry.whenCreated, bugEntry.description, employee.firstName, employee.lastName, category.description AS Expr1, status.description AS Expr2 FROM bugEntry INNER JOIN category ON bugEntry.cat_id = category.cat_id INNER JOIN status ON bugEntry.status_id = status.status_id CROSS JOIN employee">
</asp:SqlDataSource>
</asp:Content>
First ensure your drop down box has AutoPostBack, onselectedindexchanged and DataValueField properties set like below:
<'asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="description" AutoPostBack="True"
DataValueField="ItemNo" DataTextField="ItemName" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
Then set an ObjectDataSource which uses the value from the DropDownList as its Select Parameter. Bind your GridView to the ObjectDataSource.
Finally in your DropDownList1_SelectedIndexChanged method do the following:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ObjectDataSource1.DataBind();
GridView1.DataBind();
}
Its only either the ObjectDataSource1 or the GridView1 that needs to be called, cant recall which at the moment.
i also have this scenario this is how i solved
i bound my Gridview with data source and build query with the help of the SqlDataSource Wizard
when you configure data source use first option "specify custom SQl statement or stored procedure " because you are using multiple table
now click next and use query builder
on Top first pane of window right click and add your table
choose column from your table as per your requirement
now in query pane write this in new line at the end
WHERE ([column]=#column)
now ok and click next
new window will appear
select parameter source
and control id click next and you are good to go
hope this will help

DataBinding: 'UserControls.SiteDetails+StructSelectedSiteList' does not contain a property with the name 'siteName'

I get the following error at the time of DataBinding() for the gridview.
Eventhough the "siteName" field is defined as part of the structure "StructSelectedSiteList", the DataBinder(...) in ASP.net gridview definition does not recognize the field. All the fields and the structure are defined as public.
I had referred to previous posts, but it did not help.
DataBinding does not contain a property
Appreciate your response. Thanks!
Here's the code.
StructSelectedSiteList - structure which is populated with values. this is the data source.
[Serializable]
public struct StructSelectedSiteList
{
public string siteName;
public int taskId;
};
In code below, I populate the structure.
var lstSites = new List<StructSelectedSiteList>();
LOOP1:
// populate the struct for selected sites
StructSelectedSiteList structSelectedSiteList;
structSelectedSiteList.siteName = lblSiteName.Text;
structSelectedSiteList.taskId = taskId;
lstSites.Add(structSelectedSiteList);
Now the gridview is bound with populated structure.
gvSelectedSites.DataSource = lstSites;
gvSelectedSites.DataBind(); <<----- Error occurs here
The ASP.net gridview definition is-
<asp:GridView ID="gvSelectedSites" runat="server" AutoGenerateColumns="False" AllowSorting="True"
CellPadding="4" EmptyDataText="There are no data records to display." Font-Names="Segoe UI"
Width="605px" ForeColor="#333333" GridLines="None" RowStyle-HorizontalAlign="Center"
EmptyDataRowStyle-Font-Bold="true" EmptyDataRowStyle-ForeColor="Red" OnRowDataBound="gvSelectedSites_OnRowDataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="siteName">
<ItemTemplate>
<asp:Label ID="lblSiteName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "siteName")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task" Visible="false">
<ItemTemplate>
<asp:Label ID="lblTaskId" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "taskId")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
After testing I found out that I was accessing 'siteName' as structure field, but I need to access it as a property. Hence, after converting all the fields of the structure into properties, I am able to proceed without the exception. Thanks to solutions "DataBinding: 'index+NewsItem' does not contain a property with the name 'Link'", but property exists (Not a Typo) and 'myObject' does not contain a property with the name 'ID' (Not a typo)

asp.net one more updatepanel is not working

I added 3 updatepanel on a aspx page.Two updatepanel includes gridview.And i make gridview paging.two buttons fill this gridviews when clicked.I set both of them triggers and conditional state.Third updatepanel includes textbox.And i set updatemode conditional,triggers third button.
That my question,when i click firstly third button i select checked datafield from gridview,run succesfully.
But when i click after one clicked,third updatepanel and gridview paging is not working.
<asp:UpdatePanel ID="UpdatePanelWorkFLow" runat="server">
<%--<Triggers>
<asp:PostBackTrigger ControlID="btnGenerate" />
</Triggers>--%>
<ContentTemplate>
<asp:GridView ID="GridWorkFlow" runat="server" AllowPaging="True" AutoGenerateColumns="False"
EnableSortingAndPagingCallbacks="false" CellPadding="4" ForeColor="#333333" GridLines="None"
OnPageIndexChanging="GridWorkFlow_PageIndexChanging">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ShowHeader="true" HeaderText="SeƧ">
<ItemTemplate>
<asp:CheckBox ID="checkWorkFlow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label ID="labelWorkFlowId" runat="server" Text='<%# Eval("WorkFlowId")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="WorkFlow Adi">
<ItemTemplate>
<asp:Label ID="labelWorkFlowName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Birincil Entity Adi">
<ItemTemplate>
<asp:Label ID="labelPrimaryEntity" runat="server" Text='<%# Bind("PrimaryEntity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="true" HeaderText="Durum AƧiklamasi">
<ItemTemplate>
<asp:Label ID="labelWorkFlowStaus" runat="server" Text='<%# GetStatusCodeText(DataBinder.Eval(Container.DataItem,"StatusCode"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnWorkFLow" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
my gridview paging code
WorkFlowDataBind();
GridWorkFlow.PageIndex = e.NewPageIndex;
GridWorkFlow.DataBind();
my third button code
string userQueryId = GetUserQueryId();
string userqueryName = GetUserQueryName();
string workFlowId = GetWorkFlowId();
string primaryEntityName = GetWorkFlowPrimaryEntity();
GetWorkFlowName();
my one and second button when clicked load gridview
Datatable dtUserQuery=GetDataTable();
GridUserQuery.DataSource = dtUserQuery;
GridUserQuery.DataBind()
How can i fix.
Logic is .. If you have one or more update panel in the same page then properties of all update panel should be same such as if update panel data are dependency on each other
Update Mode : Always
EnableViewState : true(if you are using it)
Or you can explicitly update the Update panel when mode is conditional
Let us say you have two update panel like up1 and up2.
You have placed the label in the up1 and the other form or button placed in the up2.
Now on the click event in the up2, label placed in the up1 should be updated without loading the full page. Set the following stuffs
Set EnableParitialRendering ='True'(Script Manager)
Set Update Mode :Conditional(up1)
Set Update Mode : Always (up2). All set up done here. On click of a button in the up2, do your work and call up1.Update(); That's all.

How to put 3 GridViews inside a Repeater

I have a long table, so I want to split it into 3 GridView. Each GridView will have different headers (different columns from the DB). I want to use one SqlDataSource. For that, I am thinking to use a Repeater and then put 3 GridViews inside it.
Since I am using StoredProcedure that asks for one parameter in order to specify the headers of each GridView, I set the that parameter as HiddenField but even with that I did not get any result.
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("GroupID")%>' />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenField1" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" CellPadding="4"
DataKeyNames="BadgeNo" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT DISTINCT GroupID
from courses">
</asp:SqlDataSource>
In order to solve this problem you should have to plan or design the dataSource - may be you can use Linq or Entity Model or DataSet. I think the SqlDataSource is not appropriate here.
In current code-snippet, you should have to design SELECT - sql statement with where condition/clause of SqlDataSource for GridView controls.

Resources