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

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.

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>

Is there a way to automatically execute the 'Select' command on a GridView, when the Grid loads?

I've got a GridView who's DataSource SELECTs based on the SelectedValue of another GridView.
On the GridView with the SelectedValue (DataKeyName), I need to execute the Select command when the grid loads, rather that using a "Select" button.
I've tried using the SelectedIndex attribute of the GridView but this wont do it for me.
I've also looked at various methods and events but I cant seem to find a good way of doing this.
In the code below, there is a ButtonField with a "Select" button but I would like the Select command to take place as or after the grid is populated.
Thanks in advance.
<asp:GridView ID="ClientDetailsGridView" runat="Server" AutoGenerateColumns="False" DataKeyNames="AccountNumber" DataSourceID="ZeNetAccounts" CellPadding="2" ForeColor="#333333" GridLines="None" Width="749px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Select" HeaderText="More Information" Text="View" />
<asp:BoundField DataField="AccountNumber" HeaderText="Account #" SortExpression="AccountNumber" />
<asp:BoundField DataField="AccountName" HeaderText="Client Name" SortExpression="AccountName" />
<asp:BoundField DataField="Address1" HeaderText="Address" SortExpression="Address1" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
<asp:BoundField DataField="PostalCode" HeaderText="Zip" SortExpression="PostalCode" />
<asp:BoundField DataField="PrimaryPhoneNumberFormatted" HeaderText="Phone" SortExpression="PrimaryPhoneNumberFormatted" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Transparent" Font-Bold="True" ForeColor="White" Height="25px" CssClass="GridHeaderBasic" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SelectedRowStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedAscendingCellStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedAscendingHeaderStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedDescendingCellStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedDescendingHeaderStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
</asp:GridView>
<br />
<br />
<asp:GridView ID="CustomFieldsGridView" runat="server" AutoGenerateColumns="False" SelectedIndex="0" DataKeyNames="DocumentNumber,DocumentType,CustomFieldDefinitionKeyID" DataSourceID="ZeNetCustomFields">
<Columns>
<asp:BoundField DataField="DocumentNumber" HeaderText="DocumentNumber" ReadOnly="True" SortExpression="DocumentNumber" />
<asp:BoundField DataField="DocumentType" HeaderText="DocumentType" ReadOnly="True" SortExpression="DocumentType" />
<asp:BoundField DataField="CustomFieldDefinitionKeyID" HeaderText="CustomFieldDefinitionKeyID" ReadOnly="True" SortExpression="CustomFieldDefinitionKeyID" />
<asp:BoundField DataField="CustomFieldValue" HeaderText="CustomFieldValue" SortExpression="CustomFieldValue" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="ZeNetAccounts" runat="Server" ConnectionString="<%$ ConnectionStrings:Ze-Net_Technologies %>"
SelectCommand="SELECT * FROM [tblAccounts] WHERE ([AccountName] = #AccountName)">
<SelectParameters>
<asp:ControlParameter ControlID="ClientSearch" Name="AccountName" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="ZeNetCustomFields" runat="Server" ConnectionString="<%$ ConnectionStrings:Ze-Net_Technologies %>"
SelectCommand="SELECT * FROM [tblCustomFieldValues] WHERE ([DocumentNumber] = #DocumentNumber)">
<SelectParameters>
<asp:ControlParameter ControlID="ClientDetailsGridView" Name="DocumentNumber" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

C# 'Must declare the scalar variable "#ID_ZAPISA"' error

I am having problem with the following code. Delete function does not work (throws scalar variable error). Update function does not work ... it does not throw any error, but it simply does not make changes to the database.
If I make exact DetailsView, everything works OK. Where is the problem?
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ServisnaKnjiga.aspx.cs" Inherits="CernaticJurij_Default2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<p>
Registrska številka:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Iskanje" />
</p>
<p>
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"
AutoGenerateRows="False" CellPadding="4" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None" Height="50px" Width="915px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="REG_STEVILKA" HeaderText="REGISTRSKA ŠTEVILKA"
SortExpression="REG_STEVILKA" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="VIN_STEVILKA" HeaderText="VIN ŠTEVILKA"
SortExpression="VIN_STEVILKA" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="ZNAMKA" HeaderText="ZNAMKA"
SortExpression="ZNAMKA" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="MODEL" HeaderText="MODEL" SortExpression="MODEL"
InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="LETO_IZDELAVE" HeaderText="LETO IZDELAVE"
SortExpression="LETO_IZDELAVE" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="ID_ZAPISA" HeaderText="ID_ZAPISA"
InsertVisible="False" ReadOnly="True" SortExpression="ID_ZAPISA" />
<asp:BoundField DataField="DATUM" HeaderText="DATUM"
SortExpression="DATUM" />
<asp:BoundField DataField="KILOMETRINA" HeaderText="KILOMETRINA"
SortExpression="KILOMETRINA" />
<asp:BoundField DataField="OPIS" HeaderText="OPIS" SortExpression="OPIS" />
<asp:BoundField DataField="ODG_OSEBA" HeaderText="ODGOVORNA OSEBA"
SortExpression="ODG_OSEBA" />
<asp:BoundField DataField="CENA" HeaderText="CENA" SortExpression="CENA" />
<asp:BoundField DataField="REG_STEVILKA1" HeaderText="REG_STEVILKA1"
SortExpression="REG_STEVILKA1" Visible="False" />
<asp:BoundField DataField="ID_STRANKE" HeaderText="ID_STRANKE"
SortExpression="ID_STRANKE" Visible="False" />
<asp:BoundField DataField="PROSTORNINA_MOTORJA" HeaderText="PROSTORNINA_MOTORJA"
SortExpression="PROSTORNINA_MOTORJA" Visible="False" />
<asp:BoundField DataField="MOC_MOTORJA"
HeaderText="MOC_MOTORJA" SortExpression="MOC_MOTORJA"
Visible="False" />
<asp:BoundField DataField="TIP_MOTORJA" HeaderText="TIP_MOTORJA"
SortExpression="TIP_MOTORJA" Visible="False" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
<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" />
</asp:DetailsView>
</p>
<p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Stranke %>"
DeleteCommand="DELETE FROM [SERVISNI_ZAPIS] WHERE [ID_ZAPISA] = #ID_ZAPISA"
InsertCommand="INSERT INTO [SERVISNI_ZAPIS] ([REG_STEVILKA], [DATUM], [KILOMETRINA], [OPIS], [ODG_OSEBA], [CENA]) VALUES (#REG_STEVILKA, #DATUM, #KILOMETRINA, #OPIS, #ODG_OSEBA, #CENA)" SelectCommand="SELECT *
FROM SERVISNI_ZAPIS
FULL JOIN VOZILO
ON (SERVISNI_ZAPIS.REG_STEVILKA=VOZILO.REG_STEVILKA)
WHERE (SERVISNI_ZAPIS.REG_STEVILKA = #REG_STEVILKA)"
UpdateCommand="UPDATE [SERVISNI_ZAPIS] SET [REG_STEVILKA] = #REG_STEVILKA, [DATUM] = #DATUM, [KILOMETRINA] = #KILOMETRINA, [OPIS] = #OPIS, [ODG_OSEBA] = #ODG_OSEBA, [CENA] = #CENA WHERE [ID_ZAPISA] = #ID_ZAPISA">
<DeleteParameters>
<asp:Parameter Name="ID_ZAPISA" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:ControlParameter ControlID="TextBox1" Name="REG_STEVILKA"
PropertyName="Text" />
<asp:Parameter DbType="Date" Name="DATUM" />
<asp:Parameter Name="KILOMETRINA" Type="String" />
<asp:Parameter Name="OPIS" Type="String" />
<asp:Parameter Name="ODG_OSEBA" Type="String" />
<asp:Parameter Name="CENA" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="REG_STEVILKA"
PropertyName="Text" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="REG_STEVILKA" Type="String" />
<asp:Parameter DbType="Date" Name="DATUM" />
<asp:Parameter Name="KILOMETRINA" Type="String" />
<asp:Parameter Name="OPIS" Type="String" />
<asp:Parameter Name="ODG_OSEBA" Type="String" />
<asp:Parameter Name="CENA" Type="String" />
<asp:Parameter Name="ID_ZAPISA" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</p>
<p>
</p>
<p>
</p>
</asp:Content>
Do you mean it doesn't work with a gridview but does work with a DetailsView? If so, then you need to set DataKeyNames for the gridview.
You can look at this for further details: http://fabdata.wordpress.com/2007/03/23/must-declare-the-scalar-variable-id/
I solved it by adding:
<asp:DetailsView ID="DetailsView1" runat="server" DataKeyNames="ID"....

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.

grid view update

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".

Categories

Resources