I have a Gridview control that I'm trying to allow the users to use the delete button to delete the record from the database. When I click the delete button, it tells me that there is no value for the parameter. Here is the relevant markup:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT Org_Sec_Atty.ID, ORGANIZATIONS.ORG_NAME, ORG_SECTIONS.SectionName, ATTORNEYS.NAME, ATTORNEYS.LASTNAME, ATTORNEYS.EMAIL, ATTORNEYS.TEL
FROM (ORGANIZATIONS INNER JOIN (Org_Sec_Atty INNER JOIN ATTORNEYS ON Org_Sec_Atty.Atty_ID = ATTORNEYS.ATTY_ID) ON ORGANIZATIONS.ID = Org_Sec_Atty.OrgID) INNER JOIN ORG_SECTIONS ON Org_Sec_Atty.SecID = ORG_SECTIONS.ID;"
DeleteCommand="DELETE FROM Org_Sec_Atty WHERE ID = ?">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
</asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="20"
AllowSorting="True" DataSourceID="AccessDataSource1" >
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="ORG_NAME" HeaderText="Organization" SortExpression="ORG_NAME" />
<asp:BoundField DataField="SectionName" HeaderText="Section" SortExpression="SectionName" />
<asp:BoundField DataField="NAME" HeaderText="First Name" SortExpression="NAME" />
<asp:BoundField DataField="LASTNAME" HeaderText="Last Name" SortExpression="LASTNAME" />
<asp:BoundField DataField="EMAIL" HeaderText="E-mail" SortExpression="EMAIL" />
<asp:BoundField DataField="TEL" HeaderText="Phone" SortExpression="TEL" />
</Columns>
</asp:GridView>
I initially forgot to include the Org_Sec_Atty.ID in my SELECT statement and added it in there later when I clicked the delete button and nothing happened. I tried clearing my browser cache with no luck. Any other ideas?
I think that you also need to set the DataKeyNames property in the gridview:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="20"
AllowSorting="True" DataSourceID="AccessDataSource1" DataKeyNames="ID" >
This should sort you out.
Related
I put gridview and I added hyperlink to take the user to other page and changed the status id based on a query but the problem that the update query is not worked even no error:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" style="margin:auto" >
<Columns>
<asp:TemplateField HeaderText="user enter">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# String.Format("~/entry.aspx?request_id={0}",DataBinder.Eval (Container,"DataItem.request_id"))%>' >
enter </asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name" ReadOnly="True" SortExpression="name" />
<asp:BoundField DataField="visitor_mobile" HeaderText="visitor_mobile" SortExpression="visitor_mobile" />
<asp:BoundField DataField="request_id" HeaderText="request_id" InsertVisible="False" ReadOnly="True" SortExpression="request_id" />
<asp:BoundField DataField="request_date" HeaderText="request_date" SortExpression="request_date" />
<asp:BoundField DataField="visit_date" HeaderText="visit_date" SortExpression="visit_date" />
<asp:BoundField DataField="reason" HeaderText="reason" SortExpression="reason" />
<asp:BoundField DataField="status_name" HeaderText="status_name" SortExpression="status_name" />
</Columns>
</asp:GridView>
and this is the code when user click enter it query to update the status_id:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:visitormanagementConnectionString %>" SelectCommand="update dbo.requests set status_id=5
where request_id= #request_id;" UpdateCommand="update dbo.requests set status_id=5 where request_id= #request_id">
<SelectParameters>
<asp:QueryStringParameter Name="request_id" QueryStringField="request_id" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="request_id" />
</UpdateParameters>
</asp:SqlDataSource>
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>
I need to be able to view data from a SQL database, and select specific rows to print (dealing with this later).
Considering I don't need to edit the data at any point, and all I need is the data with one checkbox extra, what control would be best?
Most likely a GridView:
<asp:SqlDataSource id="CustomersSource"
SelectCommand="SELECT CustomerID, CompanyName, FirstName, LastName FROM SalesLT.Customer"
ConnectionString="<%$ ConnectionStrings:AWLTConnectionString %>"
runat="server"/>
<asp:GridView id="CustomersGridView"
DataSourceID="CustomersSource"
AutoGenerateColumns="False"
EmptyDataText="No data available."
AllowPaging="True"
runat="server" DataKeyNames="CustomerID">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID"
InsertVisible="False" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
</Columns>
</asp:GridView>
I'm building a GridView based on what is passed on the previous page querystring.
Is there a way to display a message saying 'no records found' if the GridView isn't populated? i.e. the query produces no records to populate the GridView.
code:
<div class="centergrid">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" CssClass="bookgridview">
<Columns>
<asp:TemplateField HeaderText="Choose">
<ItemTemplate>
<asp:HyperLink ID="lnkSelect" runat='server' NavigateUrl='<%# String.Format("~/bookingform.aspx?ID={0}", Eval("ScheduleId")) %>'>Book</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TName" HeaderText="Name" SortExpression="TName" />
<asp:BoundField DataField="SDate" HeaderText="Date" SortExpression="SDate" DataFormatString="{0:d}" />
<asp:BoundField DataField="STime" HeaderText="Time" SortExpression="STime" />
<asp:BoundField DataField="TPriceadult" HeaderText="Price (Adult)"
SortExpression="TPriceadult" DataFormatString="{0:C}" />
<asp:BoundField DataField="TPricesenior" HeaderText="Price (Student/Senior)"
SortExpression="TPricesenior" DataFormatString="{0:C}" />
<asp:BoundField DataField="TPricechild" HeaderText="Price (Child)"
SortExpression="Tpricechild" DataFormatString="{0:C}" />
<asp:BoundField DataField="TDuration" HeaderText="Duration"
SortExpression="TDuration" />
<asp:BoundField DataField="ScheduleId" HeaderText="ScheduleId"
SortExpression="ScheduleId" Visible="False" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ChinatowndbConnString %>"
SelectCommand="SELECT * FROM [vwSchedule] Where TourId=#tid">
<SelectParameters>
<asp:QueryStringParameter DbType="Int32" Name="tid" QueryStringField="tourid" />
</SelectParameters>
</asp:SqlDataSource>
Is there a way to display a message saying 'no records found' if the
GridView isn't populated?
Yes, with the EmptyDataTemplate.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" CssClass="bookgridview">
<emptydatarowstyle backcolor="LightBlue"
forecolor="Red"/>
<emptydatatemplate>
<asp:image id="NoDataImage"
imageurl="~/images/Image.jpg"
alternatetext="No Image"
runat="server"/>
No records found.
</emptydatatemplate>
<Columns>
<asp:TemplateField HeaderText="Choose">
<ItemTemplate>
<asp:HyperLink ID="lnkSelect" runat='server' NavigateUrl='<%# String.Format("~/bookingform.aspx?ID={0}", Eval("ScheduleId")) %>'>Book</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TName" HeaderText="Name" SortExpression="TName" />
<asp:BoundField DataField="SDate" HeaderText="Date" SortExpression="SDate" DataFormatString="{0:d}" />
<asp:BoundField DataField="STime" HeaderText="Time" SortExpression="STime" />
<asp:BoundField DataField="TPriceadult" HeaderText="Price (Adult)"
SortExpression="TPriceadult" DataFormatString="{0:C}" />
<asp:BoundField DataField="TPricesenior" HeaderText="Price (Student/Senior)"
SortExpression="TPricesenior" DataFormatString="{0:C}" />
<asp:BoundField DataField="TPricechild" HeaderText="Price (Child)"
SortExpression="Tpricechild" DataFormatString="{0:C}" />
<asp:BoundField DataField="TDuration" HeaderText="Duration"
SortExpression="TDuration" />
<asp:BoundField DataField="ScheduleId" HeaderText="ScheduleId"
SortExpression="ScheduleId" Visible="False" />
</Columns>
</asp:GridView>
Alternatively, you can use the built-in UI for the empty data row by setting the EmptyDataText property instead of this property.
I have a Detailsview object that is loaded with user data when a User is clicked from a gridview. The problem is, I am using a sqldatasource and I'd rather use my pre-exisiting user class which has all the built in functionality for this. I haven't been able to get this done correctly. I can't get the detailsview populated with user data without the sqldatasource, and when I try inserting or updating with my objects, it runs through the insert/update code, then fails afterwards because I don't have a InsertCommand, etc for my SQLDataSource....bottom line...can somebody help me get this working without needing the SQLDataSource?
Here's my code.
<asp:GridView runat="server" ID="gvUsers" DataKeyNames="UserID" BackColor="#eeeeee" Width="85%"
HorizontalAlign="Center"
Font-Bold="True" Font-Names="Verdana"
Font-Size="10pt" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound"
OnRowDeleting="GridView1_RowDeleting" >
<HeaderStyle BackColor="Black" ForeColor="White"
Font-Bold="True" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="yellow" ForeColor="blue" />
<AlternatingRowStyle BackColor="#ffffff" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2"
CommandArgument='<%# Eval("UserID") %>'
CommandName="Select" runat="server">
Select</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserID" Visible="false" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
CommandArgument='<%# Eval("UserID") %>'
CommandName="Delete" runat="server">
Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView><br /><br />
<asp:DetailsView runat="server" ID="dvUser" DataSourceID="SqlDataSource3" AutoGenerateRows="False" Width="85%"
HorizontalAlign="Center" DataKeyNames="UserID">
<Fields>
<asp:BoundField DataField="UserID" Visible="false" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="UserName" HeaderText="User Name" />
<asp:BoundField DataField="Password" HeaderText="Password" />
<asp:BoundField DataField="Birthdate" HeaderText="Birthdate" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="Apt" HeaderText="Apt" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Province" HeaderText="Province" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
<asp:BoundField DataField="PhoneNum" HeaderText="PhoneNum" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="ynAdminUser" HeaderText="ynAdminUser" />
<asp:CommandField ShowDeleteButton="False" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:ConnectionString%>" ID="SqlDataSource3"
runat="server" SelectCommand="sp_GetUser" SelectCommandType="StoredProcedure" OnInserting="OnInserting" >
<SelectParameters>
<asp:ControlParameter ControlID="gvUsers" Name="UserID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Not sure the CodeBehind is necessary, I just want to use that to call my data object code for updating, insert etc
Why not use a ObjectDataSource. This control works much like an SqlDataSource but instead of specifying an SQL query or Stored Procedure you specify the methods of your custom business object to perform your data access.
<asp:ObjectDataSource ID="ObjectDataSource" Drunat="server" DeleteMethod="Delete" InsertMethod="Insert" SelectMethod="Select" UpdateMethod="Update" TypeName="YourType">
Here's an example illustrating how to use an ObjectDataSource with a DetailsView.