How to display a message when GridView isn't populated - asp.net

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.

Related

update query not changed

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>

Getting invalid character error when deleting a gridview row

I was assigned to convert some .Net projects in old version to to .net 4.0 version. I used ODAC driver replacing ODBC driver. Everything went fine until I found a problem with a gridview were I get illegal character Oracle exception when I try to delete a row. The gridview is inside an update panel. The exception I receive is Microsoft Jscript run time error:Sys.WebForms.PageRequestManagerServerErrorException: ORA-00911:invalid character. The following is the code for the grid view and the sqldatasource. The linkbutton triggers the delete using the deleteparameters of the sqldatasource.
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("CODE_OWNER_ID", "default.aspx?id={0}") %>'>View</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CODE_OWNER_ID" Visible ="False" HeaderText="CODE OWNER ID" />
<asp:BoundField DataField="CODE_TYPE" ReadOnly ="True" HeaderText="CODE TYPE" />
<asp:BoundField DataField="CODE" ReadOnly="True" HeaderText="CODE" />
<asp:BoundField DataField="UNIT_CODE" ReadOnly ="True" HeaderText="UNIT CODE" />
<asp:BoundField DataField="UNIT_NAME" ReadOnly="True" HeaderText="UNIT NAME" />
<asp:BoundField DataField="UNIT_DO" ReadOnly="True" HeaderText="UNIT DO" />
<asp:BoundField DataField="OWNER" ReadOnly="True" HeaderText="OWNER" />
<asp:BoundField DataField="OWNER_EMAIL" ReadOnly="True" HeaderText="OWNER EMAIL" />
<asp:BoundField DataField="OWNER_PHONE" ReadOnly="True" HeaderText="OWNER PHONE" />
<asp:BoundField DataField="OWNER_MAILPOINT" ReadOnly="True" HeaderText="OWNER MAILPOINT" />
<asp:BoundField DataField="CODE_ESTABLISHED_DATE" ReadOnly="True" DataFormatString="{0:MM/dd/yyyy}" HeaderText="CODE ESTABLISHED DATE" />
<asp:BoundField DataField="LAST_REVIEW_DATE" ReadOnly="True" DataFormatString="{0:MM/dd/yyyy}" HeaderText="LAST REVIEW DATE" />
<asp:BoundField DataField="REVIEW_USERNAME" ReadOnly="True" HeaderText="REVIEW USERNAME" />
<asp:BoundField DataField="CODE_DELETED_DATE" ReadOnly="True" DataFormatString="{0:MM/dd/yyyy}" HeaderText="CODE DELETED DATE" />
<asp:BoundField DataField="DELETE_REASON" ReadOnly="True" HeaderText="DELETE REASON" />
<asp:BoundField DataField="UPDATE_USERNAME" ReadOnly="True" HeaderText="UPDATE USERNAME" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
Text='Delete'
OnClientClick="return confirm('Are you sure you want to delete this record?');"
CommandArgument='<%# bind("CODE_OWNER_ID") %>'></asp:LinkButton>
</ItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" ForeColor="Navy" Font-Bold="True" />
<HeaderStyle BackColor="#990000" Font-Bold="True" HorizontalAlign="Center" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnAdd" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="Sqldatasource1" runat="server" ConnectionString="<%$ ConnectionStrings:Conn_Code_Owner %>"
ProviderName="<%$ ConnectionStrings:Conn_Code_Owner.ProviderName %>"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM CODE_OWNER"
DeleteCommand="DELETE FROM CODE_OWNER WHERE CODE_OWNER_ID = ?"
OnSelecting="_data_Selecting"
ConflictDetection="CompareAllValues" >
<DeleteParameters>
<asp:Parameter Name="Original_CODE_OWNER_ID" Type="Decimal"/>
</DeleteParameters>
</asp:SqlDataSource>
Try and wrap it w/ a Begin and End
BEGIN CODE_OWNER WHERE CODE_OWNER_ID = ? END
http://www.lazyasscoder.com/Article.aspx?id=89&title=ora-00911%3A+invalid+character+when+executing+multiple+Oracle+SQL+statements
I found an easy alternative using row-deleting event of the grid-view. I Just removed the delete parameters in the sqldatasource of asp page and then I set conflict-detection to overwrite-changes.
Sqldatasource1.DeleteCommand = "DELETE FROM code_owner WHERE code_owner_ID = :original_code_owner_ID"
Sqldatasource1.DeleteParameters.Add("original_code_owner_ID", GridView1.DataKeys(e.RowIndex).Values("code_owner_ID").ToString)
Sqldatasource1.Delete()
GridView1.DataBind()

keeping GridView checkbox state when posting back

I have a GridView which with a CheckBox in a TemplateField.
When I check something and hit the submit button, the items that I have checked are not check on the postback.
I tried using EnableViewState and that did not help.
How can this be fixed?
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="SELECT">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ICAO" HeaderText="ICAO" />
<asp:BoundField DataField="IATA" HeaderText="IATA" />
<asp:BoundField DataField="AIRPORT_NAME" HeaderText="AIRPORT NAME" />
<asp:BoundField DataField="CITY" HeaderText="CITY" />
<asp:BoundField DataField="COUNTRY" HeaderText="COUNTRY" />
<asp:BoundField DataField="REVISED_DATE" HeaderText="REVISED DATE" />
<asp:BoundField DataField="EMAIL_DATE" HeaderText="EMAIL DATE" />
</Columns>
</asp:GridView>
You need to override LoadViewState and SaveViewState methods to maintain the selection.
https://web.archive.org/web/20211020153240/https://www.4guysfromrolla.com/articles/110205-1.aspx#postadlink

Help with ASP.NET ObjectDataSource

I have a User object with a Load method that takes in a UserId parameter and loads that users data to the objects member variables.
Now what I'd like to do is load this data to a DetailsView control using an ObjectDataSource, but I'm stumped as to how.
First of all, I'm not sure I've got the code set up properly to pass the parameter (UserID) to the SelectMethod (cUser.Load). Secondly, I don't know how I can load this data to the DetailsView since I'm not actually returning the results from the Load method, I'm simply loading the object with the data...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"
OnSelectedIndexChanged="IndexChanged" >
<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="ObjectDataSource1" 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:ObjectDataSource ID="ObjectDataSource1"
runat="server" SelectMethod="Load" TypeName="cUser">
<SelectParameters>
<asp:ControlParameter ControlID="gvUsers" PropertyName="SelectedValue" Name="iUserID" Type="int32" />
</SelectParameters>
</asp:ObjectDataSource>
is there a way to do what I'm trying to achieve? if so, can you please give examples with the explanation?
Very much appreciated. Thanks
you need to check this tutorial for detail help....
Master/Detail Using a Selectable Master GridView with a Details DetailView

Using a detailsview without sqldatasource

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.

Resources