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();
}
}
Related
I have a SqlDataSource connected to a GridView and I am trying to get the edit working correctly, but with everything I try I still get that parameters are not supplied.
I have tried naming the parameters with and without the '#', it seems to make no difference. As you can see in the below image, the update parameters exist and even have values!
Example image:
ASPX markup:
<asp:GridView runat="server" ID="JobGV" DataSourceID="JobApprovalsDS"
AutoGenerateColumns="False" AutoGenerateEditButton="true"
OnRowCommand="JobGV_OnRowCommand" OnRowUpdating="JobGV_OnRowUpdating">
<Columns>
<asp:BoundField HeaderText="Function" DataField="FunDesc" ReadOnly="true"/>
<asp:BoundField HeaderText="Employee" DataField="EmpName" ReadOnly="true"/>
<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlEmps" SelectedValue='<%# Eval("appEmpID")%>' DataSourceID="EmpDS" DataTextField="EmpName" DataValueField="EmpID" />
<asp:Label runat="server" ID="data" Text='<%# Eval("appBusinessUnit") +";" + Eval("appFunctID") %>' Visible="False"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("EmpName") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="JobApprovalsDS" runat="server"
ConnectionString="<%$ ConnectionStrings:JobClose %>"
SelectCommand="up_JobApprovalsSelect"
SelectCommandType="StoredProcedure"
UpdateCommand="up_JobApprovalsUpdate">
<SelectParameters>
<asp:Parameter Name="ShowAll" DefaultValue="1" />
<asp:Parameter Name="AllPhases" DefaultValue="1" />
<asp:ControlParameter Name="BusinessUnit" ControlID="ddlEditJob" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="#BusinessUnit" Type="String"/>
<asp:Parameter Name="#FunctID" Type="Int32"/>
<asp:Parameter Name="#EmpID" Type="Int32"/>
<asp:Parameter Name="#UpdateDate" Type="DateTime"/>
</UpdateParameters>
</asp:SqlDataSource>
C#:
protected void JobGV_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Update"))
{
int empid = 0;
string[] data;
int index = int.Parse(e.CommandArgument.ToString());
GridViewRow row = JobGV.Rows[index];
DropDownList ddlemp = (DropDownList) row.FindControl("ddlEmps");
empid = int.Parse(ddlemp.SelectedValue);
Label lbl = (Label) row.FindControl("data");
data = lbl.Text.Split(';');
JobApprovalsDS.UpdateParameters["#BusinessUnit"].DefaultValue = data[0];
JobApprovalsDS.UpdateParameters["#FunctID"].DefaultValue = data[1];
JobApprovalsDS.UpdateParameters["#EmpID"].DefaultValue = empid.ToString();
//JobApprovalsDS.UpdateParameters.Add("BusinessUnit", data[0]);
//JobApprovalsDS.UpdateParameters.Add("FunctID", data[1]);
//JobApprovalsDS.UpdateParameters.Add("EmpID", empid.ToString());
}
}
protected void JobGV_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
JobApprovalsDS.Update();
}
Are you talking about the update process??
Well, in your SqlDataSource, you're not specifying that the UpdateCommand is talking to a stored procedure... you need to specify the UpdateCommandType, too! (not just the SelectCommandType)
<asp:SqlDataSource ID="JobApprovalsDS" runat="server"
ConnectionString="<%$ ConnectionStrings:JobClose %>"
SelectCommand="up_JobApprovalsSelect"
SelectCommandType="StoredProcedure"
UpdateCommand="up_JobApprovalsUpdate"
UpdateCommandType="StoredProcedure" > **** this is missing from your code!
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" />
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" />
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>
I was following this so far everything is fine with a textbox but when try to modify it to checkbox it gives an error: Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.Web.UI.WebControls.CheckBox'.
Its working when I use TextBox but how can I make all the CheckBox editable in bulk updates?
Here's the sample code behind
private bool tableCopied = false;
private DataTable originalDataTable;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
if (!tableCopied)
{
originalDataTable = ((DataRowView)e.Row.DataItem).Row.Table.Copy();
ViewState["originalValueTable"] = originalDataTable;
tableCopied = true;
}
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
originalDataTable = (DataTable)ViewState["originalValueTable"];
foreach (GridViewRow r in GridView1.Rows)
if (IsRowModified(r))
{
GridView1.UpdateRow(r.RowIndex, false);
}
tableCopied = false;
GridView1.DataBind();
}
protected bool IsRowModified(GridViewRow r)
{
int currentID;
string currentreservedate;
string currentisapproved;
currentID = Convert.ToInt32(GridView1.DataKeys[0].Value);
currentreservedate = ((TextBox)r.FindControl("reservedateTextBox")).Text;
currentisapproved = ((CheckBox)r.FindControl("isapprovedCheckBox")).Text;
DataRow row = originalDataTable.Select(String.Format("reservationid = {0}", currentID))[0];
if (!currentreservedate.Equals(row["reservedate"].ToString()))
if (!currentisapproved.Equals(row["isapproved"].ToString()))
{
return true;
}
return false;
}
}
}
Here's the aspx markup
Pending Reservation<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT dbo.BookReservation.reservationid, dbo.BookReservation.bookid, dbo.BookReservation.EmployeeID, dbo.BookReservation.reservedate, dbo.BookReservation.isapproved, dbo.BookReservation.reschedule, dbo.BookReservation.isdeleted, dbo.TblBooks.booktitle FROM dbo.BookReservation INNER JOIN dbo.TblBooks ON dbo.BookReservation.bookid = dbo.TblBooks.bookid"
DeleteCommand="DELETE FROM [BookReservation] WHERE [reservationid] = #reservationid"
InsertCommand="INSERT INTO [BookReservation] ([bookid], [EmployeeID], [reservedate], [isapproved], [reschedule], [isdeleted]) VALUES (#bookid, #EmployeeID, #reservedate, #isapproved, #reschedule, #isdeleted)"
UpdateCommand="UPDATE [BookReservation] SET [bookid] = #bookid, [EmployeeID] = #EmployeeID, [reservedate] = #reservedate, [isapproved] = #isapproved, [reschedule] = #reschedule, [isdeleted] = #isdeleted WHERE [reservationid] = #reservationid">
<DeleteParameters>
<asp:Parameter Name="reservationid" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="EmployeeID" Type="String" />
<asp:Parameter Name="reservedate" Type="DateTime" />
<asp:Parameter Name="isapproved" Type="Boolean" />
<asp:Parameter Name="reschedule" Type="Boolean" />
<asp:Parameter Name="isdeleted" Type="Boolean" />
<asp:Parameter Name="reservationid" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="bookid" Type="Int64" />
<asp:Parameter Name="EmployeeID" Type="String" />
<asp:Parameter Name="reservedate" Type="DateTime" />
<asp:Parameter Name="isapproved" Type="Boolean" />
<asp:Parameter Name="reschedule" Type="Boolean" />
<asp:Parameter Name="isdeleted" Type="Boolean" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true"
DataKeyNames="reservationid, bookid, EmployeeID, reservedate " DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" >
<Columns>
<asp:BoundField DataField="reservationid" HeaderText="reservationid"
InsertVisible="False" ReadOnly="True"
SortExpression="reservationid" Visible="False" />
<asp:BoundField DataField="bookid" HeaderText="bookid"
SortExpression="bookid" Visible="False" />
<asp:BoundField DataField="booktitle" HeaderText="Title"
SortExpression="booktitle" />
<asp:BoundField DataField="EmployeeID" HeaderText="Reserved by"
SortExpression="EmployeeID" />
<%--<asp:BoundField DataField="reservedate" HeaderText="Date reserved"
SortExpression="reservedate" />--%>
<asp:TemplateField HeaderText="Reserve Date"
SortExpression="reservedate">
<EditItemTemplate>
<asp:TextBox ID="reservedateTextBox" runat="server" Text='<%# Bind("reservedate") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="reservedateTextBox" runat="server" Text='<%# Bind("reservedate") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:CheckBoxField DataField="isapproved" HeaderText="Approved"
SortExpression="isapproved" />--%>
<asp:TemplateField HeaderText="Apprvoed"
SortExpression="isapproved">
<EditItemTemplate>
<asp:TextBox ID="isapprovedCheckBox" runat="server" Text='<%# Bind("isapproved") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="isapprovedCheckBox" runat="server" Text='<%# Bind("isapproved") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CheckBoxField DataField="reschedule" HeaderText="Reschedule"
SortExpression="reschedule" />
<asp:CheckBoxField DataField="isdeleted" HeaderText="Deleted"
SortExpression="isdeleted" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button ID="UpdateButton" runat="server" Text="Update" OnClick="UpdateButton_Click" />
<br />
Help would be much appreciated! Thanks in advance!
I think that to answer this question, the contents of the aspx file are required, but I'll try to help. More than likely the element in your form, isapprovedCheckBox, is still set to the the tag asp:Text. As a result the system can't convert the textbox to a checkbox.
Could you include your aspx content as well?