gridview to formview - formview not showing any data - asp.net

I have a gridview where, if you select a row, it takes you to a formview where you can view details from that entry and edit the data. This worked when I had only one filter parameter, but I added another, and now the formview is showing up blank. Here's my code:
<asp:AccessDataSource ID="courseinfo" runat="server" DataFile="~/App_Data/courseinfo.mdb" SelectCommand="SELECT [prefix], [course_number], [title], [date_submitted] FROM [tableCourse]">
</asp:AccessDataSource>
<asp:AccessDataSource ID="courseinfo2" runat="server" DataFile="~/App_Data/courseinfo.mdb" FilterExpression="prefix='{0}' AND course_number='{1}'" SelectCommand="SELECT * FROM [tableCourse]">
<FilterParameters>
<asp:ControlParameter Name="prefix" ControlID="GridView1" PropertyName="SelectedValue" />
<asp:ControlParameter Name="course_number" ControlID="GridView1" PropertyName="SelectedValue" />
</FilterParameters>
</asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="courseinfo" DataKeyNames="prefix,course_number"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False"
CellPadding="8" ForeColor="#333333" GridLines="vertical" BorderWidth="1" BorderColor="#dddddd"
AllowPaging="true" AllowSorting="true" AutoGenerateSelectButton="true">
<RowStyle BackColor="#eeeeee" />
<Columns>
<asp:BoundField DataField="prefix" HeaderText="prefix" SortExpression="prefix" />
<asp:BoundField DataField="course_number" HeaderText="number" SortExpression="course_number" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="date_submitted" HeaderText="date submitted" SortExpression="date_submitted" />
</Columns>
<FooterStyle BackColor="black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="black" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#c0c0e0" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="#eeeeee" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:FormView ID="FormView1" runat="server" DefaultMode="Edit" DataSourceID="courseinfo2"
DataKeyNames="prefix,course_number" Width="100%">
Of course the formview is complete but it contains a lot of code so I didn't include that here, although if anyone wants to see it, I can post it.

You can change your control parameters as follows reason being since you have multiple datakeynames, you cant have selected value as property of both. This should work however:
<asp:ControlParameter Name="prefix" ControlID="GridView1" PropertyName="SelectedDataKey.Values[prefix]" Type="String" />
<asp:ControlParameter Name="course_number" ControlID="GridView1" PropertyName="SelectedDataKey.Values[course_number]" Type="Int32" />

Related

CheckBox Field does not show checked or not in gridview

i'm used checkbox field in gridview to show bit value from database on my gridview
i used default editing row on gridview , when i update row (checked checkbox field)
in database update successfully but in gridview does not show checked or not checked
when i refresh page not show again or when i rerun page same problem as a whole does not show checkbox field bit value status check box filed always is empty and not checkedthis is my gridview html codes
<asp:SqlDataSource ID="sqldsReplys" runat="server" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' UpdateCommand="MNG_Forum_Update_Reply_Status" UpdateCommandType="StoredProcedure" SelectCommand="MNG_Forum_Select_Replys" SelectCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="id" Type="Int64" />
<asp:Parameter Name="PostStatus" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="grdReplys" runat="server" CssClass="GridViewStyle" DataKeyNames="id" AutoGenerateColumns="False" AllowPaging="True" Width="100%" DataSourceID="sqldsReplys">
<Columns>
<asp:CommandField ShowSelectButton="True" ButtonType="Image" SelectImageUrl="~/cpanel/assets/img/sel.png">
</asp:CommandField>
<asp:BoundField DataField="id" HeaderText="ردیف" ReadOnly="true" />
<asp:BoundField DataField="Ptitle" HeaderText="عنوان مطلب" ReadOnly="true" />
<asp:CheckBoxField DataField="PostStatus" HeaderText="وضعیت نمایش" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server" CommandName="delete" ToolTip="حذف این رکورد"
OnClientClick="return confirm('آیا مطمئن هستید؟')" ImageUrl="~/cpanel/assets/img/Trash.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" EditText="ویرایش" UpdateText="تائید" CancelText="لغو" />
</Columns>
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
</asp:GridView>

ASP.NET DropDownList used in AccessDataSource SelectCommand?

Tried adding SelectParameter with drop down selected. Page doesn't fail but class name is not filtered out - not accepting Parameter #ClassName The Affiliation Parameter works fine, but #ClassName does not!
WHERE ClassName = #ClassName
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1" BackColor="White" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
GridLines="Vertical" AllowSorting="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="RowLevelCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UsersDataTbl.StudentID" HeaderText="FDID" SortExpression="StudentID" />
<asp:BoundField DataField="UsersDataTbl.LastName" HeaderText="Last Name" SortExpression="LastName" />
<asp:BoundField DataField="UsersDataTbl.FirstName" HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="UsersDataTbl.UserName" HeaderText="User Name" SortExpression="UserName" />
<asp:BoundField DataField="UsersDataTbl.Affiliation" HeaderText="Affiliation" SortExpression="Affiliation" />
<asp:BoundField DataField="UsersDataTbl.UID" HeaderText="UID" SortExpression="UID" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#6699CC" />
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="<%$ ConnectionStrings:AccessSubSiteString %>"
SelectCommand="SELECT [UsersDataTbl.StudentID], [UsersDataTbl.UserName],
[UsersDataTbl.LastName], [UsersDataTbl.FirstName], [UsersDataTbl.Affiliation], [UsersDataTbl.UID]
FROM [UsersDataTbl] WHERE [UsersDataTbl.Active] = True AND (UsersDataTbl.Affiliation = #Affiliation)
AND ([UsersDataTbl.UID] NOT IN (SELECT UID FROM [EnrollmentsTbl] WHERE ClassName = #ClassName AND Completed = True))
ORDER BY [UsersDataTbl.LastName]" >
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList2" Name="Affiliation" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="DropDownList1" Name="ClassName" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:AccessDataSource>
Not sure why but reversing the order of the ControlParameter fixed the issue, moved ClassName above Affiliation resolved the issue... any ideas why?
<asp:ControlParameter ControlID="DropDownList1" Name="ClassName" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="DropDownList2" Name="Affiliation" PropertyName="SelectedValue" Type="String" />

Dropdown List issue in ASP.net gridview edit mode

I have a asp.net gridview that is giving me fits. I'm pulling the data into the grid with no issues. However, when I click edit, I get this error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'wrkCode'.
My intention is to allow the user to edit a group's work schedule using a dropdown list. Here is my code:
<asp:GridView ID="grdShowGroups" runat="server" datakeynames="grpID" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="5" GridLines="Vertical" CellSpacing="5" Width="700px" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="grdShowGroups_SelectedIndexChanged">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="Edit" />
<asp:BoundField DataField="grpID" HeaderText="grpID" SortExpression="grpID" InsertVisible="False" ReadOnly="True" Visible="false" />
<asp:BoundField DataField="grpStartTime" HeaderText="Start Time" SortExpression="grpStartTime" />
<asp:BoundField DataField="grpEndTime" HeaderText="End Time" SortExpression="grpEndTime" />
<asp:TemplateField HeaderText="Work Schedule" SortExpression="wrkSchedule">
<EditItemTemplate>
<asp:DropDownList ID="drpWrkSchedule" runat="server" DataSourceID="SqlDataSource2" DataTextField="wrkDescription" DataValueField="wrkCode" SelectedValue='<%#Bind("wrkCode")%>' AppendDataBoundItems="true" AutoPostBack="true"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblWrkSchedule" runat="server" Text='<%# Bind("wrkSchedule") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="grpDescription" HeaderText="Description" SortExpression="grpDescription" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeeMGTConnectionString %>" SelectCommand="SELECT [grpID], [grpStartTime], [grpEndTime], [wrkSchedule], [grpDescription] FROM [empGroups]" DeleteCommand="DELETE FROM [empGroups] WHERE [grpID] = #grpID" InsertCommand="INSERT INTO [empGroups] ([grpStartTime], [grpEndTime], [wrkSchedule], [grpDescription]) VALUES (#grpStartTime, #grpEndTime, #wrkSchedule, #grpDescription)" UpdateCommand="UPDATE [empGroups] SET [grpStartTime] = #grpStartTime, [grpEndTime] = #grpEndTime, [wrkSchedule] = #wrkSchedule, [grpDescription] = #grpDescription WHERE [grpID] = #grpID">
<DeleteParameters>
<asp:Parameter Name="grpID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter DbType="DateTime" Name="grpStartTime" />
<asp:Parameter DbType="DateTime" Name="grpEndTime" />
<asp:Parameter Name="wrkSchedule" Type="String" />
<asp:Parameter Name="grpDescription" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter DbType="DateTime" Name="grpStartTime" />
<asp:Parameter DbType="DateTime" Name="grpEndTime" />
<asp:Parameter Name="wrkSchedule" Type="String" />
<asp:Parameter Name="grpDescription" Type="String" />
<asp:Parameter Name="grpID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:EmployeeMGTConnectionString %>" SelectCommand="SELECT [wrkCode], [wrkDescription], [wrkID] FROM [wrkSchedule]"></asp:SqlDataSource>
If I run it with this code for the dropdown list:
<asp:DropDownList ID="drpWrkSchedule" runat="server" DataSourceID="SqlDataSource2" DataTextField="wrkDescription" DataValueField="wrkCode" SelectedValue='<%#Bind("wrkCode")%>'>
I get the error. IF I run it without the "SelectedValue='<%#Bind("wrkCode")%>'" code, it presents the dropdown list as I need, just without a selected value that connects to the data from the data currently in the table.
I've looked at several examples from all over the web and it may be that I'm just missing something small. I just can't figure this out.
Thanks!
You need to select the wrkCode in your sqldatasource1 otherwise it has nothing to bind to. So just include wrkCode in your select clause of that sql statement and you should be good to go.

Can you please tell me why I am getting `The GridView 'GridView1' fired event RowUpdating which wasn't handled.` error message

I have been able to work through the initial problem of not being able to insert records consistently but a new error message crops up.
Now, I am getting The GridView 'GridView1' fired event RowUpdating which wasn't handled.
This error occurs when I click the Update button to update a row of record.
First, I click Edit button. This exposes the Update/Cancel buttons.
When I click the Update button, I get the aforementioned error.
My first take was that gridview needed onRowUpdating but after adding (see markup code), still getting same error.
This is a bit frustrating.
Here is the code.
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim dd As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlstatus"), DropDownList)
e.NewValues("status") = dd.SelectedItem.Text
End Sub
Markup:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" DataKeyNames="reqnum" AllowPaging="True"
CellPadding="4" ForeColor="#333333" GridLines="None" Visible="True"
OnRowDataBound="gvRowDataBound" OnRowEditing="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
EnableViewState="False" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="rownum" HeaderText="rownum" InsertVisible="False" ReadOnly="True"
SortExpression="rownum" />
<asp:BoundField DataField="reqnum" HeaderText="reqnum" SortExpression="reqnum" />
<asp:BoundField DataField="reqrecdate" HeaderText="reqrecdate" SortExpression="reqrecdate" />
<asp:BoundField DataField="reqrecfrom" HeaderText="reqrecfrom" SortExpression="reqrecfrom" />
<asp:BoundField DataField="skillsets" HeaderText="skillsets" SortExpression="skillsets" />
<asp:BoundField DataField="application" HeaderText="application" SortExpression="application" />
<asp:BoundField DataField="hoursperweek" HeaderText="hoursperweek" SortExpression="hoursperweek" />
<asp:BoundField DataField="fromdate" HeaderText="fromdate" SortExpression="fromdate" />
<asp:BoundField DataField="todate" HeaderText="todate" SortExpression="todate" />
<%-- <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />--%>
<asp:TemplateField HeaderText="status">
<EditItemTemplate>
<asp:DropDownList id="ddlstatus" CssClass="dropdown" DataSourceID="DSforDDL" runat="server">
<asp:ListItem Text="none" Value=""></asp:ListItem>
<asp:ListItem>Not Started</asp:ListItem>
<asp:ListItem>Pending</asp:ListItem>
<asp:ListItem>Completed</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblstatus" runat="server" Text='<% #Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="statusupdate" HeaderText="statusupdate" SortExpression="statusupdate" />
<asp:BoundField DataField="statusupby" HeaderText="statusupby" SortExpression="statusupby" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT Distinct [rownum],[reqnum], [reqrecdate], [reqrecfrom], [skillsets], [application], [hoursperweek], [fromdate], [todate], [status], [statusupdate], [statusupby] FROM [Term] ORDER BY [reqnum]"
UpdateCommand="INSERT INTO Term(reum, reqrecdate, reqrecfrom, skillsets, application, hoursperweek, fromdate, todate, status, statusupby, statusupdate) VALUES (#reum,#reqrecdatelbltxt, #reqrecfromlbltxt, #skillsets, #application, #hoursperweek,#fromdate,#todate, #status, #lognametxt, #logdatetxt)"
<DeleteParameters>
<asp:Parameter Name="rownum" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="reqnum" Type="String" />
<asp:Parameter DbType="DateTime" Name="reqrecdate" />
<asp:Parameter Name="reqrecfrom" Type="String" />
<asp:Parameter Name="skillsets" Type="String" />
<asp:Parameter Name="application" Type="String" />
<asp:Parameter Name="hoursperweek" Type="Int32" />
<asp:Parameter DbType="DateTime" Name="fromdate" />
<asp:Parameter DbType="DateTime" Name="todate" />
<asp:Parameter Name="status" Type="String" />
<asp:Parameter DbType="DateTime" Name="statusupdate" />
<asp:Parameter Name="statusupby" Type="String" />
<asp:Parameter Name="rownum" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Thanks a lot in advance for your assistance.
The gridView tracks handlers to ensure something has been added to deal with the update intentionally.
Unless you are Manually calling "AddHandler" somewhwere else in code, I don't see a handles claus on your event method that would intercept the event being raised.

dropdown list wont populate values from sql database

I've been strugglng with this one problem for a while now. I have an application as:
<asp:SqlDataSource runat="server" ID="categoriesDataSource"
SelectCommand="SELECT [CategoryID], [Name], [UserId] FROM [Categories] WHERE ([UserId] = #UserId) ORDER BY [Name]">
<SelectParameters>
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>
</asp:SqlDataSource>
Pick a category:
<asp:DropDownList ID="categories" runat="server" AutoPostBack="True"
DataSourceID="categoriesDataSource" DataTextField="Name"
DataValueField="CategoryID" AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value="">-- All Categories --</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="picturesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT PictureID, Title, UploadedOn
FROM Pictures
WHERE UserId = #UserId AND
(CategoryID = #CategoryID OR #CategoryID IS NULL)
ORDER BY UploadedOn DESC" CancelSelectOnNullParameter="False">
<SelectParameters>
<asp:QueryStringParameter Name="UserId" QueryStringField="ID" />
<asp:ControlParameter ControlID="categories" Name="CategoryID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="PictureID" DataSourceID="picturesDataSource" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="PictureID"
DataNavigateUrlFormatString="~/Photodetail.aspx?ID={0}" Text="View Comments" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="UploadedOn" HeaderText="Date Added"
SortExpression="UploadedOn" />
<asp:ImageField DataImageUrlField="PictureID"
DataImageUrlFormatString="~/UploadedImages/{0}.jpg" HeaderText="Image">
<ControlStyle Width="100px" />
</asp:ImageField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" 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>
When i take away the
<SelectParameters>
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>
and ([UserId] = #UserId), the dropdown list control will populate values from the database but when I leave those 2, it won't populate any value from the database.
When I remove the
<asp:QueryStringParameter Name="CategoryID" QueryStringField="ID" />
</SelectParameters>,
and leave ([UserId] = #UserId), I get the error:
Must declare the scalar variable "#UserId".
Can somebody please help me out?
Thanks in advance
The one I posted here in your previous thread is definitely working.
I think you might have missed out the "ID" querystring in your url.
Are you browsing to "http://localhost:1234/WebAppName/PageName.aspx?=12"? (assume the auto generated port number is 1234 and the ID is 12)
It will throw error if you don't specify "?ID=12".
public void bind()
{
sqldataadapter da=new sqldataadapter("select A,B from table",cn);
datatable dt=new datatable();
da.fill(dt);
dropdownlist1.datatextfield="A";
dropdownlist1.datavaluefield="B";
dropdownlist1.datasource=dt;
dropdownlist1.databind();
}

Resources