grid view update - c#

I had grid view which bind sql data source and added field to update data in grid view but it did not do any update and no error appeared and I did not know where the error
SQL Stored Procedures:
ALTER proc [dbo].[GetNewswithType]
As
Begin
Select News.Id,News.Type_Id,
News.Header,News.HText,News.DText,News.Active,News.Add_Date,
NewsType.Type_AR,NewsType.Type_EN
From News
Inner Join NewsType On
NewsType.Id=News.Type_Id
End
ALTER Proc [dbo].[UpdateNews]
(
#Id Int
,#Header Nvarchar(50)
,#HText Nvarchar(Max)
,#DText Nvarchar(Max)
,#Type_Id Int
,#Active Bit
)
AS
BEGIN
Update News Set
#Header =Header
,#HText =HText
,#DText =DText
,#Type_Id=Type_Id
,#Active =Active
WHERE #Id=Id
END
ASPX Page:
<div class="m10">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
CellPadding="4" DataKeyNames="Id" DataSourceID="SDSNews" ForeColor="Black"
GridLines="Vertical"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onselectedindexchanging="GridView1_SelectedIndexChanging"
onpageindexchanging="GridView1_PageIndexChanging" AllowPaging="True"
onrowupdated="GridView1_RowUpdated"
onrowdatabound="GridView1_RowDataBound">
<FooterStyle BackColor="#CCCC99" />
<RowStyle BackColor="#F7F7DE" />
<Columns>
<asp:CommandField HeaderText="Function" ShowEditButton="True" />
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id"
Visible="False" />
<asp:BoundField DataField="Header" HeaderText="Header"
SortExpression="Header" />
<asp:BoundField DataField="HText" HeaderText="HomeText"
SortExpression="HText" />
<asp:BoundField DataField="DText" HeaderText="DetailsText"
SortExpression="DText" />
<asp:BoundField DataField="Type_Id" HeaderText="TypeNumber"
SortExpression="Type_Id" />
<asp:BoundField DataField="Type_AR" HeaderText="Type_AR"
SortExpression="Type_AR" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="Type_EN" HeaderText="Type_EN"
SortExpression="Type_EN" InsertVisible="False" ReadOnly="True" />
<asp:CheckBoxField DataField="Active" HeaderText="Active"
SortExpression="Active" />
<asp:BoundField DataField="Add_Date" HeaderText="Add_Date"
SortExpression="Add_Date" InsertVisible="False" ReadOnly="True" />
</Columns>
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SDSNews" runat="server"
ConnectionString="Data Source=ELARABY-1EACFA3\SQLEXPRESS;Initial Catalog=ElarabyGroup;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="GetNewswithType"
SelectCommandType="StoredProcedure" UpdateCommand="UpdateNews"
UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:ControlParameter ControlID="GridView1" Name="Id"
PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="GridView1" Name="Header"
PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="GridView1" Name="HText"
PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="GridView1" Name="DText"
PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="GridView1" Name="Type_Id"
PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="GridView1" Name="Active"
PropertyName="SelectedValue" Type="Boolean" />
</UpdateParameters>
</asp:SqlDataSource>
</div>

Add some code to your update procedure to determine if it's being called:
Create a table called Logging (or any name of your choice) that has a column called Message (again, or any name of your choice)
Add a line to your UpdateNews procedure to INSERT INTO [dbo].[Logging] (Message) VALUES ('Procedure Called'). You could log the values of the parameters passed in, if you so wished.
By doing this you can determine if the stored procedure is actually being called or not. The other option is to use Sql Server Profiler to determine this. If you can determine that it is calling the stored procedure, then something is going wrong in there and by logging the parameters you can "hand call" it to find out why.
More likely is that the stored procedure is never being called, how are you telling the grid to persist the changes back out to the database? Looking at this tutorial, I would suggest that your updates are not triggering a "save".

Related

How do you stop ReadOnly columns from disappearing when editing/updating a GridView in ASP.Net?

I'm trying to create a GridView to display the contents of a table on my database. I want the user to be able to edit only one of the columns, so I set every other one to read only. When I test this, I can only edit the one column, like intended, but after updating, the other columns become null and post to the database. I don't know how to stop this from happening. Here is my code for the GridView. It is bound with one of Visual Studio's SQLDataSources.
I tried to create several events including RowEditing, RowUpdated, and RowUpdating, but with no luck. Nothing would stop the non-edited fields from getting replaced with NULL.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Align="center" Height="138px" Width="548px" BorderColor="#6600FF" BorderStyle="Ridge" HorizontalAlign="Center" DataKeyNames="Id" DataSourceID="SqlDataSource1" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" Visible="False" />
<asp:BoundField DataField="PhoneName" HeaderText="PhoneName" SortExpression="PhoneName" ReadOnly="True" />
<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" ReadOnly="True" />
<asp:BoundField DataField="TotalPrice" HeaderText="TotalPrice" SortExpression="TotalPrice" ReadOnly="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Edit: I found a working solution by changing the SQLDataSource's Update Query String to set the unchangeable categories to their original values and leaving the one changeable field to a parameter. I'll post the code for that here.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="*connection string here*" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Cart]" DeleteCommand="DELETE FROM [Cart] WHERE [Id] = #original_Id" InsertCommand="INSERT INTO [Cart] ([PhoneName], [Amount], [Price], [TotalPrice]) VALUES (#PhoneName, #Amount, #Price, #TotalPrice)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [Cart] SET [PhoneName] = PhoneName, [Amount] = #Amount, [Price] = Price, [TotalPrice] = TotalPrice WHERE [Id] = #original_Id" DataSourceMode="DataReader">
<DeleteParameters>
<asp:Parameter Name="original_Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="PhoneName" Type="String" />
<asp:Parameter Name="Amount" Type="Int32" />
<asp:Parameter Name="Price" Type="String" />
<asp:Parameter Name="TotalPrice" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="PhoneName" Type="String" />
<asp:Parameter Name="Amount" Type="Int32" />
<asp:Parameter Name="Price" Type="String" />
<asp:Parameter Name="TotalPrice" Type="String" />
<asp:Parameter Name="original_Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>

Incorrect syntax near 'nvarchar'. Must declare the scalar variable "#RegID"

I am trying to update/edit the user details from the update query but I am getting the error Must declare a scalar Variable #RegID. I tried playing around with the query but it still comes up with the same error.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:rm3558nConnectionString2 %>" DeleteCommand="DELETE FROM [Registration] WHERE [RegID] = #original_RegID" InsertCommand="INSERT INTO [Registration] ([First Name], [Last Name], [Email ID], [Password], [Confirm Password], [DOB], [Gender]) VALUES (#First_Name, #Last_Name, #Email_ID, #Password, #Confirm_Password, #DOB, #Gender)" SelectCommand="SELECT * FROM [Registration] WHERE ([Email ID] = #Email_ID)" UpdateCommand="UPDATE [Registration] SET [First Name] = #First_Name, [Last Name] = #Last_Name, [Email ID] = #Email_ID, [Password] = #Password, [Confirm Password] = #Confirm_Password, [DOB] = #DOB, [Gender] = #Gender WHERE [RegID] = #original_RegID" OldValuesParameterFormatString="original_{0}">
<DeleteParameters>
<asp:Parameter Name="original_RegID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="First_Name" Type="String" />
<asp:Parameter Name="Last_Name" Type="String" />
<asp:Parameter Name="Email_ID" Type="String" />
<asp:Parameter Name="Password" Type="String" />
<asp:Parameter Name="Confirm_Password" Type="String" />
<asp:Parameter DbType="Date" Name="DOB" />
<asp:Parameter Name="Gender" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="Namelbl" Name="Email_ID" PropertyName="Text" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="First_Name" Type="String" />
<asp:Parameter Name="Last_Name" Type="String" />
<asp:Parameter Name="Email_ID" Type="String" />
<asp:Parameter Name="Password" Type="String" />
<asp:Parameter Name="Confirm_Password" Type="String" />
<asp:Parameter DbType="Date" Name="DOB" />
<asp:Parameter Name="Gender" Type="String" />
<asp:Parameter Name="original_RegID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
GRID VIEW
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="RegID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Horizontal">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="RegID" HeaderText="RegID" InsertVisible="False" ReadOnly="True" SortExpression="RegID" />
<asp:BoundField DataField="First Name" HeaderText="First Name" SortExpression="First Name" />
<asp:BoundField DataField="Last Name" HeaderText="Last Name" SortExpression="Last Name" />
<asp:BoundField DataField="Email ID" HeaderText="Email ID" SortExpression="Email ID" />
<asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />
<asp:BoundField DataField="Confirm Password" HeaderText="Confirm Password" SortExpression="Confirm Password" />
<asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
<asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
The problem occurs when SelectCommand output inside SqlDataSource using field names with whitespaces (marked with SELECT * ... which selects all columns without using alias), while BoundField inside GridView control doesn't accept field/column names using whitespaces.
Therefore, you need to use SELECT statement in SelectCommand mentioning all field names used for DataField attribute in GridView, and only alias all columns that have whitespaces on them like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ...
SelectCommand="SELECT [RegID], [First Name] AS First_Name, [Last Name] AS Last_Name, [Email ID] AS Email_ID, [Password], [Confirm Password] AS Confirm_Password, DOB, Gender FROM [Registration] WHERE ([Email ID] = #Email_ID)"
...>
...
</asp:SqlDataSource>
Then in your GridView, declare all DataField field names with underscores or just remove whitespaces following convention in SelectCommand inside SqlDataSource, and use them with SortExpression attribute:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="RegID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Horizontal">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="RegID" HeaderText="RegID" InsertVisible="False" ReadOnly="True" SortExpression="RegID" />
<asp:BoundField DataField="First_Name" HeaderText="First Name" SortExpression="First_Name" />
<asp:BoundField DataField="Last_Name" HeaderText="Last Name" SortExpression="Last_Name" />
<asp:BoundField DataField="Email_ID" HeaderText="Email ID" SortExpression="Email_ID" />
<asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />
<asp:BoundField DataField="Confirm_Password" HeaderText="Confirm Password" SortExpression="Confirm_Password" />
<asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
<asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
</Columns>
...
</asp:GridView>
With these setup above, the binding on BoundField should be working properly without changing field names inside DB.
NB: ... marks removed sections on code sample for brevity.
Similar issues:
Incorrect syntax near 'nvarchar' must declare scalar variable near #num
Incorrect syntax near 'nvarchar'
SqlDataSource/DataField Bug in 2.0 (the conclusion made from this post)

Customize gridview edit and prevent Update / Cancel from showing

One of the fields in my database could have hundreds or even thousands of words. I want to know how to make the edit button in gridview work as an action event but also turn the rest of its automatic functions off. What I mean is I don't want the the actual cells editable in the table and I don't want the update and cancel button to become visible.
Here is the gridview:
<asp:Panel ID="updatePanel" runat="server" Visible="false">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource4"
AllowPaging="True" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
BorderWidth="1px" CellPadding="3" CellSpacing="2" OnRowDeleting="GridView1_OnRowDeleting" OnRowEditing="GridView1_OnRowEditing"
DataKeyNames="PK_DailyTaskHours, PK_NonScrumStory">
<Columns>
<asp:BoundField DataField="Catagory" HeaderText="Support Catagory" SortExpression="Catagory" />
<asp:BoundField DataField="AppName" HeaderText="Application Name" SortExpression="IncidentNumber" />
<asp:BoundField DataField="IncidentNumber" HeaderText="Incident #" SortExpression="IncidentNumber" />
<asp:BoundField DataField="Hours" HeaderText="Hours" SortExpression="Hours" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="CreatedDate" HeaderText="Created Date" SortExpression="CreatedDate" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [NonScrumStory].[IncidentNumber], [NonScrumStory].[Description], [NonScrumStory].[CreatedDate],
[DailyTaskHours].[Hours], [Application].[AppName], [SupportCatagory].[Catagory], [DailyTaskHours].[PK_DailyTaskHours],
[NonScrumStory].[PK_NonScrumStory] FROM [NonScrumStory], [DailyTaskHours], [Application], [SupportCatagory] WHERE
([NonScrumStory].[UserId] = #userIdSelected) AND ([NonScrumStory].[PK_NonScrumStory] = [DailyTaskHours].[NonScrumStoryId])
AND ([NonScrumStory].[CatagoryId] = [SupportCatagory].[PK_SupportCatagory]) AND
([NonScrumStory].[ApplicationId] = [Application].[PK_Application]) AND ([NonScrumStory].[Deleted] != 1)"
DeleteCommand="UPDATE [NonScrumStory] SET [Deleted] = 1 WHERE (PK_NonScrumStory = #nonScrumStoryPK);
UPDATE [DailyTaskHours] SET [Deleted] = 1 WHERE (PK_DailyTaskHours = #dailyTaskHoursPK); ">
<SelectParameters>
<asp:QueryStringParameter Name="userIdSelected" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:QueryStringParameter Name="dailyTaskHoursPK" Type="String" />
<asp:QueryStringParameter Name="nonScrumStoryPK" Type="String" />
</DeleteParameters>
</asp:SqlDataSource>
Additionally, I have the action event working:
protected virtual void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
{
}
And I know using ReadOnly="false" prevents the cells from becoming editable. How do I get rid of the update / cancel? Is it possible to execute cancel immediately so the user doesn't see the change?
When the user pressed edit, the panel showing the gridview will become invisible and a new panel will appear with edit options.

C# - Must declare the scalar variable "#ms_id" - Error

I'm writing an web-app that keeps track of deadlines. With this app you have to be able to update records that are being saved in an SQL DB.
However I'm having some problem with my update in my aspx-file.
<asp:GridView ID="gv_editMilestones" runat="server" DataSourceID="sql_ds_milestones"
CellPadding="4" ForeColor="#333333" GridLines="None" Font-Size="Small"
AutoGenerateColumns="False" DataKeyNames="id" Visible="false"
onrowupdated="gv_editMilestones_RowUpdated"
onrowupdating="gv_editMilestones_RowUpdating"
onrowediting="gv_editMilestones_RowEditing">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id"
ReadOnly="True" Visible="false"/>
<asp:BoundField DataField="ms_id" HeaderText="ms_id"
SortExpression="ms_id" ReadOnly="True"/>
<asp:BoundField DataField="ms_description" HeaderText="ms_description"
SortExpression="ms_description"/>
<%-- <asp:BoundField DataField="ms_resp_team" HeaderText="ms_resp_team"
SortExpression="ms_resp_team"/>--%>
<asp:TemplateField HeaderText="ms_resp_team" SortExpression="ms_resp_team">
<ItemTemplate>
<%# Eval("ms_resp_team") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DDL_ms_resp_team" runat="server"
DataSourceID="sql_ds_ms_resp_team" DataTextField="team_name"
DataValueField="id">
<%--SelectedValue='<%# Bind("ms_resp_team") %>'--%>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ms_focal_point" HeaderText="ms_focal_point"
SortExpression="ms_focal_point" />
<asp:BoundField DataField="ms_exp_date" HeaderText="ms_exp_date"
SortExpression="ms_exp_date" DataFormatString="{0:d}"/>
<asp:BoundField DataField="ms_deal" HeaderText="ms_deal"
SortExpression="ms_deal" ReadOnly="True"/>
<asp:CheckBoxField DataField="ms_active" HeaderText="ms_active"
SortExpression="ms_active"/>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#999999" />
</asp:GridView>
<asp:SqlDataSource ID="sql_ds_milestones" runat="server"
ConnectionString="<%$ ConnectionStrings:testServer %>"
SelectCommand="SELECT [id]
,[ms_id]
,[ms_description]
,(SELECT [team_name] FROM [NSBP].[dbo].[tbl_teams] as teams
WHERE milestones.[ms_resp_team] = teams.[id]) as 'ms_resp_team'
,[ms_focal_point]
,[ms_exp_date]
,(SELECT [deal] FROM [NSBP].[dbo].[tbl_deals] as deals
WHERE milestones.[ms_deal] = deals.[id]) as 'ms_deal'
,[ms_active]
FROM [NSBP].[dbo].[tbl_milestones] as milestones"
UpdateCommand="UPDATE [NSBP].[dbo].[tbl_milestones]
SET [ms_description] = #ms_description
,[ms_focal_point] = #ms_focal_point
,[ms_active] = #ms_active
WHERE [ms_id] = #ms_id">
<UpdateParameters>
<asp:Parameter Name="ms_description" Type="String" />
<%-- <asp:Parameter Name="ms_resp_team" Type="String" />--%>
<asp:Parameter Name="ms_focal_point" Type="String" />
<asp:Parameter Name="ms_exp_date" Type="DateTime" />
<asp:Parameter Name="ms_active" Type="Boolean" />
<%-- <asp:Parameter Name="ms_id" Type="String" />--%>
</UpdateParameters>
</asp:SqlDataSource>
You can see my complete GridView-structure + my datasource bound to this GridView.
There is nothing written in my onrowupdating-function in my code-behind file.
Thx in advance
You are using #ms_id in the where clause of your SQL Statement, but the line that sets that <asp:Parameter ... /> is commented out. Try uncommenting:
<%-- <asp:Parameter Name="ms_id" Type="String" /> --%>
and try again
This line;
<%-- <asp:Parameter Name="ms_id" Type="String" />--%>
appears to be commented out and is used in the SQL where clause.

Filling an Gridview EmptyDataTemplate with data ASP.NET C#

I have a gridview where the data it shows depends on the textbox and button control. Since the gridview doesnt show anything (unless a user typed an input in the textbox) how can I fill it with all data from the tables? I'm thinking of inserting another gridview inside of the EmptyDataTemplate but is there any way I could show all the records even without user input?
Just started in ASP.NET so I really need your help guys.
Thanks in advance ;)
Here's my code sample:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="lenid" DataSourceID="returningDataSource" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField HeaderStyle-Width="120px" ButtonType="Button" ShowEditButton="True" ShowDeleteButton="True" />
<asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN"
SortExpression="bookid" />
<asp:BoundField DataField="booktitle" HeaderText="Title"
SortExpression="booktitle" />
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID"
SortExpression="EmployeeID" />
<asp:BoundField DataField="department" HeaderText="Department"
SortExpression="department" />
<asp:BoundField DataField="dateborrowed" HeaderText="Date borrowed"
SortExpression="dateborrowed" />
<asp:BoundField DataField="datereturned" HeaderText="Date returned"
SortExpression="datereturned" NullDisplayText="-- not yet returned --" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<EmptyDataTemplate>
<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="lenid" DataSourceID="returningDataSource" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField HeaderStyle-Width="120" ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN"
SortExpression="bookid" />
<asp:BoundField DataField="booktitle" HeaderText="Title"
SortExpression="booktitle" />
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID"
SortExpression="EmployeeID" />
<asp:BoundField DataField="department" HeaderText="Department"
SortExpression="department" />
<asp:BoundField DataField="dateborrowed" HeaderText="Date borrowed"
SortExpression="dateborrowed" />
<asp:BoundField DataField="datereturned" HeaderText="Date returned"
SortExpression="datereturned" NullDisplayText="-- not yet returned --" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="returningDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
DeleteCommand="DELETE FROM [LendTable] WHERE [lenid] = #lenid"
InsertCommand="INSERT INTO [LendTable] ([bookid], [EmployeeID], [department], [dateborrowed], [datereturned]) VALUES (#bookid, #EmployeeID, #department, #dateborrowed, #datereturned)"
SelectCommand="SELECT dbo.LendTable.lenid, dbo.LendTable.bookid, dbo.LendTable.EmployeeID, dbo.LendTable.department, dbo.LendTable.dateborrowed, dbo.LendTable.datereturned, dbo.TblBooks.booktitle FROM dbo.LendTable INNER JOIN dbo.TblBooks ON dbo.LendTable.bookid = dbo.TblBooks.bookid"
UpdateCommand="UPDATE [LendTable] SET [bookid] = #bookid, [EmployeeID] = #EmployeeID, [department] = #department, [dateborrowed] = #dateborrowed, [datereturned] = #datereturned WHERE [lenid] = #lenid">
<DeleteParameters>
<asp:Parameter Name="lenid" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="EmployeeID" Type="String" />
<asp:Parameter Name="department" Type="String" />
<asp:Parameter Name="dateborrowed" Type="DateTime" />
<asp:Parameter Name="datereturned" Type="DateTime" />
<asp:Parameter Name="lenid" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="EmployeeID" Type="String" />
<asp:Parameter Name="department" Type="String" />
<asp:Parameter Name="dateborrowed" Type="DateTime" />
<asp:Parameter Name="datereturned" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>
</EmptyDataTemplate>
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
I want to display all the records from a table if a user doesn't type any input (or fill the EmptyDataTemplate with records from the database).
Thanks again!
Execute the Select command of the returning data source as its gonna return the entire rows od the table and bind to the original grid in the (!IsPostBack) of the page_load and next time when you need to bind data based on text box input execute the query and bind the data to same grid.

Categories

Resources