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.
Related
I'm trying to update the 'question' field when I hit the 'update' button for a specific row. I am able to change the text when I hit the 'edit' button, but when I hit the 'update' button it reverts back to its original value. Nothing is changed in the database either.
Here is my markup:
<div class="divGrid" style="margin: 0 auto; width: 70%;">
<asp:GridView ID="QuestionsGridView" CssClass="gridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Width="100%" CellPadding="4" ForeColor="#333333">
<Columns>
<asp:BoundField DataField="QUESTION_NUMBER" ReadOnly="true" HeaderText="Question Number" SortExpression="QUESTION_NUMBER" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderText="Question" SortExpression="QUESTION" HeaderStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:TextBox ID="EditQuestionBox" runat="server" Height="50px" Text='<%# Bind("QUESTION") %>' TextMode="MultiLine" Width="99%" CssClass="multilineTBox" Columns="1" Rows="2"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("QUESTION") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="99%" />
</asp:TemplateField>
<asp:BoundField DataField="CAT_NAME" ReadOnly="true" HeaderText="Category" SortExpression="CAT_NAME" HeaderStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="QUES_ORDER" ReadOnly="true" HeaderText="Order in Category" SortExpression="QUES_ORDER" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
<asp:CommandField CausesValidation="False" HeaderText="Edit" ShowEditButton="True" ControlStyle-Width="4em" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#a0a0a0" Font-Bold="True" ForeColor="White" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#ffffff" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Corp_TaxConnectionString %>"
SelectCommand="SELECT tq.id, tq.question, tq.ques_order, tc.cat_name, tq.question_number FROM t01_tax_questions AS tq INNER JOIN t01_tax_categories AS tc ON tq.category = tc.cat_order ORDER BY tq.question_number"
UpdateCommand="UPDATE t01_tax_questions SET question = #question WHERE question_number = #question_number">
<UpdateParameters>
<asp:Parameter Name="question_number" Type="Int32" />
<asp:Parameter Name="question" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
And the table:
SQLServer Table
EDIT:
After looking, in my EditItemTemplate, I bind the question to the textbox. Is it possible that after I click the update button that the original question is databound to the textbox again before it tries to update? If this is the case, how would I get around that?
After looking further into it, I realized I had forgotten the apparently very important "DataKeyNames" attribute in my GridView tag. I feel really dumb. Here is the new working code.
<div class="divGrid" style="margin: 0 auto; width: 70%;">
<asp:GridView ID="QuestionsGridView" CssClass="gridView" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="ID, QUESTION_NUMBER" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="question_number" HeaderText="Question Number" SortExpression="question_number" ReadOnly="True" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="question" HeaderText="Question" SortExpression="question" >
<ControlStyle Width="100%" Height="80%" />
</asp:BoundField>
<asp:BoundField DataField="cat_name" HeaderText="Category" SortExpression="cat_name" ItemStyle-HorizontalAlign="Center">
<ControlStyle Height="80%" />
</asp:BoundField>
<asp:BoundField DataField="ques_order" HeaderText="Question Order" SortExpression="ques_order" ReadOnly="True" ItemStyle-HorizontalAlign="Center" />
<asp:CommandField HeaderText="Edit" ShowEditButton="True" CausesValidation="False" HeaderStyle-Width="4%" ItemStyle-HorizontalAlign="Center"/>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" CausesValidation="False" HeaderStyle-Width ="4%" ItemStyle-HorizontalAlign="Center" />
</Columns>
<EditRowStyle BackColor="Yellow" Width="100%" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#a0a0a0" Font-Bold="True" ForeColor="White" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#ffffff" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Corp_TaxConnectionString %>" SelectCommand="SELECT
tq.id, tq.question_number, tq.question, tc.cat_name, tq.ques_order
FROM
t01_tax_questions AS tq
INNER JOIN
t01_tax_categories AS tc
ON
tq.category = tc.cat_order
ORDER BY tq.question_number"
DeleteCommand="DELETE
FROM
t01_tax_questions
WHERE
question_number = #question_number"
UpdateCommand="UPDATE
t01_tax_questions
SET
question = #question
WHERE
id = #id">
<DeleteParameters>
<asp:Parameter Name="#question_number" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="#question"/>
<asp:Parameter Name="#id" />
</UpdateParameters>
</asp:SqlDataSource>
It is all explained in this MSDN Documentation
Thank you to all who looked at this and tried.
I have a FormView control which I only need for "Edit" mode. Here's my code
<asp:FormView ID="fvProposal" runat="server" Visible="True"
DataSourceID="sdsProposal"
DefaultMode="Edit">
<EmptyDataTemplate>
There is nothing to see here.
</EmptyDataTemplate>
<EditItemTemplate>
<span class="AttributeLabel">Title:</span>
<asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
<asp:RequiredFieldValidator runat="server" ControlToValidate="TitleTextBox" CssClass="reqfield" Text="*Required field." />
<br />
<span class="AttributeLabel">Description:</span>
<asp:TextBox ID="DescriptionTextBox" runat="server" TextMode="MultiLine" Text='<%# Bind("Description") %>' Width="40%" />
<br />
<span class="AttributeLabel">Funding Agency:</span>
<asp:TextBox ID="FundingAgencyTextBox" runat="server" Text='<%# Bind("FundingAgency") %>' />
<asp:RequiredFieldValidator runat="server" ControlToValidate="FundingAgencyTextBox" CssClass="reqfield" Text="*Required field." />
<br />
<span class="AttributeLabel">Submit for review:</span>
<asp:CheckBox ID="cbSubmit" runat="server" Checked='<%# Bind("Sub") %>'/> Check if you are ready to submit this proposal.
<br />
<span class="AttributeLabel">
<asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Proceed" />
</span>
<asp:Button ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" PostBackUrl="MyProposals.aspx"/>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit my information" CssClass="button"/><br/>
<br/>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sdsProposal" runat="server" ConnectionString="<%$ ConnectionStrings:cnIRBSS %>"
SelectCommand="SELECT [Title], [Description], [FundingAgency] FROM [Proposal] WHERE ProposalID=#PID"
UpdateCommand="UPDATE [Title] SET [Title] = #Title, [Description] = #Description, [FundingAgency] = #FundingAgency, [Submitted] = #Sub WHERE [ProposalID] = #PID">
<UpdateParameters>
<asp:SessionParameter SessionField="ProposalID" Name="PID" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="FundingAgency" Type="String" />
<asp:Parameter Name="Sub" Type="Boolean"/>
</UpdateParameters>
<SelectParameters>
<asp:SessionParameter SessionField="PID" Name="ProposalID" />
</SelectParameters>
</asp:SqlDataSource>
My problem is that FormView shows no data even though the same queries from SQLDataSource return the right data in SQLSMS. What am I missing and/or doing wrong?
I am getting this error when I try to add a search text box for my CertificateID column. I know it's becuase you can't use the "like" operation since my CertificateID column is int32 not a string. I just don't know what the code should be to make it work. I have tried Cast() function but haven't had any luck. could someone please help me with the code.
I am using the filterexpression to filter my data:
FilterExpression="[CertificateID] like '{0}%' and [Customs_Entry] like '{0}%' and [Product_Number] like '{1}%' and [Product_Name] like '{2}%'">
<asp:ControlParameter ControlID="txtCertificateID_Filter" Name="CertificateID" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
My full html is:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="CertificateID_Label" runat="server" Width="169px">Certificate Number</asp:Label>
<asp:TextBox ID="txtCertificateID_Filter" runat="server" Width="180px"></asp:TextBox>
<br />
<br />
<asp:Label ID="Customs_Entry_Label" runat="server" Width="169px">Customs Entry Number</asp:Label>
<asp:TextBox ID="txtCustoms_Entry_Filter" runat="server" Width="180px"></asp:TextBox>
<br />
<br />
<asp:Label ID="Product_Number_Label" runat="server" Width="169px">Product Number</asp:Label>
<asp:TextBox ID="txtProduct_Number_Filter" runat="server" Width="180px"></asp:TextBox>
<br />
<br />
<asp:Label ID="Customs_Name_Label" runat="server" Width="169px">Product Name</asp:Label>
<asp:TextBox ID="txtProduct_Name_Filter" runat="server" Width="180px"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnSearch" runat="server" Text="Search" />
<br />
<br />
<asp:GridView ID="Certificate" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CertificateID" DataSourceID="SqlDataSource2" ShowFooter="True">
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="AddRow" runat="server" CommandName="Insert" Text="Add" />
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CertificateID" HeaderText="Certificate Number" ReadOnly="True" SortExpression="CertificateID" />
<asp:TemplateField HeaderText="Customs Entry" SortExpression="Customs_Entry">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Customs_Entry")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtCustoms_Entry" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Customs_Entry")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Certificate" SortExpression="Date_Certificate">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Date_Certificate")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDate_Certificate" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Date_Certificate")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Invoice Number" SortExpression="Invoice_Number">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Invoice_Number")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtInvoice_Number" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Invoice_Number")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Number" SortExpression="Product_Number">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Product_Number")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtProduct_Number" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Product_Number")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Name" SortExpression="Product_Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Product_Name")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtProduct_Name" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Product_Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Category" SortExpression="Product_Category">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("Product_Category") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Category]"></asp:SqlDataSource>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="txtProduct_Category" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Category]"></asp:SqlDataSource>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Product_Category")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CPSC Regulation" SortExpression="CPSC_Regulation">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("CPSC_Regulation")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtCPSC_Regulation" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("CPSC_Regulation")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Manufactured" SortExpression="Date_Manufactured">
<EditItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Date_Manufactured")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtDate_Manufactured" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("Date_Manufactured")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Manufacturer" SortExpression="Manufacturer">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("Manufacturer")%>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Manufacturer]"></asp:SqlDataSource>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="txtManufacturer" runat="server" DataSourceID="SqlDataSource3" DataTextField="Name" DataValueField="Name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Manufacturer]"></asp:SqlDataSource>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Manufacturer")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="Date_Tested">
<EditItemTemplate>
<asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("Date_Tested")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDate_Tested" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("Date_Tested")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Lot ID" SortExpression="Test_Lot_ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox11" runat="server" Text='<%# Bind("Test_Lot_ID")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtTest_Lot_ID" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("Test_Lot_ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name Laboratory" SortExpression="Name_Laboratory">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource4" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("Name_Laboratory")%>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Laboratory]"></asp:SqlDataSource>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="txtName_Laboratory" runat="server" DataSourceID="SqlDataSource4" DataTextField="Name" DataValueField="Name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString2 %>" SelectCommand="SELECT [Name] FROM [Laboratory]"></asp:SqlDataSource>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("Name_Laboratory")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString %>" DeleteCommand="DELETE FROM [Certificate] WHERE [CertificateID] = #CertificateID" InsertCommand="INSERT INTO [Certificate] ([Customs_Entry], [Date_Certificate], [Invoice_Number], [Product_Number], [Product_Name], [Product_Category], [CPSC_Regulation], [Date_Manufactured], [Manufacturer], [Date_Tested], [Test_Lot_ID], [Name_Laboratory]) VALUES (#Customs_Entry, #Date_Certificate, #Invoice_Number, #Product_Number, #Product_Name, #Product_Category, #CPSC_Regulation, #Date_Manufactured, #Manufacturer, #Date_Tested, #Test_Lot_ID, #Name_Laboratory)" SelectCommand="SELECT * FROM [Certificate]" UpdateCommand="UPDATE [Certificate] SET [Customs_Entry] = #Customs_Entry, [Date_Certificate] = #Date_Certificate, [Invoice_Number] = #Invoice_Number, [Product_Number] = #Product_Number, [Product_Name] = #Product_Name, [Product_Category] = #Product_Category, [CPSC_Regulation] = #CPSC_Regulation, [Date_Manufactured] = #Date_Manufactured, [Manufacturer] = #Manufacturer, [Date_Tested] = #DatE_Tested, [Test_Lot_ID] = #Test_lot_ID, [Name_Laboratory] = #Name_Laboratory WHERE [CertificateID] = #CertificateID" FilterExpression="[CertificateID] like '{0}%' and [Customs_Entry] like '{0}%' and [Product_Number] like '{1}%' and [Product_Name] like '{2}%'">
<FilterParameters>
<asp:ControlParameter ControlID="txtCertificateID_Filter" Name="CertificateID" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="txtCustoms_Entry_Filter" Name="Customs_Entry" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="txtProduct_Number_Filter" Name="Product_Name" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="txtProduct_Name_Filter" Name="Product_Number" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
</FilterParameters>
<DeleteParameters>
<asp:Parameter Name="CertificateID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Customs_Entry" Type="String" />
<asp:Parameter Name="Date_Certificate" Type="String" />
<asp:Parameter Name="Invoice_Number" Type="String" />
<asp:Parameter Name="Product_Number" Type="String" />
<asp:Parameter Name="Product_Name" Type="String" />
<asp:Parameter Name="Product_Category" Type="String" />
<asp:Parameter Name="CPSC_Regulation" Type="String" />
<asp:Parameter Name="Date_Manufactured" Type="String" />
<asp:Parameter Name="Manufacturer" Type="String" />
<asp:Parameter Name="Date_Tested" Type="String" />
<asp:Parameter Name="Test_Lot_ID" Type="String" />
<asp:Parameter Name="Name_Laboratory" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Customs_Entry" Type="String" />
<asp:Parameter Name="Date_Certificate" Type="String" />
<asp:Parameter Name="Invoice_Number" Type="String" />
<asp:Parameter Name="Product_Number" Type="String" />
<asp:Parameter Name="Product_Name" Type="String" />
<asp:Parameter Name="Product_Category" Type="String" />
<asp:Parameter Name="CPSC_Regulation" Type="String" />
<asp:Parameter Name="Date_Manufactured" Type="String" />
<asp:Parameter Name="Manufacturer" Type="String" />
<asp:Parameter Name="Date_Tested" Type="String" />
<asp:Parameter Name="Test_Lot_ID" Type="String" />
<asp:Parameter Name="Name_Laboratory" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
Here is the correct code:
FilterExpression="Convert(CertificateID , 'System.String') LIKE '{0}%' and [Customs_Entry] like '{1}%' and [Product_Number] like '{2}%' and [Product_Name] like '{3}%'">
Try
CAST(Product_Number as varchar(10)) LIKE '{1}%'
you can change the 10 to whatever is appropriate.
EDIT
OK, then it is not getting to SQL server, have you tried
Convert(Product_Number , 'System.String') LIKE '{1}%'
Have you tried CONVERT ?
convert(varchar(length),[CertificateID]) like '{0}%'
Use this code.
"Convert(" + Your Column Name + ",'System.String')" + " LIKE '*" + Searching Text + "*'"
I am looking for some help with a add row button, I am having a problem with the sub to insert the data.
This is the error I am receiving:
Error1: Method 'Protected Sub SqlDataSource2_Inserting(sender As
Object, e As
System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs)' cannot
handle event 'Public Event Inserting(sender As Object, e As
System.Web.UI.WebControls.SqlDataSourceCommandEventArgs)' because they
do not have a compatible
signature. C:\Users\Administrator\Documents\Visual Studio
2012\Projects\Certificates\Certificates\WebForm1.aspx.vb 17 161 Certificates
html code:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="Certificates.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<asp:GridView ID="Laboratory" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="LaboratoryID" DataSourceID="SqlDataSource2" ShowFooter="True">
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="AddRow" runat="server" CommandName="Insert" Text="Add" />
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" SortExpression="Address">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Providence" SortExpression="Providence">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Providence") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtProvidence" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Providence") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="City">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtCity" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ZipCode" SortExpression="ZipCode">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("ZipCode") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtZipCode" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("ZipCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Country") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtCountry" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone" SortExpression="Phone">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Phone") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtPhone" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Phone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fax" SortExpression="Fax">
<EditItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Fax") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtFax" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("Fax") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="Email">
<EditItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtEmail" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString1 %>" DeleteCommand="DELETE FROM [Laboratory] WHERE [LaboratoryID] = #LaboratoryID" InsertCommand="INSERT INTO [Laboratory] ([Name], [Address], [Providence], [City], [ZipCode], [Country], [Phone], [Fax], [Email]) VALUES (#Name, #Address, #Providence, #City, #ZipCode, #Country, #Phone, #Fax, #Email)" SelectCommand="SELECT * FROM [Laboratory]" UpdateCommand="UPDATE [Laboratory] SET [Name] = #Name, [Address] = #Address, [Providence] = #Providence, [City] = #City, [ZipCode] = #ZipCode, [Country] = #Country, [Phone] = #Phone, [Fax] = #Fax, [Email] = #Email WHERE [LaboratoryID] = #LaboratoryID">
<DeleteParameters>
<asp:Parameter Name="LaboratoryID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="Providence" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="ZipCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Fax" Type="String" />
<asp:Parameter Name="Email" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="Providence" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="ZipCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Fax" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="LaboratoryID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
Here is my vb code:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles Laboratory.RowCommand
' Insert data if the CommandName == "Insert"
' and the validation controls indicate valid data...
If e.CommandName = "Insert" AndAlso Page.IsValid Then
' Insert new record...
SqlDataSource2.Insert()
End If
End Sub
' Sub to reference footer text cells and Insert them into new row in database
Protected Sub SqlDataSource2_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles SqlDataSource2.Inserting
' Programmatically reference Web controls in the inserting interface...
Dim NewName As TextBox = Laboratory.FooterRow.FindControl("txtName")
Dim NewAddress As TextBox = Laboratory.FooterRow.FindControl("txtAddress")
Dim NewProvidence As TextBox = Laboratory.FooterRow.FindControl("txtProvidence")
Dim NewCity As TextBox = Laboratory.FooterRow.FindControl("txtCity")
Dim NewZipCode As TextBox = Laboratory.FooterRow.FindControl("txtZipCode")
Dim NewCountry As TextBox = Laboratory.FooterRow.FindControl("txtcountry")
Dim NewPhone As TextBox = Laboratory.FooterRow.FindControl("txtPhone")
Dim NewFax As TextBox = Laboratory.FooterRow.FindControl("txtFax")
Dim NewEmail As TextBox = Laboratory.FooterRow.FindControl("txtEmail")
' Set the ObjectDataSource's InsertParameters values...
e.InputParameters("Name") = NewName.Text
e.InputParameters("Address") = NewAddress.Text
e.InputParameters("Providence") = NewProvidence.Text
e.InputParameters("City") = NewCity.Text
e.InputParameters("ZipCode") = NewZipCode.Text
e.InputParameters("Country") = NewCountry.Text
e.InputParameters("Phone") = NewPhone.Text
e.InputParameters("Fax") = NewFax.Text
e.InputParameters("Email") = NewEmail.Text
End Sub
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
End Sub
End Class
Did you check here?
http://forums.codeguru.com/showthread.php?385892-RESOLVED-Adding-rows-to-GridView-programatically
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.