Gridview not updating all fields - asp.net

I have a gridview that is filled correctly from 3 tables.
When I update it, it will only update one field (Kommentar) but only the first time.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID,Erledigt,Kommentar"
DataSourceID="ArtikelDataSource">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"
ReadOnly="True" >
<ControlStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Beschreibung" HeaderText="Beschreibung"
SortExpression="Beschreibung" ReadOnly="True" >
<ControlStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="Datum" HeaderText="Datum" SortExpression="Datum"
ReadOnly="True" >
<ControlStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="Erledigt" HeaderText="Erledigt"
SortExpression="Erledigt" >
<ControlStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Kommentar" HeaderText="Kommentar"
SortExpression="Kommentar" >
<ControlStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="LastUpdate" HeaderText="LastUpdate"
SortExpression="LastUpdate" ReadOnly="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" Visible="False" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="ArtikelDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT ci.Name, ci.Beschreibung, ca.ChkItemID, ca.ChkUserID, ca.Datum, ca.Erledigt, ca.Kommentar, ca.LastUpdate, ca.ID FROM ChecklistActions AS ca INNER JOIN ChecklistItems AS ci ON ca.ChkItemID = ci.ID INNER JOIN ChecklistUsers AS cu ON ca.ChkUserID = cu.ID WHERE (ca.Datum = #datum) AND (cu.Login = #userlogin)"
UpdateCommand="UPDATE ChecklistActions SET Kommentar = #Kommentar, LastUpdate = GETDATE() WHERE (ID = #ID)"
OnSelecting="_data_Selecting"
>
<SelectParameters>
<asp:Parameter Name="userlogin"/>
<asp:Parameter Name="datum"/>
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Kommentar" Type="string"/>
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
I have first an empty field "Kommentar", then it is filled, but when I try to change the value again, only the GETDATE() changes value, the content of Kommentar is the same as before.
When I try to change the value of "Erledigt" via SQL
UpdateCommand="UPDATE ChecklistActions SET Erledigt = #Erledigt, Kommentar = #Kommentar, LastUpdate = GETDATE() WHERE (ID = #ID)"
...
<asp:Parameter Name="Erledigt" Type="Int32"/>
nothing (except the Getdate()) is updated at all - not even the field Kommentar at the fist time
It seems that the gridview is sending the old values to the update command (only the first update will receive the new Kommentar-value) which is really irritating ?

you missed , in your UpdateCommand
UPDATE ChecklistActions SET Erledigt = #Erledigt, Kommentar = #Kommentar, LastUpdate = GETDATE() WHERE (ID = #ID)"
edit
Set only the primary key column names on your database tables as DataKeyNames, in this case you only update ChecklistActions table. so set primary key of ChecklistActions as DataKeyNames

Related

update query not changed

I put gridview and I added hyperlink to take the user to other page and changed the status id based on a query but the problem that the update query is not worked even no error:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" style="margin:auto" >
<Columns>
<asp:TemplateField HeaderText="user enter">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# String.Format("~/entry.aspx?request_id={0}",DataBinder.Eval (Container,"DataItem.request_id"))%>' >
enter </asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name" ReadOnly="True" SortExpression="name" />
<asp:BoundField DataField="visitor_mobile" HeaderText="visitor_mobile" SortExpression="visitor_mobile" />
<asp:BoundField DataField="request_id" HeaderText="request_id" InsertVisible="False" ReadOnly="True" SortExpression="request_id" />
<asp:BoundField DataField="request_date" HeaderText="request_date" SortExpression="request_date" />
<asp:BoundField DataField="visit_date" HeaderText="visit_date" SortExpression="visit_date" />
<asp:BoundField DataField="reason" HeaderText="reason" SortExpression="reason" />
<asp:BoundField DataField="status_name" HeaderText="status_name" SortExpression="status_name" />
</Columns>
</asp:GridView>
and this is the code when user click enter it query to update the status_id:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:visitormanagementConnectionString %>" SelectCommand="update dbo.requests set status_id=5
where request_id= #request_id;" UpdateCommand="update dbo.requests set status_id=5 where request_id= #request_id">
<SelectParameters>
<asp:QueryStringParameter Name="request_id" QueryStringField="request_id" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="request_id" />
</UpdateParameters>
</asp:SqlDataSource>

Delete Query says it has no parameter

I have a Gridview control that I'm trying to allow the users to use the delete button to delete the record from the database. When I click the delete button, it tells me that there is no value for the parameter. Here is the relevant markup:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/webvideos.mdb"
SelectCommand="SELECT Org_Sec_Atty.ID, ORGANIZATIONS.ORG_NAME, ORG_SECTIONS.SectionName, ATTORNEYS.NAME, ATTORNEYS.LASTNAME, ATTORNEYS.EMAIL, ATTORNEYS.TEL
FROM (ORGANIZATIONS INNER JOIN (Org_Sec_Atty INNER JOIN ATTORNEYS ON Org_Sec_Atty.Atty_ID = ATTORNEYS.ATTY_ID) ON ORGANIZATIONS.ID = Org_Sec_Atty.OrgID) INNER JOIN ORG_SECTIONS ON Org_Sec_Atty.SecID = ORG_SECTIONS.ID;"
DeleteCommand="DELETE FROM Org_Sec_Atty WHERE ID = ?">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
</asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="20"
AllowSorting="True" DataSourceID="AccessDataSource1" >
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="ORG_NAME" HeaderText="Organization" SortExpression="ORG_NAME" />
<asp:BoundField DataField="SectionName" HeaderText="Section" SortExpression="SectionName" />
<asp:BoundField DataField="NAME" HeaderText="First Name" SortExpression="NAME" />
<asp:BoundField DataField="LASTNAME" HeaderText="Last Name" SortExpression="LASTNAME" />
<asp:BoundField DataField="EMAIL" HeaderText="E-mail" SortExpression="EMAIL" />
<asp:BoundField DataField="TEL" HeaderText="Phone" SortExpression="TEL" />
</Columns>
</asp:GridView>
I initially forgot to include the Org_Sec_Atty.ID in my SELECT statement and added it in there later when I clicked the delete button and nothing happened. I tried clearing my browser cache with no luck. Any other ideas?
I think that you also need to set the DataKeyNames property in the gridview:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="20"
AllowSorting="True" DataSourceID="AccessDataSource1" DataKeyNames="ID" >
This should sort you out.

Dropdownlist in templatefield - update fails (null value insertion)

I have a gridview. In this i have a templatefield with a DropDownList (DDL in EditItemTemplate mode, label in ItemTemplate mode).
When i hit edit on of of the detailsview's rows i can select any value from the DDL (the DDL is populated from a sqldatasource), but if i try to execute the update, it fails, because it thinks i didn't supply the data...
Here is the exact error (the DB refuse NULL data):
Cannot insert the value NULL into column 'status', table
'gyumolcs.dbo.orders'; column does not allow nulls. UPDATE fails. The
statement has been terminated.
Here is the code for the gridview:
<!-- language: c# -->
<asp:SqlDataSource ID="orderadminSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>"
SelectCommand="SELECT orders.ID, aspnet_Users.UserName, orders.quantity AS Quantity, product.name AS Product, status.name AS Status, orders.date AS Date FROM orders INNER JOIN product ON orders.ordertype = product.ID INNER JOIN status ON orders.status = status.ID INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId ORDER BY date"
DeleteCommand="DELETE FROM orders WHERE (ID = #ID)"
UpdateCommand="UPDATE orders SET status = #status WHERE (ID = #ID)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="status" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="orderadminSqlDataSource" DataKeyNames="ID" Width="608px"
AllowPaging="True" AllowSorting="True" PageSize="15">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" InsertVisible="False" ReadOnly="True" >
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="Felhasználónév"
SortExpression="UserName" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Quantity" HeaderText="Mennyiség (kg)"
SortExpression="Quantity" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Product" HeaderText="Termék"
SortExpression="Product" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Rendelés státusz" SortExpression="status">
<EditItemTemplate>
<!--<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>-->
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="statustypeDDLSqlDataSource" DataTextField="name"
DataValueField="ID">
</asp:DropDownList>
<asp:SqlDataSource ID="statustypeDDLSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>"
SelectCommand="SELECT [ID], [name] FROM [status]">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="status" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="Date" HeaderText="Dátum" SortExpression="Date" />
</Columns>
<PagerSettings PageButtonCount="15" Mode="NumericFirstLast" />
</asp:GridView>
And here is the orderadminSqlDataSource's code (the gridview's datasource)
<!-- language: c# -->
<asp:SqlDataSource ID="orderadminSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>"
SelectCommand="SELECT orders.ID, aspnet_Users.UserName, orders.quantity AS Quantity, product.name AS Product, status.name AS Status, orders.date AS Date FROM orders INNER JOIN product ON orders.ordertype = product.ID INNER JOIN status ON orders.status = status.ID INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId ORDER BY date"
DeleteCommand="DELETE FROM orders WHERE (ID = #ID)"
UpdateCommand="UPDATE orders SET status = #status WHERE (ID = #ID)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="status" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
Please help me, i can't figure out the problem.
Thanks in advance!
I think you're forgetting to set the DropDown's SelectedValue property:
<asp:DropDownList ID="DropDownList1"
runat="server"
DataSourceID="statustypeDDLSqlDataSource"
DataTextField="name"
DataValueField="status"
SelectedValue='<%# Bind("status") %>'
AppendDataBoundItems="True" >
Change your code for statustypeDDLSqlDataSource:
<asp:SqlDataSource ID="statustypeDDLSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>"
SelectCommand="SELECT [ID] as status, [name] FROM [status]">
</asp:SqlDataSource>
You can also simplify this ( no parenthesis necessary ):
UPDATE orders SET [status] = #status WHERE [ID] = #ID
Note: I had to rename the ID column in the query so that it conforms with the status parameter you defined here:
<UpdateParameters>
<asp:Parameter Name="status" />
<asp:Parameter Name="ID" />
</UpdateParameters>
My last edit and attempt here :D
After carefully looking at your GridView's SELECT statement I see where the error lies:
SELECT orders.ID,
aspnet_Users.UserName,
orders.quantity AS Quantity,
product.name AS Product,
status.name AS Status,
orders.date AS Date
FROM orders
INNER JOIN product ON
orders.ordertype = product.ID
INNER JOIN status ON orders.status = status.ID
INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId
ORDER BY date
Change it to:
SELECT orders.ID,
aspnet_Users.UserName,
orders.quantity AS Quantity,
product.name AS Product,
status.ID AS status,
status.name AS StatusName,
orders.date AS Date
FROM orders
INNER JOIN product ON
orders.ordertype = product.ID
INNER JOIN status ON orders.status = status.ID
INNER JOIN aspnet_Users ON orders.userid = aspnet_Users.UserId
ORDER BY date
Now you must change this part too:
<ItemTemplate>
<asp:Label ID="status" runat="server" Text='<%# Bind("StatusName") %>'></asp:Label>
</ItemTemplate>
By the way... here's a nice step by step tutorial on this matter:
Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web Server Control

DetailsView not working with selectable GridView

My DetailsView is always blank. I've tested both Data Sources in QueryBuilder and they return values. I suspect something is wrong with the parameter value assignments in PoweredEquipDetails where they are set to the SelectedValue in GridView1, but I don't know why it wouldn't work.
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="PoweredEquipHeader" DataKeyNames="Model,Serial">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Model" HeaderText="Model" ReadOnly="True"
SortExpression="Model" />
<asp:BoundField DataField="Serial" HeaderText="Serial" ReadOnly="True"
SortExpression="Serial" />
</Columns>
<SelectedRowStyle BackColor="#FF6600" />
</asp:GridView>
<asp:SqlDataSource ID="PoweredEquipHeader" runat="server"
ConnectionString="<%$ ConnectionStrings:CPECConnectionString %>"
SelectCommand="Select t1.Model, t1.Serial From Powered_Equip t1 Where TechID = #TechID And Store = #Store">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="TECH01" Name="TechID"
QueryStringField="Tech" />
<asp:QueryStringParameter DefaultValue="0000" Name="Store"
QueryStringField="Store" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="PoweredEquipDetail" runat="server"
ConnectionString="<%$ ConnectionStrings:CPECConnectionString %>"
SelectCommand="SELECT t1.Serial, t1.Model, t1.Status, t1.[Trailer VIN], t2.Tag, t2.State, t2.ExpMonth, t2.ExpYear FROM Powered_Equip AS t1 INNER JOIN Trailers AS t2 ON t1.[Trailer VIN] = t2.VIN WHERE (t1.Serial = #Serial) AND (t1.Model = #Model)">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Serial"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="GridView1" Name="Model"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server"
DataSourceID="PoweredEquipDetail" DataKeyNames="Serial,Model">
<Fields>
<asp:BoundField DataField="Serial" HeaderText="Serial" ReadOnly="True"
SortExpression="Serial" />
<asp:BoundField DataField="Model" HeaderText="Model"
ReadOnly="True" SortExpression="Model" />
<asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True"
SortExpression="Status" />
<asp:BoundField DataField="Trailer VIN" HeaderText="Trailer VIN" ReadOnly="True"
SortExpression="Trailer VIN" />
<asp:BoundField DataField="Tag" HeaderText="Tag" ReadOnly="True"
SortExpression="Tag" />
<asp:BoundField DataField="State" HeaderText="State" ReadOnly="True"
SortExpression="State" />
<asp:BoundField DataField="ExpMonth" HeaderText="ExpMonth" ReadOnly="True"
SortExpression="ExpMonth" />
<asp:BoundField DataField="ExpYear" HeaderText="ExpYear" ReadOnly="True"
SortExpression="ExpYear" />
</Fields>
</asp:DetailsView>
</form>
EDIT: So I believe what is happening is the GridView's SelectedValue in the DetailsView parameter assignments is always Model, instead of both Model and Serial as specified in my DataValueKeys. In the event that an equipment item has a model and serial that are the same value (i.e. Model 118 Serial 118), the DetailsView populates. But in any case where the Model and Serial are different (which will be almost every other case), the DetailsView is blank. So how do I get two different SelectedValues to assign value to two different ControlParameters?
Changed
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Serial"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="GridView1" Name="Model"
PropertyName="SelectedValue" />
</SelectParameters>
to
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Serial"
PropertyName="SelectedDataKey.Values[Serial]" />
<asp:ControlParameter ControlID="GridView1" Name="Model"
PropertyName="SelectedValue" />
</SelectParameters>
int n = int.Parse(GridView1.SelectedValue.ToString());
TextBox4.Text = n.ToString();
con = new SqlConnection(#"Data Source=LAB-III-136;Initial Catalog=student;Integrated Security=True");
con.Open();
da = new SqlDataAdapter("select * from student1 where id='" + TextBox4.Text + " '", con);
ds = new DataSet();
da.Fill(ds, "temp");
DetailsView2.DataSource = ds;
DetailsView2.DataBind();

Radgrid edit/update not working

I'm having a wired problem with Radgrid.. Im using a Radgrid to display some results where the user can edit/update/delete them. I'm using a single SQL Server 2000 table to fetch the results... Not sure why the events are not firing in radgrid. But I am able to do it successfully using the Gridview control...
Im using .NET framework 4
Radgrid code
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteColumn="True"
AutoGenerateEditColumn="True" DataSourceID="SSDS" GridLines="None" Width="844px"
DataMember="DefaultView">
<MasterTableView DataSourceID="SSDS" DataKeyNames="id">
<Columns>
<telerik:GridBoundColumn DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id"
UniqueName="id" Visible="false">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="name" HeaderText="name" SortExpression="name"
UniqueName="name">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="address" HeaderText="address" SortExpression="address"
UniqueName="address">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<br />
<br />
<asp:SqlDataSource ID="SSDS" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringSQL %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringSQL.ProviderName %>" SelectCommand="SELECT [id], [name], [address] FROM [sitelinks]"
UpdateCommand="UPDATE [sitelinks] set [name] = ? , [address] = ? where [id] = ?"
DeleteCommand="delete from sitelinks where id = ?">
<DeleteParameters>
<asp:Parameter Name="id" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="name" />
<asp:Parameter Name="address" />
<asp:Parameter Name="id" />
</UpdateParameters>
</asp:SqlDataSource>
Gridview control code (the one that works)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SSDS"
DataKeyNames="id">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" Visible="false" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SSDS" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringSQL %>"
DeleteCommand="delete from sitelinks where id = ?" ProviderName="<%$ ConnectionStrings:ConnectionStringSQL.ProviderName %>"
SelectCommand="select id,name,address from sitelinks" UpdateCommand="update sitelinks set name=?, address=? where id = ? ">
<UpdateParameters>
<asp:ControlParameter ControlID="GridView1" Name="name" />
<asp:ControlParameter ControlID="GridView1" Name="address" />
<asp:ControlParameter ControlID="GridView1" Name="id" />
</UpdateParameters>
<DeleteParameters>
<asp:ControlParameter ControlID="GridView1" Name="id" />
</DeleteParameters>
</asp:SqlDataSource>
Thanks
Set the AllowAutomaticUpdates/AllowAutomaticDeletes properties of the telerik grid to true and see this sample.

Resources