Working with .NET 2.0, I have a FormView with fields bound to an ObjectDataSource (ODS). Those fields start out with the correct values—that is, they come from the Person object when the ODS’s SelectMethod is called—but after making changes and calling the ODS’s UpdateMethod, I can’t get these new—or any—values from the fields from anywhere in the update’s call stack. My code looks like this:
<div id="personInfo"><asp:FormView ID="fvwPerson" runat="server" DataSourceID="srcPerson" DefaultMode="Edit">
<EditItemTemplate>
<table border="0" width="100%" style="font-weight:bold">
…
<td align="right">
First Name:
</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName")%>' />
Middle Name: <asp:TextBox ID="txtMiddleName" runat="server"
Text='<%# Bind("MiddleName")%>' />
Last Name:
<asp:TextBox ID="TextBox5" runat="server"
Text='<%# Bind("LastName")%>' />
…
</EditItemTemplate>
</asp:FormView>
</div>
…
<asp:ObjectDataSource ID="srcPerson" runat="server" SelectMethod="GetPersonInfo" UpdateMethod="SavePersonInfo"
TypeName="BLL.Person">
<SelectParameters>
<asp:SessionParameter Name="personID" SessionField="personid" DefaultValue="0" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="firstName" Type="String" Direction="Input" DefaultValue="Fail 1!" />
<asp:Parameter Name="middleName" Type="String" Direction="Input" DefaultValue="Fail 2!" />
<asp:Parameter Name="lastName" Type="String" Direction="Input" DefaultValue="Fail 3!" />
</UpdateParameters>
</asp:ObjectDataSource>
In SavePerson, the parameter values are “Fail 1!”, “Fail 2!”, and “Fail 3!”, respectively. I’ve also tried using FormParameters, with the same results:
<asp:FormParameter Name="firstName" FormField="txtFirstName" Type="String" Direction="Input" DefaultValue="Fail 1!" />
<asp:FormParameter Name="middleName" FormField="txtMiddleName" Type="String" Direction="Input" DefaultValue="Fail 2!" />
<asp:FormParameter Name="lastName" FormField="txtLastName" Type="String" Direction="Input" DefaultValue="Fail 3!" />
I also tried preceding the field names with "fvwPerson.". I also tried handling the Updating event for the ODS but e.InputParameters all began with "Fail". I really thought I was following all the right examples, what am I doing wrong? Please save me from having to call DirectCast(fvwPerson.FindControl("fieldName"), TextBox).Text 38 times! Thank you…
EDIT 7/6/10: I probably should have mentioned that I have only the item template for editing, not one for viewing (I didn't feel I should have to duplicate all those controls), and as the FormView tag indicates, edit is its default mode. Is that a potential source of my problem?
Related
I have gridview and above gridview i have two textbox. when i fill textbox and cleck add button, the content should add to the gridview and sql database dynamically, and when i click the edit button which is presented in each row of the gridview, i should get the values back to the above textboxes, ADD should change to update button, after i update gridview has to populate again with updated data.
This is my HTML Page design
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
<div>
<table align="center" style="width:50%;">
<tr>
<td class="auto-style1">
<asp:Label ID="Label1" runat="server" Text="Country Name" Font-Bold="True" ForeColor="Red"></asp:Label>
</td>
<td>
<asp:TextBox ID="Text1" runat="server" Width="180px"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label2" runat="server" Text="Country Notes" Font-Bold="True" ForeColor="Red"></asp:Label>
</td>
<td>
<asp:TextBox ID="Text2" runat="server" Width="180px"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td>
<br />
<asp:Button ID="Button1" runat="server" Text="Add" BackColor="#990000" ForeColor="White" OnClick="Button1_Click" />
</td>
<td> </td>
<td> </td>
</tr>
</table>
<br />
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="PageIndexChanging" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="CountryID" DataSourceID="SqlDataSource1" OnRowEditing="cmd = new SqlCommand("insert into country1 (Name,CountryNotes) values(#Name, #CountryNotes)", con);" >
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="CountryID" HeaderText="CountryID" ReadOnly="True" SortExpression="CountryID" InsertVisible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="CountryNotes" HeaderText="CountryNotes" SortExpression="CountryNotes" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ATSConnectionString %>" DeleteCommand="DELETE FROM [country1] WHERE [CountryID] = #original_CountryID AND (([Name] = #original_Name) OR ([Name] IS NULL AND #original_Name IS NULL)) AND (([CountryNotes] = #original_CountryNotes) OR ([CountryNotes] IS NULL AND #original_CountryNotes IS NULL))" InsertCommand="INSERT INTO [country1] ([Name], [CountryNotes]) VALUES (#Name, #CountryNotes)" SelectCommand="SELECT * FROM [country1]" UpdateCommand="UPDATE [country1] SET [Name] = #Name, [CountryNotes] = #CountryNotes WHERE [CountryID] = #original_CountryID AND (([Name] = #original_Name) OR ([Name] IS NULL AND #original_Name IS NULL)) AND (([CountryNotes] = #original_CountryNotes) OR ([CountryNotes] IS NULL AND #original_CountryNotes IS NULL))" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}">
<DeleteParameters>
<asp:Parameter Name="original_CountryID" Type="Int32" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_CountryNotes" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="CountryNotes" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="CountryNotes" Type="String" />
<asp:Parameter Name="original_CountryID" Type="Int32" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_CountryNotes" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
You need to handle this in OnRowEditing method.
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
e.NewEditIndex -- will get you the Index of gridview, store this in some session or some variable .Helps you while you updating
//Assign value to the textbox.
Text1.text=GridView1.Rows[e.NewEditIndex].Cells[0].Text
Text2.text=GridView1.Rows[e.NewEditIndex].Cells[1].Text
}
And in 'Add' button event check for EditIndex is NULL
public void Button1_Click(object sender, System.EventArgs e)
{
If (EditIndex is null)
/// DO Insert
Else
// Do Update with EditIndex
}
This was working correctly, but now its inserting duplicate rows everytime I perform an insert from the page. Both rows are good with all data inserted correctly, but its putting the record in the database twice each time. How do I stop this?
VB.net code:
Protected Sub gvOnCallSchedule_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles gvOnCallSchedule.RowCommand
If e.CommandName = "Insert" AndAlso Page.IsValid Then
Try
dsOncallGroup.Insert()
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical, "SQL Error")
End Try
End If
If e.CommandName = "Delete" Then
Try
dsOncallGroup.Delete()
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical, "SQL Error")
End Try
'Response.Redirect("Default.aspx")
End If
End Sub
Protected Sub dsOnCallGroup_Inserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs) Handles dsOncallGroup.Inserting
Dim strddlGroupName As New DropDownList
Dim txtStartOnCall As New TextBox
Dim txtEndOnCall As New TextBox
Dim txtRank As New TextBox
Dim strddlEmpName As New DropDownList
Dim strddlOCPreferredContact As New DropDownList
Dim txtEmpWorkPhone As New TextBox
Dim txtEmpHomePhone As New TextBox
Dim txtEmpHomeCellPhone As New TextBox
Dim txtEmpAltPhone As New TextBox
strddlGroupName = CType(gvOnCallSchedule.FooterRow.FindControl("ftrddlOCGroup"), DropDownList)
txtStartOnCall = CType(gvOnCallSchedule.FooterRow.FindControl("txtStartOnCallFtr"), TextBox)
txtEndOnCall = CType(gvOnCallSchedule.FooterRow.FindControl("txtEndOnCallFtr"), TextBox)
txtRank = CType(gvOnCallSchedule.FooterRow.FindControl("txtCallOrderFtr"), TextBox)
strddlEmpName = CType(gvOnCallSchedule.FooterRow.FindControl("ddlAnalystFtr"), DropDownList)
strddlOCPreferredContact = CType(gvOnCallSchedule.FooterRow.FindControl("ftrddlOCPreferredContact"), DropDownList)
txtEmpWorkPhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtWorkPhoneFtr"), TextBox)
txtEmpHomePhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtHomePhoneFtr"), TextBox)
txtEmpHomeCellPhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtHomeCellPhoneFtr"), TextBox)
txtEmpAltPhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtAltPhoneFtr"), TextBox)
e.Command.Parameters("#fldOnCallGroup").Value = strddlGroupName.Text
e.Command.Parameters("#fldStartOnCall").Value = txtStartOnCall.Text
e.Command.Parameters("#fldEndOnCall").Value = txtEndOnCall.Text
e.Command.Parameters("#fldRank").Value = txtRank.Text
e.Command.Parameters("#fldEmpName").Value = strddlEmpName.Text
e.Command.Parameters("#fldOCPreferredContact").Value = strddlOCPreferredContact.Text
e.Command.Parameters("#fldEmpWorkPhone").Value = txtEmpWorkPhone.Text
e.Command.Parameters("#fldEmpHomePhone").Value = txtEmpHomePhone.Text
e.Command.Parameters("#fldEmpHomeCellPhone").Value = txtEmpHomeCellPhone.Text
e.Command.Parameters("#fldEmpAltPhone").Value = txtEmpAltPhone.Text
End Sub
ASP code
Datasource:
<asp:SqlDataSource ID="dsOncallGroup" runat="server"
ConnectionString="<%$ ConnectionStrings:DiscussSQLConnectionString %>"
SelectCommand="SELECT * FROM tblOnCallSchedule WHERE (tblOnCallSchedule.fldOnCallGroup = #fldOnCallGroup)
AND (tblOnCallSchedule.fldEndOnCall > GETDATE())
ORDER BY tblOnCallSchedule.fldOnCallGroup, tblOnCallSchedule.fldEndOnCall, tblOnCallSchedule.fldRank"
OldValuesParameterFormatString="original_{0}"
DeleteCommand="DELETE FROM tblOnCallSchedule WHERE fldOCID = #original_fldOCID"
InsertCommand="INSERT INTO [tblOnCallSchedule] ([fldOnCallGroup], [fldStartOnCall], [fldEndOnCall], [fldRank], [fldEmpName], [fldOCPreferredContact], [fldEmpWorkPhone],
[fldEmpHomePhone],[fldEmpHomeCellPhone],[fldEmpAltPhone])
VALUES (#fldOnCallGroup, #fldStartOnCall, #fldEndOnCall, #fldRank, #fldEmpName, #fldOCPreferredContact, #fldEmpWorkPhone, #fldEmpHomePhone,
#fldEmpHomeCellPhone, #fldEmpAltPhone)"
UpdateCommand="UPDATE [tblOnCallSchedule] SET [fldOnCallGroup] = #fldOnCallGroup, [fldStartOnCall] = #fldStartOnCall,
[fldEndOnCall] = #fldEndOnCall, [fldRank] = #fldRank, [fldEmpName] = #fldEmpName, [fldOCPreferredContact] = #fldOCPreferredContact,
[fldEmpWorkPhone] = #fldEmpWorkPhone,[fldEmpHomePhone] = #fldEmpHomePhone,[fldEmpHomeCellPhone] = #fldEmpHomeCellPhone,
[fldEmpAltPhone] = #fldEmpAltPhone
WHERE [fldOCID] = #original_fldOCID">
<SelectParameters>
<asp:ControlParameter ControlID="cboOncallGroup" Name="fldOnCallGroup"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name= "original_fldOCID" Type="Int16" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="fldOnCallGroup" Type="String" ConvertEmptyStringToNull="false" />
<asp:Parameter Name="fldStartOnCall" DbType ="Date" />
<asp:Parameter Name="fldEndOnCall" DbType ="Date" />
<asp:Parameter Name= "fldRank" Type="Int16" />
<asp:Parameter Name= "fldEmpName" Type="String" />
<asp:Parameter Name= "fldOCPreferredContact" Type="String" />
<asp:Parameter Name= "fldEmpWorkPhone" Type="String" />
<asp:Parameter Name= "fldEmpHomePhone" Type="String" />
<asp:Parameter Name= "fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name= "fldEmpAltPhone" Type="String" />
<asp:Parameter Name="original_fldOnCallGroup" Type="String" ConvertEmptyStringToNull="false" />
<asp:Parameter Name="original_fldStartOnCall" DbType="Date" />
<asp:Parameter Name="original_fldEndOnCall" DbType="Date" />
<asp:Parameter Name="original_fldRank" Type="Int16" />
<asp:Parameter Name="original_fldEmpName" Type="String" />
<asp:Parameter Name="original_fldOCPreferredContact" Type="String" />
<asp:Parameter Name="original_fldEmpWorkPhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomePhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name="original_fldEmpAltPhone" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="fldOnCallGroup" Type="String" />
<asp:Parameter DbType="Date" Name="fldStartOnCall" />
<asp:Parameter DbType="Date" Name="fldEndOnCall" />
<asp:Parameter Name="fldRank" Type="Int16" />
<asp:Parameter Name="fldEmpName" Type="String" />
<asp:Parameter Name="fldOCPreferredContact" Type="String" />
<asp:Parameter Name="fldEmpWorkPhone" Type="String" />
<asp:Parameter Name="fldEmpHomePhone" Type="String" />
<asp:Parameter Name="fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name="fldEmpAltPhone" Type="String" />
<asp:Parameter Name="original_fldOnCallGroup" Type="String" ConvertEmptyStringToNull="false" />
<asp:Parameter Name="original_fldStartOnCall" DbType="Date" />
<asp:Parameter Name="original_fldEndOnCall" DbType="Date" />
<asp:Parameter Name="original_fldRank" Type="Int16" />
<asp:Parameter Name="original_fldEmpName" Type="String" />
<asp:Parameter Name="original_fldOCPreferredContact" Type="String" />
<asp:Parameter Name="original_fldEmpWorkPhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomePhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name="original_fldEmpAltPhone" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
Gridview Code:
<asp:Gridview ID="gvOnCallSchedule" runat="server" AllowSorting="True" AllowPaging="True" showfooter="true"
OnRowUpdating="GvOnCall_Update" onRowCommand="gvOnCallSchedule_RowCommand" DataKeyNames="fldOCID"
BackColor="Aqua" DataSourceID="dsOncallGroup" Font-Bold="True"
Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" ForeColor="#333333" HorizontalAlign="Justify"
CellPadding="4" GridLines="None" PageSize="20" AutoGenerateColumns ="false"
OnSelectedIndexChanged="cboOnCallGroup_SelectedIndexChanged">
<Columns>
<asp:TemplateField ShowHeader="false">
<FooterTemplate>
<asp:ImageButton ID="AddButton" runat="server" CommandName="Insert" ImageURL="~/images/New.png"
Text="Add" ToolTip="Add New On Call Record" />
</FooterTemplate>
<EditItemTemplate>
<asp:ImageButton ID="UpdateButton" runat="server" CausesValidation="False" CommandName="Update"
ImageUrl="~/images/Save.png" Text="Update" ToolTip="Update" />
<asp:ImageButton ID="CancelButton" runat="server" CausesValidation="false" CommandName="Cancel"
ImageURL="~/images/Cancel.png" Text="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
ImageUrl="~/images/Edit.gif" Text="Edit" ToolTip="Edit" />
<asp:ImageButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
ImageUrl="~/images/Delete.png" Text="Delete" ToolTip="Delete" />
</ItemTemplate>
<ItemStyle Wrap="false" />
</asp:TemplateField>
<asp:Boundfield headertext="fldOCID" datafield="fldOCID" sortexpression="fldOCID" visible="false" InsertVisible="False" />
<asp:TemplateField headertext="On Call Group">
<ItemTemplate>
<asp:DropDownList ID="ddlOCGroup" width="200px" runat="server" datavaluefield="fldOnCallGroup" Enabled="false"
DataSourceID= "dsListbox" SelectedValue="<%# Bind('fldOnCallGroup') %>">
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ftrddlOCGroup" width="200px" runat="server" datavaluefield="fldOnCallGroup"
DataSourceID= "dsListbox" SelectedValue="<%# Bind('fldOnCallGroup') %>">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Start Date/Time">
<ItemTemplate>
<asp:Textbox ID="txtStartOnCall" runat="server" Text='<%# Bind("fldStartOnCall") %>'>
</asp:Textbox>
<asp:Image ID="calpopup" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="StartOnCallCal" runat="server" TargetControlID="txtStartOnCall"
PopupButtonID="calpopup" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtStartOnCallFtr" runat="server" Text='<%# Bind("fldStartOncall") %>'>
</asp:Textbox>
<asp:Image ID="calpopupFtr" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="StartOnCallCalFtr" runat="server" TargetControlID="txtStartOnCallFtr"
PopupButtonID="calpopupFtr" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="End Date/Time">
<ItemTemplate>
<asp:Textbox ID="txtEndOnCall" runat="server" Text='<%# Bind("fldEndOnCall") %>'>
</asp:Textbox>
<asp:Image ID="calpopup2" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="EndOnCallCal" runat="server" TargetControlID="txtEndOnCall"
PopupButtonID="calpopup2" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtEndOnCallFtr" runat="server" Text='<%# Bind("fldEndOnCall") %>'>
</asp:Textbox>
<asp:Image ID="calpopup2Ftr" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="EndOnCallCalFtr" runat="server" TargetControlID="txtEndOnCallFtr"
PopupButtonID="calpopup2Ftr" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Call Order">
<ItemTemplate>
<asp:Textbox ID="lblCallOrder" runat="server" width="100" Text='<%# Bind("fldRank") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtCallOrderFtr" runat="server" width="100" Text='<%# Bind("fldOnCallGroup") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OnCall Analyst">
<ItemTemplate>
<asp:DropDownList ID="ddlAnalyst" runat="server" datavaluefield="fldEmpName"
datasourceID="dsEmp" SelectedValue='<%# Bind("fldEmpName") %>'>
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlAnalystFtr" runat="server" datavaluefield="fldEmpName"
DataSourceID= "dsEmp" SelectedValue='<%# Bind("fldEmpName") %>'>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Preferred Contact">
<ItemTemplate>
<asp:DropDownList ID="ddlOCPreferredContact" Width="150px" runat="server" datavaluefield="fldOCPreferredContact"
datasourceID="dsPref" SelectedValue='<%# Bind("fldOCPreferredContact") %>'>
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ftrddlOCPreferredContact" width="150px" runat="server" datavaluefield="fldOCPreferredContact"
DataSourceID= "dsPref" SelectedValue='<%# Bind("fldOCPreferredContact") %>'>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Work Phone">
<ItemTemplate>
<asp:Textbox ID="lblWorkPhone" runat="server" width="90" Text='<%# Bind("fldEmpWorkPhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtWorkPhoneFtr" runat="server" width="90" Text='<%# Bind("fldEmpWorkPhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Home Phone">
<ItemTemplate>
<asp:Textbox ID="lblHomePhone" runat="server" width="90" Text='<%# Bind("fldEmpHomePhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtHomePhoneFtr" runat="server" width="90" Text='<%# Bind("fldEmpHomePhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Personal Cell">
<ItemTemplate>
<asp:Textbox ID="lblHomeCellPhone" runat="server" width="95" Text='<%# Bind("fldEmpHomeCellPhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtHomeCellPhoneFtr" runat="server" width="95" Text='<%# Bind("fldEmpHomeCellPhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Alternate Phone">
<ItemTemplate>
<asp:Textbox ID="lblAltPhone" runat="server" width="105" Text='<%# Bind("fldEmpAltPhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtAltPhoneFtr" runat="server" width="105" Text='<%# Bind("fldEmpAltPhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:Gridview>
Put a break point on the call to dsOncallGroup.Insert() and see if it is called twice, if so then it is your code doing it and you can figure out where the second call is happening by examining the Call Stack window (Debug -> Windows -> Call Stack).
If there are not two calls in the code, then you need to investigate if a trigger has been added to your database that is somehow causing extra data to be inserted.
Anybody know how to get a Templatefield column of text-boxes to feed data typed into them back into a sql database using the SqlDataSource already being used in the gridview the templatefield is in and a buttonfield using an update function? I was just wondering if anybody has any idea how to get this to work because I haven't been able to find any way of doing this.
Please let me know if any of you need any more information to help you solve this dilemma.
I am able to get the information to feed back into the SQL server if i enable edit and change the information in the textbox as well as another column's field in the same row, but it won't update if I only change the information in the textbox whether in edit mode or just clicking the update button.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<center>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Brdcst_Nbr" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." Width="657px">
<Columns>
<asp:BoundField DataField="Brdcst_Nbr" HeaderText="Brdcst_Nbr" ReadOnly="True"
SortExpression="Brdcst_Nbr" />
<asp:BoundField DataField="Item_Nbr" HeaderText="Item_Nbr"
SortExpression="Item_Nbr" ReadOnly="True" />
<asp:CheckBoxField DataField="IsPicked" HeaderText="IsPicked"
SortExpression="IsPicked" />
<asp:TemplateField SortExpression="Row" HeaderText="Row">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Mode="twoway" AutoPostBack="true" CausesValidation="true" Text='<%# Bind("Row") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Mode="twoway" AutoPostBack="true" CausesValidation="true" Text='<%# Bind("Row") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" CommandName="Update" Text="Update" />
<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" />
</Columns>
<EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ORDCConnectionString1 %>"
DeleteCommand="DELETE FROM [Test_New_Transpick] WHERE [Brdcst_Nbr] = #original_Brdcst_Nbr AND (([Item_Nbr] = #original_Item_Nbr) OR ([Item_Nbr] IS NULL AND #original_Item_Nbr IS NULL)) AND [IsPicked] = #original_IsPicked AND (([Row] = #original_Row) OR ([Row] IS NULL AND #original_Row IS NULL))"
InsertCommand="INSERT INTO [Test_New_Transpick] ([Brdcst_Nbr], [Item_Nbr], [IsPicked], [Row]) VALUES (#Brdcst_Nbr, #Item_Nbr, #IsPicked, #Row)"
UpdateCommand="UPDATE [Test_New_Transpick] SET [IsPicked] = #IsPicked, [Row] = #Row WHERE [Brdcst_Nbr] = #original_Brdcst_Nbr"
OldValuesParameterFormatString="original_{0}"
ProviderName="<%$ ConnectionStrings:ORDCConnectionString1.ProviderName %>"
SelectCommand="SELECT * FROM [Test_New_Transpick]" >
<DeleteParameters>
<asp:Parameter Name="original_Brdcst_Nbr" Type="String" />
<asp:Parameter Name="original_Item_Nbr" Type="String" />
<asp:Parameter Name="original_IsPicked" Type="Boolean" />
<asp:Parameter Name="original_Row" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Brdcst_Nbr" Type="String" />
<asp:Parameter Name="Item_Nbr" Type="String" />
<asp:Parameter Name="IsPicked" Type="Boolean" />
<asp:Parameter Name="Row" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Item_Nbr" Type="String" />
<asp:Parameter Name="IsPicked" Type="Boolean" />
<asp:Parameter Name="Row" Type="String" />
<asp:Parameter Name="original_Brdcst_Nbr" Type="String" />
<asp:Parameter Name="original_Item_Nbr" Type="String" />
<asp:Parameter Name="original_IsPicked" Type="Boolean" />
<asp:Parameter Name="original_Row" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</center>
</body>
</html>
So, changing the property of the SQLdatabase from CompareAllValues to OverwriteChanges fixed this issue. It is now allowing the changes to be written back to the sql database by just changing the textbox in the row/column that needed to be updated without having to change anything else.
I have a DetailsView that looks just about perfect. The only thing is, there is one field that actually gets its information from another table. It is an ID field that is linked to another table in the database and I was hoping that there was some way to get that one field in the DetailsView to pull the information from the other table.
Is there a way to maybe add a + (plus sign) next to the PicklistID and then have it show the results of the other query? (SELECT TEXT FROM PICKLIST WHERE PicklistID = #PicklistID???)
UPDATE 1/11/12: The PICKLISTID in the table PICKLIST is not unique or a primary key so I cannot reference it from the SURVEY table. Is there a way to give these 2 columns a relationship so that I can just change my SELECT statement in my DetailsView to include the PICKLISTID?
UPDATE 1/23/12: I'm still stuck on this. I am not getting how to make this work at all. Help would be appreciated. I am still new....
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="Label2" runat="server" Text="<h3>Manage Questions</h3>"></asp:Label>
<table style="width: 100%">
<tr>
<td align="right">
<asp:Label ID="Label1" runat="server" Text="Select Survey:"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="dsSurvey" DataTextField="SurveyName" DataValueField="SurveyID">
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:DetailsView ID="dvSurveyQuestions" runat="server" AllowPaging="True"
AutoGenerateRows="False" CellPadding="4" DataKeyNames="QuestionID"
DataSourceID="dsSurveyQuestions" ForeColor="#333333" GridLines="None"
Height="50px" Width="100%">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="QuestionID" HeaderText="QuestionID"
InsertVisible="False" ReadOnly="True" SortExpression="QuestionID" />
<asp:TemplateField HeaderText="Question" SortExpression="Question">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"
Text='<%# Bind("Question") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"
Text='<%# Bind("Question") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblQuestion" runat="server" Text='<%# Bind("Question") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Answer Type" SortExpression="AnswerType">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
SelectedValue='<%# Bind("AnswerType") %>'>
<asp:ListItem Value="S">Single Choice (radio button)</asp:ListItem>
<asp:ListItem Value="M">Multiple Choices (checkboxes)</asp:ListItem>
<asp:ListItem Value="T">Text (textbox)</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server"
SelectedValue='<%# Bind("AnswerType") %>'>
<asp:ListItem Value="S">Single Choice (radio button)</asp:ListItem>
<asp:ListItem Value="M">Multiple Choices (checkboxes)</asp:ListItem>
<asp:ListItem Value="T">Text (textbox)</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblAnswerType" runat="server"
Text='<%# Bind("AnswerType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Question Type" SortExpression="QType">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server"
SelectedValue='<%# Bind("QType") %>'>
<asp:ListItem Value="Picklist">Picklist</asp:ListItem>
<asp:ListItem Value="Text">Text</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server"
SelectedValue='<%# Bind("QType") %>'>
<asp:ListItem Value="Picklist">Picklist</asp:ListItem>
<asp:ListItem Value="Text">Text</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblQType" runat="server" Text='<%# Bind("QType") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Answer" SortExpression="TEXT">
<EditItemTemplate>
</EditItemTemplate>
<InsertItemTemplate>
</InsertItemTemplate>
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("PicklistID") %>' />
<asp:SqlDataSource ID="dsPicklist" runat="server"
ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>"
SelectCommand="SELECT DISTINCT p.TEXT, p.PICKLISTID
FROM PICKLIST p JOIN C_Survey_Questions c
ON c.PicklistID = p.PICKLISTID
AND c.SurveyID = #SurveyID
ORDER BY p.TEXT">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenField1" Name="PicklistID"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Label ID="lblTEXT" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="QuestionNum" HeaderText="Question Number"
SortExpression="QuestionNum" />
<asp:BoundField DataField="Subsequence" HeaderText="Subsequence"
SortExpression="Subsequence" />
<asp:CheckBoxField DataField="Active" HeaderText="Active"
SortExpression="Active" />
<asp:BoundField DataField="Script" HeaderText="Script"
SortExpression="Script" />
<asp:CheckBoxField DataField="Question_Locked" HeaderText="Question Locked"
SortExpression="Question_Locked" />
<asp:BoundField DataField="QHelp" HeaderText="Question Help"
SortExpression="QHelp" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" Width="10%" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:DetailsView>
</td>
</tr>
</table>
<asp:SqlDataSource ID="dsSurvey" runat="server"
ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>"
SelectCommand="SELECT [SurveyID], [SurveyName]
FROM [C_Survey]
ORDER BY [SurveyName]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsSurveyQuestions" runat="server"
ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>"
DeleteCommand="DELETE FROM [C_Survey_Questions] WHERE [QuestionID] = #QuestionID"
InsertCommand="INSERT INTO [C_Survey_Questions] ([SurveyID], [Question], [QType],
[PickListID], [QuestionNum], [Subsequence], [Active], [Script],
[Question_Locked], [QHelp], [Createdate],
[Modifydate], [AnswerType])
VALUES (#SurveyID, #Question, #QType, #PickListID, #QuestionNum,
#Subsequence, #Active, #Script, #Question_Locked, #QHelp,
getdate(), getdate(), #AnswerType)"
SelectCommand="SELECT * FROM [C_Survey_Questions]
WHERE ([SurveyID] = #SurveyID)"
UpdateCommand="UPDATE [C_Survey_Questions] SET [SurveyID] = #SurveyID,
[Question] = #Question, [QType] = #QType,
[PickListID] = #PickListID, [QuestionNum] = #QuestionNum,
[Subsequence] = #Subsequence, [Active] = #Active,
[Script] = #Script, [Question_Locked] = #Question_Locked,
[QHelp] = #QHelp, [Modifydate] = getdate(),
[AnswerType] = #AnswerType
WHERE [QuestionID] = #QuestionID">
<DeleteParameters>
<asp:Parameter Name="QuestionID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="SurveyID" Type="Int32" />
<asp:Parameter Name="Question" Type="String" />
<asp:Parameter Name="QType" Type="String" />
<asp:Parameter Name="PickListID" Type="String" />
<asp:Parameter Name="QuestionNum" Type="Int32" />
<asp:Parameter Name="Subsequence" Type="Int32" />
<asp:Parameter Name="Active" Type="Boolean" />
<asp:Parameter Name="Script" Type="String" />
<asp:Parameter Name="Question_Locked" Type="Boolean" />
<asp:Parameter Name="QHelp" Type="String" />
<asp:Parameter Name="Createdate" Type="DateTime" />
<asp:Parameter Name="Modifydate" Type="DateTime" />
<asp:Parameter Name="AnswerType" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="SurveyID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="SurveyID" Type="Int32" />
<asp:Parameter Name="Question" Type="String" />
<asp:Parameter Name="QType" Type="String" />
<asp:Parameter Name="PickListID" Type="String" />
<asp:Parameter Name="QuestionNum" Type="Int32" />
<asp:Parameter Name="Subsequence" Type="Int32" />
<asp:Parameter Name="Active" Type="Boolean" />
<asp:Parameter Name="Script" Type="String" />
<asp:Parameter Name="Question_Locked" Type="Boolean" />
<asp:Parameter Name="QHelp" Type="String" />
<asp:Parameter Name="Modifydate" Type="DateTime" />
<asp:Parameter Name="AnswerType" Type="String" />
<asp:Parameter Name="QuestionID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</asp:Content>
Partial Class Admin_ManageQuestions
Inherits System.Web.UI.Page
Protected Sub dsPicklist_ItemSelecting(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim myControl1 As Control = FindControl("lblTEXT")
If (Not myControl1 Is Nothing) Then
myControl1.DataBind()
Else
'Control not found
End If
End Sub
Protected Sub dvSurveyQuestions_ItemInserting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles
dvSurveyQuestions.ItemInserting
'The DetailsView does not include SurveyID column...we need to set this column
during INSERT operations because each question must belong to some survey.
e.Values("SurveyID") = DropDownList1.SelectedValue
End Sub
Protected Sub dvSurveyQuestions_ItemUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles
dvSurveyQuestions.ItemUpdating
'The DetailsView does not include SurveyID column...we need to set this column
during UPDATE operations because each question must belong to some survey.
e.NewValues("SurveyID") = DropDownList1.SelectedValue
End Sub
Protected Sub dvSurveyQuestions_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dvSurveyQuestions.DataBound
'The event handler checks the row count of the DetailsView control.
If it is zero then the mode of the DetailsView is changed to Insert using
ChangeMode() method.
If dvSurveyQuestions.Rows.Count = 0 Then
dvSurveyQuestions.ChangeMode(DetailsViewMode.Insert)
End If
End Sub
End Class
You can do that, but maybe not in the way you want to.
The simplest way would be to use the DataBinding event on the DetailsView itself. Then use code to run the query.
Another way would be to create your own control as a subclass of BoundField and override the OnDataBindField method
The most performant way would be to join the data at the source in a DataView or another construct. Ideally the joining of data should be done as close to the actual source of the data (e.g. in a SQL query or view) to avoid unnecessary trips across the network to the database. For a single row it wouldn't be a big issue, but that is generally what you should do.
I have a radgrid bound to a SqlDataSource that includes a hidden, readonly column that stores a pk. I want to pass the value bound to that column to a stored procedure for Updates but the default behavior when the column is readonly is not to pass the parameter to the sqlDataSource. My question is whether there is a way to pass that value without having to go into the code behind. Here is a snippet of my asp markup.
<telerik:RadGrid ID="rgEmployees" runat="server" AutoGenerateColumns="False"
DataSourceID="sdsEmployees" GridLines="None" AllowAutomaticUpdates="true" AllowAutomaticDeletes="true">
<MasterTableView DataSourceID="sdsEmployees" EditMode="EditForms">
<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton" HeaderStyle-Width="20px"
UniqueName="Edit" HeaderText="Edit">
<HeaderStyle Width="10px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="10px" />
</telerik:GridEditCommandColumn>
<telerik:GridBoundColumn DataField="pkWorkerID" HeaderText="pkWorkerID" UniqueName="pkWorkerID" SortExpression="pkWorkerID" Visible="false" ReadOnly="true"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ContractManagerName" HeaderText="ContractManager Name" UniqueName="ContractManagerName" SortExpression="ContractManagerName" Visible="true" ReadOnly="true"></telerik:GridBoundColumn>
<telerik:GridDropDownColumn
DataField="CODE"
HeaderText="Financial Software Name"
DataSourceID="sdsFinancialSystemGetUnassignedWorkers"
ListTextField="NAME"
ListValueField="CODE"
EnableEmptyListItem="true"
DropDownControlType="RadComboBox">
</telerik:GridDropDownColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="sdsEmployees" runat="server"
ConnectionString="" ProviderName="System.Data.SqlClient"
SelectCommand="spFinancialSystemGetWorkerMappingDataSource" SelectCommandType="StoredProcedure"
UpdateCommand="spFinancialSystemMapWorkerToCode" UpdateCommandType="StoredProcedure"
DeleteCommand="spFinancialSystemRemoveWorkerMapping" DeleteCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="hfpkSystemSupportedFinancialSoftware" Name="pkSystemSupportedFinancialSystems"
Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter ControlID="hfpkSystemSupportedFinancialSoftware" Name="pkSystemSupportedFinancialSystems"
Type="Int32" PropertyName="Value" />
<asp:Parameter Name="pkWorkerID" Type="Int32" />
<asp:Parameter Name="CODE" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:ControlParameter ControlID="hfpkSystemSupportedFinancialSoftware" Name="pkSystemSupportedFinancialSystems"
Type="Int32" PropertyName="Value" />
<asp:Parameter Name="pkWorkerID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
The stored procedure prototype looks like this:
CREATE PROCEDURE [dbo].[spFinancialSystemMapWorkerToCode]
#pkWorkerID int,
#pkSystemSupportedFinancialSystems int,
#CODE nvarchar(200)
When I run the page and try to edit a record I get the following error:
Procedure or function 'spFinancialSystemMapWorkerToCode' expects parameter '#pkWorkerID', which was not supplied.
If I set readonly="false" on the It works fine but then the user can edit the primary key value which is not desired. I know work arounds in code behind but is there any way to do it straight in the markup? Thanks.
The answer is to set the ForceExtractValue to InEditMode for the pkWorkerID column
<telerik:GridBoundColumn DataField="pkWorkerID" HeaderText="pkWorkerID" UniqueName="pkWorkerID" SortExpression="pkWorkerID" Visible="false" ReadOnly="true" ForceExtractValue="InEditMode"></telerik:GridBoundColumn>
Other thing you can try is add the pkWorkerID field to the DataKeyNames collection of the grid. It should be stored in the viewstate and passed automatically to the stored procedure on edit.
Dick