I'm having a problem with an update function I'm trying to create for a group website I'm building. I have code in place that uses a SQL datasource and a list view with edit functionality. Basically I'm having a problem updating the table. I only want to update certain fields within the table in the DB but I only know how to update them all using the ID as a datakey name. Can someone help me/direct me as to what I need to add for the code to just update the fields I want? Basically I just want to be able to edit the name, description and units fields.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:UniString %>"
SelectCommand="SELECT [module_name], [module_desc], [module_units]
FROM [modules] ORDER BY [module_name], [module_desc], [module_units]"
UpdateCommand="UPDATE [modules] SET [module_name]= #Module_Name, [module_desc]=#Module_Description,
[module_units]=#Module_Units"
>
<UpdateParameters>
<asp:Parameter Name="Module_Name" Type="String" />
<asp:Parameter Name="Module_Description" Type="String" />
<asp:Parameter Name="Module_Units" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</p>
<asp:ListView ID="ListView1" runat="server"
DataSourceID="SqlDataSource1"
**DataKeyNames= ""** >
<AlternatingItemTemplate>
<span style="">module_name:
<asp:Label ID="module_nameLabel" runat="server" Text='<%# Eval("module_name") %>' />
<br />
module_desc:
<asp:Label ID="module_descLabel" runat="server" Text='<%# Eval("module_desc") %>' />
<br />
module_units:
<asp:Label ID="module_unitsLabel" runat="server" Text='<%# Eval("module_units") %>' />
<br />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
<br />
</span>
</AlternatingItemTemplate>
<EditItemTemplate>
<span style="">module_name:
<asp:TextBox ID="module_nameTextBox" runat="server" Text='<%# Bind("module_name") %>' />
<br />
module_desc:
<asp:TextBox ID="module_descTextBox" runat="server" Text='<%# Bind("module_desc") %>'
TextMode="MultiLine" Columns="30" Rows="10" />
<br />
module_units:
<asp:TextBox ID="module_unitsTextBox" runat="server" Text='<%# Bind("module_units") %>' />
<br />
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
<br /><br /></span>
</EditItemTemplate>
I have no back-end code on the CS file as I don't think its needed also I haven't added the whole list view as its long and probably isn't needed to be displayed. I have put ** beside the key names as I think that's where the error is coming from. When I had something such as module name in there, I think it works, but it updates everything to the same name, desciption etc. I want to individually change each of the list edits to be a different module name, description etc.
Briefly you should
Change select and select ID column from DB SelectCommand="SELECT [module_id]
Change update "UPDATE
[modules] SET [module_name]= #Module_Name,
[module_desc]=#Module_Description,
[module_units]=#Module_Units where [module_id]=#Module_ID"
Set the datakey column to "module_id" us you mention.
Add a hidden colummn into the listview
Related
I have a fairly simple FormView control I'm using with ASP.Net/C#. My issue is when I use the code to redirect after the update/edit, the FormView won't actually perform the update, but, it will Redirect. I won't post all of the FormView code because it does update when I comment out the Redirect code. I think what I am asking is how to change the code to update while using the Redirect Code? Again, the update works fine when I don't do the Redirect! Is it possible the redirect is happening before the update can take place?
Code for Redirect
<script runat="server">
protected void FormView1_ItemUpdating(Object sender, FormViewUpdateEventArgs e)
{
{
Response.Redirect("redirect_main.aspx");
}
}
</script>
FormView1
<asp:FormView ID="FormView1" runat="server" onitemupdating="FormView1_ItemUpdating" DataKeyNames="req_submitted_key" DataSourceID="SqlDataSource2" DefaultMode="Edit" >
<EditItemTemplate>
req_submitted_key:
<asp:Label ID="req_submitted_keyLabel1" runat="server" Text='<%# Eval("req_submitted_key") %>' />
<br />
Role_job_title:
<asp:TextBox ID="Role_job_titleTextBox" runat="server" Text='<%# Bind("Role_job_title") %>' />
<br />
requestname:
<asp:TextBox ID="requestnameTextBox" runat="server" Text='<%# Bind("requestname") %>' />
<br />
submitted_by_name:
<asp:TextBox ID="submitted_by_nameTextBox" runat="server" Text='<%# Bind("submitted_by_name") %>' />
<br />
Submitted_by_email:
<asp:TextBox ID="Submitted_by_emailTextBox" runat="server" Text='<%# Bind("Submitted_by_email") %>' />
<br />
submitted_date:
<asp:TextBox ID="submitted_dateTextBox" runat="server" Text='<%# Bind("submitted_date") %>' />
<br />
submitted_by_comment:
<asp:TextBox ID="submitted_by_commentTextBox" runat="server" Text='<%# Bind("submitted_by_comment") %>' />
<br />
approved_denied:
<asp:TextBox ID="approved_deniedTextBox" runat="server" Text='<%# Bind("approved_denied") %>' />
<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>
Yes...it redirecting before the update. Take your redirect out of ItemUpdating and put it in ItemUpdated.
From: DetailsView.ItemUpdatint Event
DetailsView.ItemUpdating Event
Occurs when an Update button within a DetailsView control is clicked,
but before the update operation.
DetailsView.ItemUpdated Event
Occurs when an Update button within a DetailsView control is clicked,
but after the update operation.
DetailsView.ItemUpdated Event
Your code should look like:
protected void FormView1_ItemUpdated(Object sender, FormViewUpdateEventArgs e)
{
Response.Redirect("redirect_main.aspx");
}
While working on asp.Net webforms. I realize and got many complains. dropdown selected Index is taking huge amount of time to bind another Dropdown on Selected Index Change did very simple thing to check but no luck.
<div class="col-sm-4">
<div>
<asp:Label Text="Category *" ID="LabelCompanyCategory" runat="server"></asp:Label>
</div>
<div>
<telerik:RadComboBox ID="DropDownListCompanyCategory" runat="server"
AutoPostBack="true" AppendDataBoundItems="true"
Width="100%" CssClass="form-control" CausesValidation="False"
DataSourceID="SqlDataSourceCategory" DataTextField="catDescription" DataValueField="CatId"
OnSelectedIndexChanged="DropDownListCompanyCategory_SelectedIndexChanged"
EmptyMessage="Select Category">
</telerik:RadComboBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="please select a category" Display="Dynamic"
ControlToValidate="DropDownListCompanyCategory" ForeColor="Red" ID="rfv2"></asp:RequiredFieldValidator>
<asp:SqlDataSource ID="SqlDataSourceCategory" ConnectionString='<%$ ConnectionStrings:MainConnection %>' runat="server"
SelectCommand=" select CatId, Catdescription from dbo.category"></asp:SqlDataSource>
</div>
</div>
<div class="col-sm-4" runat="server" id="DivActivity" visible="false">
<div>
<asp:Label Text="Activity *" ID="LabelCompanyActivity" runat="server"></asp:Label>
</div>
<div>
<telerik:RadComboBox ID="DropDownListActivity" runat="server" CssClass="form-control" Width="100%"
AppendDataBoundItems="false" DataSourceID="SqlDataSourceActivity" EmptyMessage="Select Activity" CausesValidation="False"
DataTextField="activity" DataValueField="id">
</telerik:RadComboBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="please select a Activity"
ControlToValidate="DropDownListActivity" ForeColor="Red" Display="Dynamic" ID="RequiredFieldValidator1"></asp:RequiredFieldValidator>
</div>
<asp:SqlDataSource ID="SqlDataSourceActivity" ConnectionString='<%$ ConnectionStrings:MainConnection %>' runat="server"
SelectCommand="select activity, id from activity where catid=#CategoryId">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListCompanyCategory" PropertyName="SelectedValue"
DefaultValue="0" Name="CategoryId" />
</SelectParameters>
</asp:SqlDataSource>
</div>
Back end Code:
protected void DropDownListCompanyCategory_SelectedIndexChanged(object sender, EventArgs e)
{
DivActivity.Visible = true;
DropDownListActivity.DataBind();
}
Telerik Components are amazingly fast, but you must configure them appropriately and use the RadAjaxManager.
Follow the example here:
http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/multiplecomboboxes/defaultcs.aspx
if the dataset is very large and the performance is still not good enough you must configure the loadOnDemand or revert to client side data binding.
I am creating a GridView that will initially have a blank row (consisting of 2 DropDownLists and one TextBox). There is a button in the footer that will allow a user to add another row after they have filled in the first. I am binding this to a SqlDataSource so I am able to use the InsertCommand to call a stored procedure to Insert the user's data into a database table. However, I am not sure how to go about adding an empty row. Right now, the headers will not show up until a postback call is executed. I have seen tutorials that use a DataTable to add an empty row, but I could not get this to work with the bound DropDownLists. Is there a way to add a row to the SqlDataSource from code behind? The following is the relevant part of my ASP page:
<table>
<tr>
<td colspan="2" style="width:100%">
<div>
<asp:UpdatePanel ID="upUpdateChecklist" runat="server">
<ContentTemplate>
<asp:GridView ID="gvUpdate" runat="server" AutoGenerateColumns="False"
CssClass="mGrid" PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt"
ShowHeaderWhenEmpty="true"
Visible="true" CellSpacing="2"
CellPadding="2" ShowFooter="true"
DataSourceID="dsUpdate">
<Columns>
<asp:TemplateField HeaderText="Division/Context">
<ItemTemplate>
<asp:DropDownList ID="ddDivision" runat="server" DataTextField="Division" DataValueField="ID" DataSourceID="dsDivision"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Application">
<ItemTemplate>
<asp:DropDownList ID="ddApplication" runat="server" DataTextField="Application" DataValueField="ID" DataSourceID="dsApplication" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task">
<ItemTemplate>
<asp:TextBox ID="tbTask" runat="server" ReadOnly="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="btnAdd" Text="Add New Row" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</td>
</tr>
</table>
<asp:SqlDataSource ID="dsApplication" SelectCommand="SELECT ID, ApplicationName FROM Automation.dbo.Applications"
runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsDivision" SelectCommand="SELECT ID, Division FROM Automation.dbo.Division ORDER BY Application, Division"
runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsUpdate" runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"
SelectCommand="">
<InsertParameters>
<asp:Parameter Name="Division" DbType="Int32" />
<asp:Parameter Name="Application" DbType="Int32" />
<asp:Parameter Name="Task" DbType="String" />
</InsertParameters>
</asp:SqlDataSource>
I am not completely sure I understand how do you intend to achieve what you want but if you want to generate an empty row, change the select command of the SQL data source to do a union with an empty dummy row.
<asp:SqlDataSource ID="dsApplication" SelectCommand="SELECT ID, ApplicationName FROM Automation.dbo.Applications UNION select -1 as ID, '' as ApplicationName "
I want To Add tow level nested DataList so that the second level data source takes parameters as the ID of the first level.
How can I do this?
Here's my code:
</tr>
</table>
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
</asp:DataList>
<asp:TextBox ID="TextBox1" runat="server" Height="63px" Width="619px"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Height="34px" Text="Comment" Width="116px" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Social_NetworkConnectionString %>" SelectCommand="MSP_PostGetComments" SelectCommandType="StoredProcedure">
<SelectParameters>
<!--Want to add parametar from The first Level!-->
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
I have a table that has Edit and Delete links in each row. I am supposed to be able to click either Edit or Delete and it would do it. The edit link works but the delete has this error:
Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified.
My tables data source is sqlDataSource1.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:PayrollSystem_DBConnectionString %>"
ProviderName="<%$ ConnectionStrings:PayrollSystem_DBConnectionString.ProviderName %>"
SelectCommand="SELECT [UserID], [UserName], [UserPassword], [SecurityLevel] FROM [tblUserLogin]">
</asp:SqlDataSource>
</div>
<div align="center">
<asp:Label ID="Label1" runat="server" Text="Manage Users"></asp:Label>
<p>
<asp:Label ID="Label2" runat="server" Text="User Name:"></asp:Label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="Label3" runat="server" Text="Password:"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="Label4" runat="server" Text="Security Level:"></asp:Label>
<asp:DropDownList ID="drpdwnlstSecurityLevel" runat="server"
onselectedindexchanged="drpdwnlstSecurityLevel_SelectedIndexChanged">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>U</asp:ListItem>
</asp:DropDownList>
</p>
<p>
<asp:Button ID="btnAddUser" runat="server" onclick="btnAddUser_Click1"
Text="Add User" />
</p>
<p>
<asp:Label ID="lblError" runat="server"></asp:Label>
</p>
</div>
<p>
</p>
<div align="center">
<asp:GridView ID="tblUserLogin" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="UserID" HeaderText="UserID" InsertVisible="False"
SortExpression="UserID"></asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName"></asp:BoundField>
<asp:BoundField DataField="UserPassword" HeaderText="UserPassword"
SortExpression="UserPassword"></asp:BoundField>
<asp:BoundField DataField="SecurityLevel" HeaderText="SecurityLevel"
SortExpression="SecurityLevel"></asp:BoundField>
<asp:CommandField ShowEditButton="True"></asp:CommandField>
<asp:CommandField ShowDeleteButton="True"></asp:CommandField>
</Columns>
</asp:GridView>
</form>
</body>
The table doesn't have a primary key, that's why you could not autogenerate the update and delete statments.
It's probably something like this:
<asp:SqlDataSource
id="AccessDataSource1"
runat="server"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:PayrollSystem_DBConnectionString %>"
SelectCommand="SELECT [UserID], [UserName], [UserPassword], [SecurityLevel] FROM tblUserLogin"
DeleteCommand="DELETE FROM [tblUserLogin] WHERE UserID=#UserID">
<DeleteParameters>
<asp:Parameter Name="UserID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
You have to add a DeleteCommand to your SqlDataSource. Of course you have to change the ConnectionString and the tableNames. In your DataGridView you have a CommandField to show the DeleteButton. That's why your SqlDataSource requires a DeleteCommand.
If you want to do it by the wizard, you can configure the SqlDataSource by clicking on the little arrow on top right. Under advanced options you can generate insert, update and delete commands, because you probably need them as well.
There is a nice tutorial about SqlDataSource