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>
Related
I have a GridView and its work correctly. But it does not show True False Value. Whats the problem?
<asp:GridView ID="GridView1" runat="server" Width="1048px" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" BorderColor="#663300" Visible="False">
<Columns>
<asp:BoundField DataField="SeatNumber" HeaderText="SeatNumber" SortExpression="SeatNumber" />
<asp:BoundField DataField="customer_id" HeaderText="customer_id" SortExpression="customer_id" />
<asp:CheckBoxField DataField="Reserved" HeaderText="Reserved" SortExpression="Reserved" />
<asp:BoundField DataField="Time" HeaderText="Time" SortExpression="Time" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="SeatPrice" HeaderText="SeatPrice" SortExpression="SeatPrice" />
<asp:BoundField DataField="EventName" HeaderText="EventName" SortExpression="EventName" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="family" HeaderText="family" SortExpression="family" />
</Columns>
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AraratComplexConnectionString %>" SelectCommand="SELECT dbo.SingleSeatTable.SeatNumber, dbo.SingleSeatTable.customer_id, dbo.SingleSeatTable.Reserved, dbo.Event_SingleReservation.Time, dbo.Event_SingleReservation.Date,
dbo.Event_SingleReservation.SeatPrice, dbo.Event_SingleReservation.EventName, dbo.Registration.name, dbo.Registration.family
FROM dbo.Registration INNER JOIN
dbo.SingleSeatTable ON dbo.Registration.customer_id = dbo.SingleSeatTable.customer_id INNER JOIN
dbo.Event_SingleReservation ON dbo.SingleSeatTable.EventCode = dbo.Event_SingleReservation.EventCode"></asp:SqlDataSource>
I am trying to display the 7 days of the week inside GridView header so my code below shows how to get those days but I don't know how to put them in Gridview Header
with day of names of the week. About like image shows. I run ASP.NET with SQL SERVER EXPRESS database use sqldatasource connection to select all employees to a gridview.
foreach (DateTime selectedDateTime in Calendar1.SelectedDates)
{
Response.Write(selectedDateTime.ToShortDateString() + " <br/>");
}
You can create the columns in the GridView this way:
<asp:GridView ID="GridView1" runat="server" ShowHeader="true" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Monday" HeaderText="Monday" />
<asp:BoundField DataField="Thuesday" HeaderText="Thuesday" />
<asp:BoundField DataField="Wednesday" HeaderText="Wednesday" />
<asp:BoundField DataField="Thursday" HeaderText="Thursday" />
<asp:BoundField DataField="Friday" HeaderText="Friday" />
<asp:BoundField DataField="Saturday" HeaderText="Saturday" />
<asp:BoundField DataField="Sunday" HeaderText="Sunday" />
</Columns>
</asp:GridView>
If no data is expected in the days of the week columns, you could use TemplateFields:
<asp:GridView ID="GridView1" runat="server" ShowHeader="true" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:TemplateField HeaderText="Monday" />
<asp:TemplateField HeaderText="Thuesday" />
<asp:TemplateField HeaderText="Wednesday" />
<asp:TemplateField HeaderText="Thursday" />
<asp:TemplateField HeaderText="Friday" />
<asp:TemplateField HeaderText="Saturday" />
<asp:TemplateField HeaderText="Sunday" />
</Columns>
</asp:GridView>
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.
I have this gridview, that works perfectly when running in local computer. Platform is VWD 2010 - Sql Server
<asp:GridView ID="Gv_Usuarios" runat="server"
AutoGenerateColumns="False"
DataKeyNames="Login,ID" DataSourceID="SqlDataSource1" EnableModelValidation="True"
Width="619px">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False" CommandName="Delete" OnClientClick='if
(!confirm("Borra?")) return false;' Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Tipo" HeaderText="Tipo" SortExpression="Tipo" />
<asp:BoundField DataField="Nombre" HeaderText="Nombre" SortExpression="Nombre" />
<asp:BoundField DataField="Login" HeaderText="Login" ReadOnly="True"
SortExpression="Login" />
<asp:BoundField DataField="Password" HeaderText="Password"
SortExpression="Password" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="Vendedor" HeaderText="Vendedor"
SortExpression="Vendedor" />
<asp:BoundField DataField="Sucursal" HeaderText="Sucursal"
SortExpression="Sucursal" />
<asp:BoundField DataField="Almacen" HeaderText="Almacen"
SortExpression="Almacen" />
<asp:BoundField DataField="Fecha" HeaderText="Fecha" SortExpression="Fecha" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
</Columns>
</asp:GridView>
Browser fire the confirmation question, but after hittting YES, nothing happens.
It works fine in my computer, nothing happens in the hosting server.
Thanks in advance.
Use asp:Button instead of asp:LinkButton
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.