grid view won't update database (no errors) - c#

I have a page with a details view and a grid view (each with their own data source. I can't figure out why the update says it's successful but don't update the table. the details view works fine. The grid view is only suppose to show data related to what's in the details view. If I hard code the values in the source it works but when I change it back it blows up. here's my code
<asp:SqlDataSource OnUpdated="dsCar_Updated" ID="dsCar" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT Car.* FROM Car WHERE (VIN = ?)"
UpdateCommand="UPDATE Car SET [Branch ID] = ?, [State Registration] = ?, [License Plate] = ?, Color = ?, Model = ?, [Car Year] = ?, [Plate Expiration] = ? WHERE (VIN = ?)">
<SelectParameters>
<asp:QueryStringParameter Name="?" QueryStringField="VIN" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataSourceID="dsCar" Height="50px" Width="265px" CellPadding="4"
ForeColor="#333333" GridLines="None" DataKeyNames="VIN" DefaultMode="Edit">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="VIN" HeaderText="VIN" ReadOnly="True"
SortExpression="VIN" />
<asp:TemplateField HeaderText="Branch Name" SortExpression="Branch ID">
<EditItemTemplate>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Branch]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Branch Name"
DataValueField="Branch ID" SelectedValue='<%# Bind("[Branch ID]") %>'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Branch]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource3" DataTextField="Branch Name"
DataValueField="Branch ID" SelectedValue='<%# Bind("[Branch ID]") %>'>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Branch]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Branch Name"
DataValueField="Branch ID" Enabled="False" SelectedValue='<%# Bind("[Branch ID]") %>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="State Registration" HeaderText="State Registration"
SortExpression="State Registration" />
<asp:BoundField DataField="License Plate" HeaderText="License Plate"
SortExpression="License Plate" />
<asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" />
<asp:TemplateField HeaderText="Model" SortExpression="Model">
<EditItemTemplate>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Model]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource4" DataTextField="Model Name"
DataValueField="ModelID" SelectedValue='<%# Bind("Model") %>'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Model") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Model]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource4" DataTextField="Model Name"
DataValueField="ModelID" SelectedValue='<%# Bind("Model") %>'
Enabled="False">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Car Year" HeaderText="Car Year"
SortExpression="Car Year" />
<asp:TemplateField HeaderText="Plate Expiration"
SortExpression="Plate Expiration">
<EditItemTemplate>
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Car]"></asp:SqlDataSource>
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"
VisibleDate='<%# Eval("[Plate Expiration]") %>'
SelectedDate='<%# Bind("[Plate Expiration]") %>'></asp:Calendar>
<asp:TextBox ID="txtDateEdit" runat="server"
Text='<%# Bind("[Plate Expiration]") %>'></asp:TextBox>
<br />
</EditItemTemplate>
<InsertItemTemplate>
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"
VisibleDate='<%# Eval("[Plate Expiration]") %>'></asp:Calendar>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("[Plate Expiration]") %>' Visible="false"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<br />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[Plate Expiration]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource OnUpdated="dsMaintance_Updated" ID="dsMaintance" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
DeleteCommand="DELETE FROM [Maintenance] WHERE [MainteanceID] = ?"
InsertCommand="INSERT INTO [Maintenance] ([MainteanceID], [VIN], [Procedure ID], [Date]) VALUES (?, ?, ?, ?)"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT MainteanceID, VIN, [Procedure ID], [Date] FROM Maintenance WHERE (VIN = ?)"
UpdateCommand="UPDATE Maintenance SET [Procedure ID] = ?, [Date] = ? WHERE (MainteanceID = ?)">
<DeleteParameters>
<asp:Parameter Name="MainteanceID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="MainteanceID" Type="Int32" />
<asp:Parameter Name="VIN" Type="String" />
<asp:Parameter Name="Procedure_ID" Type="Int32" />
<asp:Parameter Name="Date" Type="DateTime" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="VIN" QueryStringField="VIN" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="MainteanceID" Type="Int32" />
<asp:Parameter Name="VIN" Type="String" />
<asp:Parameter Name="Procedure_ID" Type="Int32" />
<asp:Parameter Name="Date" Type="DateTime" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="MainteanceID" DataSourceID="dsMaintance" BackColor="White"
BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
CellSpacing="1" GridLines="None">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="MainteanceID" HeaderText="MainteanceID"
InsertVisible="False" ReadOnly="True" SortExpression="MainteanceID" />
<asp:BoundField DataField="VIN" HeaderText="VIN" SortExpression="VIN" />
<asp:BoundField DataField="Procedure ID" HeaderText="Procedure ID"
SortExpression="Procedure ID" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
</Columns>
</asp:GridView>

You should pass the proper parameters to the UpdateParameters section. It looks like you have a copy-paste problem (maybe you copied the section from the InsertParameters section). Your UPDATE statement needs only 3 parameters.

<asp:SqlDataSource OnUpdated="dsMaintance_Updated" ID="dsMaintance" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
DeleteCommand="DELETE FROM [Maintenance] WHERE [MainteanceID] = ?"
InsertCommand="INSERT INTO [Maintenance] ([MainteanceID], [VIN], [Procedure ID], [Date]) VALUES (?, ?, ?, ?)"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT MainteanceID, VIN, [Procedure ID], [Date] FROM Maintenance WHERE (VIN = ?)"
UpdateCommand="UPDATE Maintenance SET [Procedure ID] = ?, [Date] = ? WHERE (MainteanceID = ?)">
<DeleteParameters>
<asp:Parameter Name="MainteanceID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="MainteanceID" Type="Int32" />
<asp:Parameter Name="VIN" Type="String" />
<asp:Parameter Name="Procedure_ID" Type="Int32" />
<asp:Parameter Name="Date" Type="DateTime" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="VIN" QueryStringField="VIN" Type="String" />
</SelectParameters>
<UpdateParameters>
<%--<asp:Parameter Name="MainteanceID" Type="Int32" />--%>
<%--<asp:Parameter Name="VIN" Type="String" />--%>
<asp:Parameter Name="Procedure_ID" Type="Int32" />
<asp:Parameter Name="Date" Type="DateTime" />
</UpdateParameters>
</asp:SqlDataSource>

Related

asp.net c# DetailsView DropDownList fails to Update

I've created a DetailsView that displays data based on a selected item from a GridView. When the DetailsView is under Edit Mode, it displays dropdownlists that contain data from a SQL databind. The data displays in the dropdownlist without issue, but fails to update. All other fields succeed.
The data is bound to the drop down via OnDataBound.
The idea is to populate the SelectedValue with the current data, and populate what it can be changed to. This works without issue. It simply fails to update.
I believe it has to do with syncing the control with the "an" parameter, but am at a loss at how to do so.
Front End Code pertaining to the issue:
<asp:DetailsView ID="userDetails" runat="server"
Height="50px"
Width="400px"
AutoGenerateRows="False"
CellPadding="4"
DataKeyNames="id"
DataSourceID="detailsSqlDataSource"
ForeColor="#333333"
GridLines="None"
OnDataBound="userDetails_ItemEdit">
<AlternatingRowStyle BackColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<EditRowStyle BackColor="#2461BF" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" width="125px"/>
<Fields>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
<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="user_name" HeaderText="User Name" SortExpression="user_name" />
<asp:TemplateField HeaderText="T" SortExpression="t">
<EditItemTemplate>
<asp:DropDownList ID="tEditDD" runat="server" SelectedValue='<%# Bind("t") %>'>
<asp:ListItem Value="t1" Text="t1"></asp:ListItem>
<asp:ListItem Value="t2" Text="t2"></asp:ListItem>
<asp:ListItem Value="t3" Text="t3"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("t") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("t") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="P" SortExpression="p">
<EditItemTemplate>
<asp:DropDownList ID="pEditDD" runat="server" SelectedValue='<%# Bind("p") %>'>
<asp:ListItem Value="As" Text="As"></asp:ListItem>
<asp:ListItem Value="An" Text="An"></asp:ListItem>
<asp:ListItem Value="Su" Text="Su"></asp:ListItem>
<asp:ListItem Value="Ad" Text="Ad"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("p") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("p") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="An" SortExpression="an">
<EditItemTemplate>
<asp:DropDownList ID="anEditDD" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("an") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("an") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Su" SortExpression="su">
<EditItemTemplate>
<asp:DropDownList ID="suEditDD" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("su") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("su") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="email" HeaderText="E-Mail" SortExpression="email" />
<asp:TemplateField HeaderText="AL" SortExpression="al">
<EditItemTemplate>
<asp:DropDownList ID="alEditDD" runat="server" SelectedValue='<%# Bind("al") %>'>
<asp:ListItem Value="As" Text="As"></asp:ListItem>
<asp:ListItem Value="An" Text="An"></asp:ListItem>
<asp:ListItem Value="Su" Text="Su"></asp:ListItem>
<asp:ListItem Value="Ad" Text="Ad"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="alInsertDD" runat="server" SelectedValue='<%# Bind("al") %>'>
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="As" Text="As"></asp:ListItem>
<asp:ListItem Value="An" Text="An"></asp:ListItem>
<asp:ListItem Value="Su" Text="Su"></asp:ListItem>
<asp:ListItem Value="Ad" Text="Ad"></asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("al") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
</asp:DetailsView>
<asp:SqlDataSource ID="detailsSqlDataSource" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:CPConnectionString %>"
UpdateCommand="UPDATE [users] SET [first_name] = #first_name, [last_name] = #last_name, [user_name] = #user_name, [t] = #t, [p] = #p, [an] = #an, [su] = #su, [email] = #email, [al] = #al WHERE [id] = #original_id">
<SelectParameters>
<asp:ControlParameter ControlID="usersGrid" Name="id" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="first_name" Type="String" />
<asp:Parameter Name="last_name" Type="String" />
<asp:Parameter Name="user_name" Type="String" />
<asp:Parameter Name="t" Type="String" />
<asp:Parameter Name="p" Type="String" />
<asp:Parameter Name="an" Type="String" />
<asp:Parameter Name="su" Type="String" />
<asp:Parameter Name="email" Type="String" />
<asp:Parameter Name="al" Type="String" />
<asp:Parameter Name="original_id" Type="Int32" />
<asp:Parameter Name="original_first_name" Type="String" />
<asp:Parameter Name="original_last_name" Type="String" />
<asp:Parameter Name="original_user_name" Type="String" />
<asp:Parameter Name="original_t" Type="String" />
<asp:Parameter Name="original_p" Type="String" />
<asp:Parameter Name="original_an" Type="String" />
<asp:Parameter Name="original_su" Type="String" />
<asp:Parameter Name="original_email" Type="String" />
<asp:Parameter Name="original_al" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
Code Behind:
protected void userDetails_ItemEdit(object sender, EventArgs e)
{
if (userDetails.CurrentMode == DetailsViewMode.Edit)
{
AnDD();
}
}
protected void AnDD()
{
DropDownList anEditDD = userDetails.FindControl("anEditDD") as DropDownList;
string userName = ((TextBox)userDetails.Rows[3].Cells[1].Controls[0]).Text;
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["CPConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(conStr);
con.Open();
string currentAnSqlSelect = "SELECT an FROM users WHERE user_name='" + userName + "'";
SqlDataReader currentReader;
SqlCommand ccmd = new SqlCommand(currentAnSqlSelect, con);
currentReader = ccmd.ExecuteReader();
while (currentReader.Read())
{
ListItem currentList = new ListItem();
currentList.Text = currentReader["an"].ToString();
errorLBL.Text = currentReader["an"].ToString();
anEditDD.Items.Add(currentList);
}
currentReader.Close();
string anSqlSelect = "SELECT first_name, last_name FROM users WHERE position='An'";
SqlDataReader anReader;
SqlCommand cmd = new SqlCommand(anSqlSelect, con);
anReader = cmd.ExecuteReader();
while (anReader.Read())
{
ListItem anList = new ListItem();
anList.Text = anReader["first_name"].ToString() + " " + anReader["last_name"].ToString();
anEditDD.Items.Add(anList);
}
anReader.Close();
con.Close();
anEditDD.Items.Add("N/A");
anEditDD.DataBind();
anEditDD.SelectedIndex = 0;
}
Your update command appears to be part of the details view, and not part of any ObjectDataSource. Can you confirm your markup above is correct?
Edit
In your UpdateParameters group, you aren't getting the values from the controls. Try doing the following:
<UpdateParameters>
<asp:Parameter />
...snip
<asp:ControlParameter Name="an" ControlID="idOfWhateverControlHasTheUserSetValue" Type="String" />
...snip
</UpdateParameters>
I took your advice and did a little research about using the ControlParameter.
The fix was adding the following to the Update Parameters, and removing the auto-populated parameters for 'an'. Thanks for the help, Nick.
<asp:ControlParameter Name="an" ControlID="userDetails$anEditDD" PropertyName="SelectedValue" Type="String" />

DetailsView DropDownList not updating

I am new to ASP.net so I'm just now getting into using TemplateFields. I have a DetailsView which contains a DropDownList that has it's items populated from my codebehind when the DetailsView is in EditMode.
This much is working fine. However, after selecting an item from my DropDownList, and clicking "Update" the selection is not updated, however the other DetailsView form values are.
Here is my code:
DetailsView & SQL Queries:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="315px"
AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="SqlDataSource1"
OnDataBound="DetailsView1_DataBound">
<Fields>
<asp:CommandField ShowEditButton="True" ControlStyle-CssClass="button" >
<ControlStyle CssClass="button"></ControlStyle>
</asp:CommandField>
<asp:BoundField DataField="FirstName" HeaderText="First Name"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="Last Name"
SortExpression="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="JobID" HeaderText="JobID" SortExpression="JobID" />
<asp:BoundField DataField="SupervisorID" HeaderText="Supervisor"
SortExpression="SupervisorID" />
<asp:TemplateField HeaderText="Hire Date" SortExpression="HireDate">
<EditItemTemplate>
<span class="input-type-text margin-right relative"><asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HireDate","{0:d}") %>' CssClass="datepicker"></asp:TextBox><img src="images/icons/fugue/calendar-month.png" width="16" height="16"></span>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HireDate","{0:d}") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("HireDate","{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<EditItemTemplate>
<asp:DropDownList ID="StatusList" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dbConnectionString %>"
DeleteCommand="DELETE FROM [Users] WHERE [ID] = #ID"
InsertCommand="INSERT INTO [Users] ([ClientID], [UserName], [Password], [FirstName], [LastName], [Email], [JobID], [SupervisorID], [HireDate], [Status]) VALUES (#ClientID, #UserName, #Password, #FirstName, #LastName, #Email, #JobID, #SupervisorID, #HireDate, #Status)"
ProviderName="<%$ ConnectionStrings:dbConnectionString.ProviderName %>"
SelectCommand="SELECT [ID], [ClientID], [UserName], [Password], [FirstName], [LastName], [Email], [JobID], [SupervisorID], [HireDate], [Status] FROM [Users] WHERE [ID] = #ID"
UpdateCommand="UPDATE [Users] SET [ClientID] = #ClientID, [UserName] = #UserName, [Password] = #Password, [FirstName] = #FirstName, [LastName] = #LastName, [Email] = #Email, [JobID] = #JobID, [SupervisorID] = #SupervisorID, [HireDate] = #HireDate, [Status] = #StatusList WHERE [ID] = #ID">
<SelectParameters>
<asp:QueryStringParameter Name="ID" QueryStringField="curEmp" Type="Int64" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ClientID" Type="Int64" />
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="Password" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="JobID" Type="Double" />
<asp:Parameter Name="SupervisorID" Type="Double" />
<asp:Parameter Name="HireDate" Type="DateTime" />
<asp:Parameter Name="StatusList" Type="String" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
Code Behind:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
DropDownList statusList = DetailsView1.FindControl("StatusList") as DropDownList;
//Add list items (later to be added by database)
if (statusList != null)
{
statusList.Items.Add(new ListItem("Select Status", ""));
statusList.Items.Add(new ListItem("Active", "Active"));
statusList.Items.Add(new ListItem("Inactive", "Inactive"));
statusList.Items.Add(new ListItem("Terminated", "Terminated"));
statusList.DataBind();
}
//Highlight selected item in dropdownlist while in edit mode
DataRowView row = (DataRowView)DetailsView1.DataItem;
ListItem liItem = statusList.Items.FindByText(row["Status"].ToString());
if (liItem != null)
{
statusList.Items.FindByText(row["Status"].ToString()).Selected = true;
}
}
}
I'm not sure where to go from here. I've tried to change the Update command in my SQLDataSource for the DropDownList (named StatusList) but haven't had any luck. Any idea what I'm missing here?
Thanks for any help!
I don't think the statusList is bound to your datasource. You would either need to bind the dropdown in the aspx or in the datasource.
So, like this in the aspx...
<asp:DropDownList ID="statusList" runat="server" SelectedValue='<%# Bind("datafield") %>'>
or in the data source parameters...
<asp:ControlParameter Name="status" ControlID="DetailsView1$statusDropDownList" PropertyName="SelectedValue" Type="String" />

RadGrid(Gridview) Recursive Checkbox in EditItemTemplate Column for Inserts and Updates

I have a RadGrid with a Checkbox inside an EditItemTemplate for an Active or Inactive status.
This checkbox only shows when the item is being updated or added as new. I have a ControlParameter for the checkbox, but since it is recursive with many of these checkboxes in the list, it throws the good old "Could not find control 'cbActive' in ControlParameter" Error.
I dont have any real CS code other than the Radgrid binding on page load.
ASPX Code:
<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Vista" Width="500px"
GridLines="None" AllowFilteringByColumn="False" AllowSorting="True" OnExportCellFormatting="RadGrid1_ExportCellFormatting"
AllowAutomaticInserts="True" AllowAutomaticDeletes="true" AllowAutomaticUpdates="True" AutoGenerateEditColumn="True" AutoGenerateDeleteColumn="true">
<MasterTableView AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1" ItemStyle-HorizontalAlign="Left" CommandItemDisplay="TopAndBottom">
<Columns>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" SortExpression="ID"
UniqueName="ID" Visible="False" ReadOnly="true">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="Active" SortExpression="Active" UniqueName="Active" ItemStyle-Width="100" Visible="false">
<EditItemTemplate>
<asp:CheckBox ID="cbActive" runat="server" Checked='<%# GenerateBindString(Container.DataItem) %>' />
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LBCust %>"
SelectCommand="SELECT * FROM [LBX_Portal_AccountNumbers] WHERE ([Site] = #Site) AND (Active=#Active OR #Active = '0') ORDER BY AccountNumber"
DeleteCommand="DELETE FROM [LBX_Portal_AccountNumbers] WHERE [ID] = #ID"
InsertCommand="INSERT INTO [LBX_Portal_AccountNumbers] ([AccountNumber], [Site], [Active]) VALUES (#AccountNumber, #Site, #Active)"
UpdateCommand="UPDATE [LBX_Portal_AccountNumbers] SET [AccountNumber] = #AccountNumber, [Active] = #Active WHERE [ID] = #ID">
<SelectParameters>
<asp:ControlParameter ControlID="dd_Status" Name="Active"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="AccountNumber" Type="String" />
<asp:Parameter Name="Site" Type="String" />
<asp:ControlParameter ControlID="cbActive"
PropertyName="Checked" Type="Boolean" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="AccountNumber" Type="String" />
<asp:ControlParameter ControlID="cbActive"
PropertyName="Checked" Type="Boolean" />
</UpdateParameters>
</asp:SqlDataSource>
You aren't going to be able to set the value through a control parameter; what you can do though, is use an <asp:Parameter> instead, and set the DefaultValue property on the parameter via:
sqlDataSource1.SelectParameters[0].DefaultValue = "X"
Or, with the Grids, I know with the GridView it has a collection of values that it used to perform the update with; you could add an entry representing the checkbox control, which would get pushed into the data source. I know know if the RadGrid has the same option.
So it took a lot of looking around but i couldnt do this without tapping into the commands via the CS. Here is my new Code for it to work:
<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Vista" Width="500px"
GridLines="None" AllowFilteringByColumn="False" AllowSorting="True" OnExportCellFormatting="RadGrid1_ExportCellFormatting"
AllowAutomaticInserts="True" AllowAutomaticDeletes="true" AllowAutomaticUpdates="True" AutoGenerateEditColumn="True" AutoGenerateDeleteColumn="true" OnItemCommand="RadGrid1_ItemCommand">
<MasterTableView AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1" ItemStyle-HorizontalAlign="Left" CommandItemDisplay="TopAndBottom">
<Columns>
<telerik:GridTemplateColumn HeaderText="ID" SortExpression="ID" UniqueName="ID" Visible="false" ReadOnly="true">
<ItemTemplate>
<asp:Label ID="lblIDView" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="ID" SortExpression="IDEdit" UniqueName="IDEdit" Visible="false">
<ItemTemplate />
<EditItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="AccountNumber" HeaderText="Sapphire Account Number"
SortExpression="AccountNumber" UniqueName="AccountNumberView" ReadOnly="true" ItemStyle-Width="400">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="Account Number" SortExpression="AccountNumber" UniqueName="AccountNumber" Visible="false">
<ItemTemplate />
<EditItemTemplate>
<asp:Textbox ID="txtAccountNumber" runat="server" Text='<%# Bind("AccountNumber") %>' />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Active" SortExpression="Active" UniqueName="Active" ItemStyle-Width="100" Visible="false">
<ItemTemplate />
<EditItemTemplate>
<asp:CheckBox ID="cbActive" runat="server" Checked='<%# GenerateBindString(Container.DataItem) %>' />
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Status" SortExpression="Active" UniqueName="Active" ItemStyle-Width="100">
<ItemTemplate>
<asp:Label ID="lblActive" runat="server" Text='<%# Convert.ToBoolean(GenerateBindString(Container.DataItem)) == true ? "Active" : "Inactive" %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LBCust %>"
SelectCommand="SELECT * FROM [LBX_Portal_AccountNumbers] WHERE ([Site] = #Site) AND (Active=#Active OR #Active = '0') ORDER BY AccountNumber"
DeleteCommand="DELETE FROM [LBX_Portal_AccountNumbers] WHERE [ID] = #ID"
InsertCommand="INSERT INTO [LBX_Portal_AccountNumbers] ([AccountNumber], [Site], [Active]) VALUES (#AccountNumber, #Site, #Active)"
UpdateCommand="UPDATE [LBX_Portal_AccountNumbers] SET [AccountNumber] = #AccountNumber, [Active] = #Active WHERE [ID] = #ID">
<SelectParameters>
<asp:ControlParameter ControlID="dd_Status" Name="Active"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="AccountNumber" Type="String" />
<asp:Parameter Name="Site" Type="String" />
<asp:Parameter Name="Active" Type="Boolean" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="AccountNumber" Type="String" />
<asp:Parameter Name="Active" Type="Boolean" />
</UpdateParameters>
</asp:SqlDataSource>
CS CODE:
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == "PerformInsert")
{
GridEditFormItem gridEditFormItem = (GridEditFormItem)e.Item;
Label lblID = (Label)gridEditFormItem.FindControl("lblID");
TextBox txtAccountNumber = (TextBox)gridEditFormItem.FindControl("txtAccountNumber");
CheckBox cbActive = (CheckBox)gridEditFormItem.FindControl("cbActive");
bool isActive = false;
if (cbActive.Checked)
isActive = true;
else
isActive = false;
string SqlStr = "INSERT INTO [LBX_Portal_AccountNumbers] ([AccountNumber], [Site], [Active])";
SqlStr += " VALUES ('" + txtAccountNumber.Text + "'" + ", '" + SiteName + "'" + ", '" + isActive + "')";
SqlDataSource1.InsertCommand = SqlStr;
SqlDataSource1.Insert();
}
if (e.CommandName == "Update")
{
GridEditFormItem gridEditFormItem = (GridEditFormItem)e.Item;
Label lblID = (Label)gridEditFormItem.FindControl("lblID");
TextBox txtAccountNumber = (TextBox)gridEditFormItem.FindControl("txtAccountNumber");
CheckBox cbActive = (CheckBox)gridEditFormItem.FindControl("cbActive");
bool isActive = false;
if (cbActive.Checked)
isActive = true;
else
isActive = false;
string SqlStr = "UPDATE [LBX_Portal_AccountNumbers] SET [AccountNumber] = '" + txtAccountNumber.Text;
SqlStr += "', [Active] = '" + isActive + "' WHERE [ID] = " + lblID.Text;
SqlDataSource1.UpdateCommand = SqlStr;
SqlDataSource1.Update();
}
}

Set current date in formview

I want to put the current date in my formview 'dateadded' but its not showing when I load it in the browser. I'm using code behind but how can I display it with date and time?
Here's a my code behind.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LibrarySystem.AdminPage
{
public partial class ManageBooks : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(FormView1.CurrentMode == FormViewMode.Insert)
{
TextBox dateadded = FormView1.FindControl("dateaddedTextBox") as TextBox;
dateadded.Text = DateTime.Now.ToString("d");
}
}
}
}
Here's my complete design
<%# Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Lending.aspx.cs" Inherits="LibrarySystem.Test" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<h3>
Lending of Books</h3>
<p>
<asp:FormView ID="FormView1" runat="server" DataSourceID="lendDataSource" DefaultMode="Insert" OnDataBound="FormView1_DataBound">
<EditItemTemplate>
Book ID/ISBN:
<asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>' />
<br />
Book Title:
<asp:TextBox ID="booktitleTextBox" runat="server"
Text='<%# Bind("booktitle") %>' />
<br />
Employee ID:
<asp:TextBox ID="employeeidTextBox" runat="server"
Text='<%# Bind("employeeid") %>' />
<br />
Department:
<asp:TextBox ID="departmentTextBox" runat="server"
Text='<%# Bind("department") %>' />
<br />
Date borrowed:
<asp:TextBox ID="dateborrowedTextBox" runat="server"
Text='<%# Bind("dateborrowed") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
Book:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="booktitleDataSource" DataTextField="booktitle"
DataValueField="bookid" SelectedValue='<%# Bind("bookid", "{0}") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="booktitleDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [bookid], [booktitle] FROM [TblBooks]">
</asp:SqlDataSource>
<br />
Employee ID:
<asp:TextBox ID="employeeidTextBox" runat="server"
Text='<%# Bind("employeeid") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="* Required" ControlToValidate="employeeidTextBox" ValidationGroup="lendbook">
</asp:RequiredFieldValidator>
<br />
Department:
<asp:TextBox ID="departmentTextBox" runat="server"
Text='<%# Bind("department") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="* Required" ControlToValidate="departmentTextBox" ValidationGroup="lendbook">
</asp:RequiredFieldValidator>
<br />
Date borrowed:
<asp:TextBox ID="dateborrowedTextBox" runat="server"
Text='<%# Bind("dateborrowed") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="* Required" ControlToValidate="dateborrowedTextBox" ValidationGroup="lendbook">
</asp:RequiredFieldValidator>
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" ValidationGroup="lendbook"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
<EmptyDataTemplate>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</EmptyDataTemplate>
</asp:FormView>
</p>
<p>
<asp:SqlDataSource ID="lendDataSource" 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.bookid, dbo.TblBooks.booktitle, dbo.LendTable.employeeid, dbo.LendTable.department, dbo.LendTable.dateborrowed 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>
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="lenid" DataSourceID="lendgridviewDataSource" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN"
SortExpression="bookid" />
<asp:BoundField DataField="booktitle" HeaderText="Book 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"
NullDisplayText="-- not yet returned --" SortExpression="datereturned" />
</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="lendgridviewDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
DeleteCommand="DELETE FROM [LendTable] WHERE [lenid] = #lenid"
InsertCommand="INSERT INTO [LendTable] ([bookid], [department], [EmployeeID], [dateborrowed], [datereturned]) VALUES (#bookid, #department, #EmployeeID, #dateborrowed, #datereturned)"
SelectCommand="SELECT dbo.LendTable.lenid, dbo.LendTable.bookid, dbo.LendTable.department, dbo.LendTable.EmployeeID, 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, [department] = #department, [EmployeeID] = #EmployeeID, [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="department" Type="String" />
<asp:Parameter Name="EmployeeID" 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="department" Type="String" />
<asp:Parameter Name="EmployeeID" Type="String" />
<asp:Parameter Name="dateborrowed" Type="DateTime" />
<asp:Parameter Name="datereturned" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>
</p>
<p>
<asp:HyperLink ID="HyperLink4" runat="server"
NavigateUrl="~/Admin/Returning.aspx">Returning</asp:HyperLink>
</p>
<asp:HyperLink ID="HyperLink5" runat="server"
NavigateUrl="~/Admin/AdminPage.aspx">Back to Admin Page</asp:HyperLink>
<p>
</p>
</asp:Content>
You have to use the Databound event for that...
protected void FormView1_DataBound(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Insert)
{
TextBox dateadded = FormView1.FindControl("dateaddedTextBox") as TextBox;
dateadded.Text = DateTime.Now.ToString("d");
}
}
Edit: You need to pass your Date Column in the inserting method of the DetailsView
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
e.Values["DateColumnName"] = ((TextBox)DetailsView1.FindControl("dateaddedTextBox")).Text;
}
Instead of doing that in your Page_Load() method, try doing it in the Page_PreRender() method.

deleting row from gridview

I have 2 tables MachineGroups and Machines. MachineGroups has columns:
MachinegroupID
MachineGroupName
MachineGroupDesc
And Machines has columns:
MachineGroupID (FK)
MachineID
MachineName
Machinedesc
Now I want to delete a machinegroup but not the ones that have machines in it.
DELETE FROM MachineGroups
WHERE MachineGroupID NOT IN (SELECT DISTINCT MachineGroupID FROM Machines)
This will delete all the rows in the gridview which have no. of machines= 0
What i want is to delete only the row whose delete link is clicked...
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="1" CellSpacing="2"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"
Width="100%" ondatabound="GridView1_DataBound1"
onrowdatabound="GridView1_RowDataBound1">
<RowStyle BackColor="#D0D8E8" ForeColor="#333333" Height="35px" />
<Columns>
<asp:BoundField DataField="MachineGroupID" HeaderText="MachineGroupID"
InsertVisible="False" ReadOnly="True" SortExpression="MachineGroupID"
Visible="False" />
<asp:BoundField DataField="MachineGroupName" HeaderText="MachineGroupName"
SortExpression="MachineGroupName" />
<asp:BoundField DataField="MachineGroupDesc" HeaderText="MachineGroupDesc"
SortExpression="MachineGroupDesc" />
<asp:BoundField DataField="TimeAdded" HeaderText="TimeAdded"
SortExpression="TimeAdded" />
<asp:TemplateField HeaderText="CanBeDeleted" SortExpression="CanBeDeleted"
Visible="False">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Bind("CanBeDeleted") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Bind("CanBeDeleted") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="No. of PCs" HeaderText="No. of PCs" ReadOnly="True"
SortExpression="No. of PCs" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SumooHAgentDBConnectionString %>"
SelectCommand="SELECT MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted, COUNT(Machines.MachineName) AS 'No. of PCs' FROM MachineGroups FULL OUTER JOIN Machines ON Machines.MachineGroupID = MachineGroups.MachineGroupID GROUP BY MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted"
DeleteCommand="DELETE FROM MachineGroups
WHERE MachineGroupID NOT IN (SELECT DISTINCT MachineGroupID FROM Machines)">
<DeleteParameters>
<asp:Parameter Name="original_MachineGroupID" />
<asp:Parameter Name="original_MachineGroupName" />
<asp:Parameter Name="original_MachineGroupDesc" />
<asp:Parameter Name="original_CanBeDeleted" />
<asp:Parameter Name="original_TimeAdded" />
</DeleteParameters>
</asp:SqlDataSource>
Not tested, but the direction should be right:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SumooHAgentDBConnectionString %>"
SelectCommand="SELECT MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted, COUNT(Machines.MachineName) AS 'No. of PCs' FROM MachineGroups FULL OUTER JOIN Machines ON Machines.MachineGroupID = MachineGroups.MachineGroupID GROUP BY MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted"
DeleteCommand="DELETE FROM MachineGroups WHERE MachineGroupID = #MachineGroupID">
<DeleteParameters>
<asp:Parameter Name="MachineGroupID" />
</DeleteParameters>
</asp:SqlDataSource>

Categories

Resources