Cannot call update function on gridview - c#

I have aproblem everytime I call my update function,
I belive that the source of the problem is because this function gets object paramter,
and also get a paramter from diffrant souce, and i am not doing it currectly.
This is the error:
http://prntscr.com/2z0cd6
GridView code:
<asp:GridView ID="gvAnimals" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="ObjectDataSourceAnimals" ForeColor="#333333" GridLines="None" DataKeyNames="animalId">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="animalId" HeaderText="animalId" SortExpression="animalId" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:CheckBoxField DataField="vertebrates" HeaderText="vertebrates" SortExpression="vertebrates" />
<asp:CheckBoxField DataField="vegetarian" HeaderText="vegetarian" SortExpression="vegetarian" />
<asp:CheckBoxField DataField="terrestrial" HeaderText="terrestrial" SortExpression="terrestrial" />
<asp:BoundField DataField="kind" HeaderText="kind" SortExpression="kind" />
<asp:BoundField DataField="avgWeight" HeaderText="avgWeight" SortExpression="avgWeight" />
<asp:BoundField DataField="avgHeight" HeaderText="avgHeight" SortExpression="avgHeight" />
<asp:BoundField DataField="infoAdress" HeaderText="infoAdress" SortExpression="infoAdress" />
<asp:BoundField DataField="imageAdress" HeaderText="imageAdress" SortExpression="imageAdress" />
</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>
<asp:ObjectDataSource ID="ObjectDataSourceAnimals" runat="server" DeleteMethod="DeleteAnimal" OldValuesParameterFormatString="original_{0}" SelectMethod="GetAllAnimals" TypeName="BLProject.Animal" UpdateMethod="UpdateAnimal" DataObjectTypeName="BLProject.Animal">
<DeleteParameters>
<asp:Parameter Name="a" Type="Object" />
<asp:SessionParameter Name="newsAdress" SessionField="Adress" Type="String" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter ControlID="textSearch" Name="Search" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="dropdownlistAnimalToSearch" Name="type" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="a" Type="Object" />
<asp:SessionParameter DefaultValue="D:\\project\\Ilan Project 29-11\\Web\\NewsInfo" Name="newsAdress" SessionField="Adress" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
*I belive that the source of the error is in the bold lines
The Update function:
[DataObjectMethod(DataObjectMethodType.Update)]
public static int UpdateAnimal(Animal a, string newsAdress)
Animal a is the object paramter which the grid view get by using the select function,
but the adress is another paramter from session source which the function gets too.
However, since I added this paramter I have this new error.
Thanks.

You can do it like this:
Change your method with this one
public void UpdateAnimal(int animalId, string name, bool vertebrates, bool vegetarian, string newsAdress)
{
}
and your ObjectDataSource
<asp:ObjectDataSource ID="ObjectDataSourceAnimals" runat="server" DeleteMethod="DeleteAnimal" SelectMethod="GetAllAnimals" TypeName="BLProject.Animal" UpdateMethod="UpdateAnimal">
<DeleteParameters>
<asp:Parameter Name="a" Type="Object" />
<asp:SessionParameter Name="newsAdress" SessionField="Adress" Type="String" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter ControlID="textSearch" Name="Search" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="dropdownlistAnimalToSearch" Name="type" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="animalId" Type="Int32"/>
.......the other fields from Animal that you want to update
<asp:Parameter Name="name" Type="String"/>
<asp:Parameter Name="vegetarian" Type="Boolean"/>
<asp:Parameter Name="vertebrates" Type="Boolean"/>
<asp:SessionParameter DefaultValue="D:\\project\\Ilan Project 29-11\\Web\\NewsInfo" Name="newsAdress" SessionField="Adress" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
Seems that when using DataObjectTypeName you cannot add parameters. The solution is to send some or all the properties of the Animal row edited as parameters plus the other parameter.
If you want to use OldValuesParameterFormatString="original_{0}", add this int original_animalid to the method input parameters.

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)

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.

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.

Categories

Resources