Why is my gridview empty? - c#

I set up an Sqldatasource and a Gridview:
<asp:SqlDataSource ID="DetailsSQLDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-WebApplication6-20131007103938ConnectionString1 %>"
SelectCommand="SELECT [ProjectID], [ProjectName], [CreatorID], [Deadline] FROM [tProject] WHERE 1=1"
>
<SelectParameters>
<asp:Parameter Type="String" Name="ProjectID"></asp:Parameter>
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="DetailsSQLDataSource" AutoGenerateColumns="False" DataKeyNames="ProjectID" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="ProjectID" HeaderText="ProjectID" ReadOnly="True" SortExpression="ProjectID"></asp:BoundField>
<asp:BoundField DataField="ProjectName" HeaderText="ProjectName" SortExpression="ProjectName"></asp:BoundField>
<asp:BoundField DataField="CreatorID" HeaderText="CreatorID" SortExpression="CreatorID"></asp:BoundField>
<asp:BoundField DataField="Deadline" HeaderText="Deadline" SortExpression="Deadline"></asp:BoundField>
</Columns>
</asp:GridView>
I have tested the selectcommand, and it returns a table with 4 rows. Yet there is no data to show in the gridview. The codebehind is almost empty and surely doesn't affect these controls. Can it be something with the connection? How can I test that?

Call .DataBind() to actually force the data to be bound to the grid, like this:
GridView1.DataBind();

Related

Procedure or function has too many arguments specified in FormView and SQL SERVER c#

I have one stored procedure called VideoGalleryDelete . I am using FormView and Gridview to insert, update, show and delete data. VideoGallery is my primary table with PK VideoId and it has a foreign key as CategoryId .
When i click Delete linkButton, its says Procedure or function has too many arguments specified
This is my SP :
ALTER PROCEDURE [dbo].[VideoGalleryDelete]
#VideoId int
AS
BEGIN
DELETE FROM [dbo].[VideoGallery]
WHERE VideoId = #VideoId
END
GridView and its assigned SqlDataSource :
<asp:GridView ID="gvVideo" CssClass="table" runat="server" OnSelectedIndexChanged="gvVideo_SelectedIndexChanged" AutoGenerateColumns="False" DataKeyNames="VideoId,CategoryId" DataSourceID="sdVideoList">
<Columns>
<asp:BoundField DataField="VideoId" Visible="false" HeaderText="VideoId" InsertVisible="False" ReadOnly="True" SortExpression="VideoId" />
<asp:BoundField DataField="VideoLink" HeaderText="Video Link" SortExpression="VideoLink" />
<asp:BoundField DataField="CategoryId" HeaderText="Category Id" SortExpression="CategoryId" />
<asp:TemplateField ItemStyle-CssClass="Center" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:ImageButton ImageUrl="~/images/edit.png" ID="lnkedit" Style="margin-left: 15px" CommandName="Select" runat="server" Width="20px"></asp:ImageButton>
<asp:ImageButton ImageUrl="~/images/delete.png" ID="lnkDelete" CommandName="Delete" Style="margin-left: 15px" runat="server" Width="20px" OnClientClick="return ConfirmDelete();"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdVideoList" runat="server" ConnectionString="<%$ ConnectionStrings:WebAAERT_DBConnectionString %>" DeleteCommand="VideoGalleryDelete" DeleteCommandType="StoredProcedure" SelectCommand="VideoGallerySelect" SelectCommandType="StoredProcedure">
<DeleteParameters>
<asp:Parameter Name="VideoId" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="CategoryId" Type="Int32" />
</SelectParameters>
I think you're misusing the DataKeyNames property of the grid view control.This property should ideally contain only the primary key field(s) to identify the current row.Also every single value set on this property will be passed through to the Delete command.
Your delete stored proc takes one parameter but two values are being passed through beacuase of DataKeyNames="VideoId,CategoryId".So you can do what #Chris Flynn suggested or change your grid view to DataKeyNames="VideoId"

dropdownlist populate gridview

I am using the wizard in Visual Studio to define the a data source that populates a dropdownlist. This is working fine. Then I have a second datasource that should filter data from another table using the selected value from the dropdownlist and display it in a gridview. When I make my selection in the dropdownlist I thought the gridview should changed automatically to show the new filtered data. But it is not. Seems like I am missing an auto post back or something like that. Below is the code I am using.
<form id="appointmentform" runat="server">
<div class="wdn-band" id="findpersonband" runat="server" visible="true">
<div class="wdn-inner-wrapper wdn-inner-padding-sm">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SNR_InformationConnectionString %>" SelectCommand="SELECT [personnelid], [lastname] FROM [tblmasterpersonnel] ORDER BY [lastname], [preferredfirstname]"></asp:SqlDataSource>
<asp:DropDownList ID="MasterPersonnelDDL" runat="server" DataSourceID="SqlDataSource1" DataTextField="lastname" DataValueField="personnelid"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:SNR_InformationConnectionString %>" SelectCommand="SELECT [personnelid], [startdate], [endingdate], [employeetypeid], [dateentered] FROM [tblappointmentdate] WHERE ([personnelid] = #personnelid)">
<SelectParameters>
<asp:ControlParameter ControlID="MasterPersonnelDDL" Name="personnelid" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="startdate" HeaderText="startdate" SortExpression="startdate" />
<asp:BoundField DataField="endingdate" HeaderText="endingdate" SortExpression="endingdate" />
<asp:BoundField DataField="employeetypeid" HeaderText="employeetypeid" SortExpression="employeetypeid" />
<asp:BoundField DataField="dateentered" HeaderText="dateentered" SortExpression="dateentered" />
</Columns>
</asp:GridView>
</div>
</div>
</form>
Thanks for the help
may be you could use
<asp:DropDownList ID="MasterPersonnelDDL" runat="server" DataSourceID="SqlDataSource1" DataTextField="lastname" DataValueField="personnelid" AutoPostBack="True"></asp:DropDownList>

Delete link in gridview deleting all row

I am facing a problem with my gridview as the Delete link is deleting all the rows where i want to delete one by one. I have record my screen that may exolain more about my problem
https://www.youtube.com/watch?v=VAh0A-UOafU&feature=youtu.be
<asp:GridView ID="FavoritGRDSHOW" runat="server" AllowPaging="True"
DataKeyNames="FavID" DataSourceID="FavoriteGRDView"
PageSize="6" Width="600px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ButtonType="Image"
DeleteImageUrl="~/iconsimg/Delete2.png" />
<asp:BoundField DataField="FavID" HeaderText="FavID"
InsertVisible="False" ReadOnly="True" SortExpression="FavID" />
<asp:BoundField DataField="AdsID" HeaderText="AdsID"
SortExpression="AdsID" />
<asp:BoundField DataField="UID" HeaderText="UID" SortExpression="UID" />
<asp:BoundField DataField="AdsTit" HeaderText="AdsTit"
SortExpression="AdsTit" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="FavoriteGRDView" runat="server"
ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>"
SelectCommand="SELECT [FavID], [AdsID], [UID], [AdsTit] FROM [favourite]
WHERE ([UID] = #UID)"
OldValuesParameterFormatString="original_{0}"
DeleteCommand="DELETE FROM [favourite] WHERE [FavID] = FavID" >
<SelectParameters>
<asp:SessionParameter Name="UID" SessionField="UsrNme" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Your delete statement:
DELETE FROM [favourite] WHERE [FavID] = FavID
Is equivalent to this, because every record is going to match its own fields:
DELETE FROM [favourite] // deletes all rows in the table
You need to specify which ID to delete and pass it as a parameter:
<asp:SqlDataSource ID="FavoriteGRDView" runat="server" ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>"
DeleteCommand="DELETE FROM [favourite] WHERE [FavID] = #FavID">
<DeleteParameters>
<asp:Parameter Name="FavID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
You can read more here.
use this:
<asp:SqlDataSource ID="FavoriteGRDView" runat="server" ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>"
DeleteCommand="DELETE FROM [favourite] WHERE [FavID] = #FavID">
<DeleteParameters>
<asp:ControlParameter ControlID="FavoritGRDSHOW" PropertyName="SelectedValue" Name="FavID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>

Editable GridView

I'm trying to allow for editing of the items in a GridView (the DataSource is a database connection). Every example I find has complicated examples implemented within. I'd like to know what the simplest change(s) I need to do are in order to allow for editing of items in the GridView.
In other words, how can I modify the following to allow for editing?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSourceWS">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME" />
<asp:BoundField DataField="ACCESS_TO" HeaderText="ACCESS_TO" SortExpression="ACCESS_TO" />
</Columns>
</asp:GridView>
Obviously I'd need to add code as well, but I'm just not sure how to start with this.
EDIT: I thought I'd specified, but I didn't - it's an SQLDataSource.
There are several thing you need to do to enable editing
Assuming that your data source is an SQLDataSource
1) Add a command field to your girdview columns
<asp:CommandField ButtonType="Button" EditText="Edit" ShowEditButton="true"/>
2) Add an update command to your data source
<asp:SqlDataSource ID="SqlDataSourceWS" runat="server"
ConnectionString="<%$ Connection String %>"
SelectCommand=" SELECT COMMAND HERE "
UpdateCommand=" UPDATE COMMAND HERE ">
<UpdateParameters>
<asp:Parameter Name="" />
<asp:Parameter Name="" />
</UpdateParameters>
</asp:SqlDataSource>
For more information on the command field and how to use it you can look at the Microsoft Developer Network Documentation
EDIT
Here is a very simple example of a SQLDatasource that shows how you may update an item
<asp:SqlDataSource ID="sqlMeetings" runat="server"
ConnectionString="<%$ connection %>"
SelectCommand="SELECT [meetingid]
,[groupname]
,[meetingtime]
,[meetingdate]
FROM [DCMS].[dbo].[tbl_meetings] "
UpdateCommand="UPDATE tbl_meetings
SET meetingdate = #meetingdate, meetingtime = #meetingtime
WHERE meetingid = #meetingid">
<UpdateParameters>
<asp:Parameter Name="meetingdate" />
<asp:Parameter Name="meetingtime" />
</UpdateParameters>
</asp:SqlDataSource>
From the example above you can see that I am selecting 3 fields from the data base but only allowing for two to be updated (meetingdate and meetingtime).
To edit the records, the GridView must enter EditMode. The grid changes modes according to the commands it receives, in this case "edit". See the remarks on the gridview page, 'Data Operations' section, for more info.
There are basically three ways to fire the command and enter edit mode on the grid:
Use the AutoGenerateEditButton property on the grid
Use a CommandField with ShowEditButton like the other answer stated
Use a custom TemplateField with a LinkButton inside like this:
<Columns>
...
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" Text="Update" CommandName="Update" />
<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
...
</Columns>
After that, it will depend on which DataSource control you are using. With an EntityDataSource, for instance, all you need to do is set EnableUpdate="true"

SQLDataSource DeleteCommand to get parameters

I am having some problems passing parameters to a DELETE command, and cant seem to get a good understanding on how it works.
<asp:SqlDataSource ID="sdsPropertyList"
runat="server"
ProviderName="<%$ appSettings:ProviderName %>"
ConnectionString="<%$ appSettings:ConnectionString %>"
SelectCommand="selPropertyByAcntID"
SelectCommandType="StoredProcedure"
OnSelecting="sdsPropertyList_Selecting"
OnSelected="sdsPropertyList_Selected"
DeleteCommand="delPropertyByPropID"
DeleteCommandType="StoredProcedure"
OnDeleting="sdsPropertyList_Deleting"
OnDeleted="sdsPropertyList_Deleted">
<SelectParameters>
<asp:Parameter Name="in_acntID" Type="Int32" DefaultValue="0" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="in_acntID" Type="Int32" DefaultValue="0" />
<asp:Parameter Name="in_propID" Type="Int32" DefaultValue="0" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:GridView ID="gvProperty" runat="server" DataSourceID="sdsPropertyList"
AutoGenerateColumns="false" CssClass="gvPropList">
<Columns>
<asp:BoundField HeaderText="ID" InsertVisible="true" DataField="prop_id" ReadOnly="true" Visible="False" />
<asp:BoundField HeaderText="Property" DataField="prop_title"
ItemStyle-CssClass="gvPropTitle" >
<ItemStyle CssClass="gvPropTitle" />
</asp:BoundField>
<asp:BoundField HeaderText="Units" DataField="unitCount"
ItemStyle-CssClass="gvUnitCount" >
<ItemStyle CssClass="gvUnitCount" />
</asp:BoundField>
<asp:BoundField DataField="prop_lastmodified" HeaderText="Last Modified"
ItemStyle-CssClass="gvDate" DataFormatString="{0:M/dd/yyyy hh:mm tt}" >
<ItemStyle CssClass="gvDate" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="EditRecord" Text="Edit" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton>
<asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnAdd" runat="server" CommandName="AddRecord" Text="Add" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="headerPropList"/>
<RowStyle CssClass="gvPropRow" />
</asp:GridView>
protected void sdsPropertyList_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
int userID = Convert.ToInt32(Page.User.Identity.Name);
if (userID != 0)
e.Command.Parameters["in_acntID"].Value = userID;
}
protected void sdsPropertyList_Deleting(object sender, SqlDataSourceCommandEventArgs e)
{
int userID = Convert.ToInt32(Page.User.Identity.Name);
if (userID != 0)
{
e.Command.Parameters["in_acntID"].Value = userID;
}
}
The SELECT statement is straightforward which requires one input parameter of userID.
However, the DELETE statement, requires 2 parameter inputs.
in_acntID = userID
in_propID = the boundfield datafield prop_id
What am I doing wrong? and should the CommandName and CommandArgument be passed at the ItemTemplate level if I have it defined at the SqlDataSource level?
I want the delete button to achieve the following:
delete records from table(s) in DB
remove the row from the gridview
UPDATE
After some additional research, I've found that, the NAME for parameters and HeaderText for Boundfield must be the same so that the values within your gridview can be used by the SQL commands of the datasource.
With the exception of the initial select commant, i have removed all the code behind references.
All is working corrently now.
According to the MSDN documentation, you need to specify the DataKeyNames on the gridView:
"Use the DataKeyNames property to specify the field or fields that represent the primary key of the data source. You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work. The values of these key fields are passed to the data source control in order to specify the row to update or delete."
e.g. if the id was in a listbox or dropdown
<DeleteParameters>
<asp:ControlParameter ControlID="controlname" Name="id" PropertyName="SelectedValue" Type="Int32" />
</DeleteParameters>
i have used the above successfully for deleting . this could work for textboxes or labels. If you are handling the event , why not just take the entire delete process to the even handler ? Specify the entire sql setup including connection , command execution there . This method has also worked for me .
After some additional research, I've found that, the NAME for parameters and HeaderText for Boundfield must be the same so that the values within your gridview can be used by the SQL commands of the datasource.
With the exception of the initial select commant, i have removed all the code behind references.
All is working corrently now.
<asp:SqlDataSource ID="sdsPropertyList"
runat="server"
ProviderName="<%$ appSettings:ProviderName %>"
ConnectionString="<%$ appSettings:ConnectionString %>"
SelectCommand="selPropertyByAcntID"
SelectCommandType="StoredProcedure"
OnSelecting="sdsPropertyList_Selecting"
DeleteCommand="delPropertyByPropID"
DeleteCommandType="StoredProcedure"
OnDeleted="sdsPropertyList_Deleted" >
<SelectParameters>
<asp:Parameter Name="acnt_id" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="acnt_id" Type="Int32" />
<asp:Parameter Name="prop_id" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:GridView ID="gvProperty" runat="server" DataSourceID="sdsPropertyList"
AutoGenerateColumns="false" CssClass="gvPropList" DataKeyNames="acnt_id, prop_id">
<Columns>
<asp:BoundField HeaderText="acnt_id" InsertVisible="true" DataField="acnt_id" ReadOnly="true" Visible="False" />
<asp:BoundField HeaderText="prop_id" InsertVisible="true" DataField="prop_id" ReadOnly="true" Visible="False" />
<asp:BoundField HeaderText="Property" DataField="prop_title">
<ItemStyle CssClass="gvPropTitle" />
</asp:BoundField>
<asp:BoundField HeaderText="Units" DataField="unitCount" >
<ItemStyle CssClass="gvUnitCount" />
</asp:BoundField>
<asp:BoundField DataField="prop_lastmodified" HeaderText="Last Modified"
ItemStyle-CssClass="gvDate" DataFormatString="{0:M/dd/yyyy hh:mm tt}" >
<ItemStyle CssClass="gvDate" />
</asp:BoundField>
<asp:BoundField HeaderText="Active" DataField="prop_active">
<ItemStyle CssClass="gvPropActive" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="EditRecord" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnAdd" runat="server" CommandName="AddRecord" Text="Add"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="headerPropList"/>
<RowStyle CssClass="gvPropRow" />
</asp:GridView>

Categories