Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem updating database from DetailsView

Status
Not open for further replies.

jmgrin

MIS
Jan 4, 2002
32
0
0
US
I'm new to Asp.net and need some help.
I am using a DetailsView and the update statement of a datasource to update a database table. When I use the command field edit button that is provided by setting "show edit button" to true, the database is updated properly. However, I converted the command field to a template field so that I had more control over the button click. Now the parameters in the datasource's update statement are not being automatically filled and my database is being updated with nulls. Can I get the parameters to fill automatically or do I need to do that manually, and if so, how do I do it.

Any help would be greatly appreciated.
 
I forgot to post my code. Here it is...


<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateRows="False" CellPadding="4" ForeColor="#333333" GridLines="None" DefaultMode="Edit" Height="224px">
<Fields>
<asp:TemplateField HeaderText="First Name:" SortExpression="FIRSTNAME">
<EditItemTemplate>
<asp:TextBox ID="txtEditFirstName" runat="server"
Text='<%# Bind("FIRSTNAME") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="FirstNameRequiredValidator" runat="server" ControlToValidate="txtEditFirstName"
ErrorMessage="First name cannot be blank." ForeColor="Black" SetFocusOnError="True">**</asp:RequiredFieldValidator>

</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtnsertFirstName" runat="server" Text='<%# Bind("FIRSTNAME") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("FIRSTNAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middle Initial:" SortExpression="MIDINIT">
<EditItemTemplate>
<asp:TextBox ID="txtEditMI" runat="server" Text='<%# Bind("MIDINIT") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtInsertMI" runat="server" Text='<%# Bind("MIDINIT") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblMI" runat="server" Text='<%# Bind("MIDINIT") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name:" SortExpression="LASTNAME">
<EditItemTemplate>
<asp:TextBox ID="txtEditLastName" runat="server" Text='<%# Bind("LASTNAME") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="LastnameRequiredValidator" runat="server" ControlToValidate="txtEditLastName"
ErrorMessage="Last name cannot be blank" ForeColor="Black">**</asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtInsertFirstName" runat="server"
Text='<%# Bind("LASTNAME") %>'></asp:TextBox>

</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblLastname" runat="server" Text='<%# Bind("LASTNAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Birth Date:" SortExpression="BIRTHDATE">
<EditItemTemplate>
<asp:TextBox ID="txtEditBirthdate" runat="server" Text='<%# Bind("BIRTHDATE", "{0:d}") %>'></asp:TextBox>
<asp:CompareValidator ID="BirthdateCompareValidator" runat="server" ControlToValidate="txtEditBirthdate"
ErrorMessage="Birthdate must be a valid date in the format: mm/dd/yyyy" ForeColor="Black"
Operator="DataTypeCheck" Type="Date">**</asp:CompareValidator>

</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtInsertBirthdate" runat="server" Text='<%# Bind("BIRTHDATE", "{0:d}") %>'></asp:TextBox>

</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblBirthdate" runat="server" Text='<%# Bind("BIRTHDATE", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Date:" SortExpression="TESTDATE">
<EditItemTemplate>
<asp:TextBox ID="txtEditTestDate" runat="server" Text='<%# Bind("TESTDATE", "{0:d}") %>'></asp:TextBox>
<asp:CompareValidator ID="TestdateCompareValidator" runat="server" ControlToValidate="txtEditTestDate"
ErrorMessage="Test date must be a valid date in the format: mm/dd/yyyy" ForeColor="Black"
Operator="DataTypeCheck" Type="Date">**</asp:CompareValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtInsertTestDate" runat="server" Text='<%# Bind("TESTDATE", "{0:d") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTestdate" runat="server" Text='<%# Bind("TESTDATE", "{0:d") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Location:" SortExpression="TESTLOC">
<EditItemTemplate>
<asp:TextBox ID="txtEditTestLoc" runat="server" Text='<%# Bind("TESTLOC") %>'></asp:TextBox>
<asp:RangeValidator ID="TestsiteRangeValidator" runat="server" ControlToValidate="txtEditTestLoc"
ErrorMessage="Test location must be a campus code A-Z" ForeColor="Black" MaximumValue="Z"
SetFocusOnError="True">**</asp:RangeValidator>
</EditItemTemplate>
<InsertItemTemplate>
&nbsp;<asp:TextBox ID="txtInsertTestLoc" runat="server" Text='<%# Bind("TESTLOC") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTestLoc" runat="server" Text='<%# Bind("TESTLOC") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Campus Attending:" SortExpression="ATTEND">
<EditItemTemplate>
<asp:TextBox ID="txtEditCampus" runat="server" Text='<%# Bind("ATTEND") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtInsertCampus" runat="server" Text='<%# Bind("ATTEND") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCampus" runat="server" Text='<%# Bind("ATTEND") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Scores:">
<ItemTemplate>
<asp:GridView ID="ScoresGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="CODENO" DataSourceID="ScoresDataSource"
OnRowDeleting = "ScoresGridView_Row_Deleting"
OnRowUpdating = "ScoresGridview_Row_Updating"
ShowFooter="True" CellPadding="4" ForeColor="#333333" GridLines="None" ShowHeader="False" >
<Columns>
<asp:TemplateField SortExpression="CNAME">
<EditItemTemplate>
<asp:Label ID="lblScoreCode" runat="server" Text='<%# Bind("CNAME") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblScoreCode" runat="server" Text='<%# Bind("CNAME") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlScoreCode" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="24">ENGL</asp:ListItem>
<asp:ListItem Value="39">READ</asp:ListItem>
<asp:ListItem Value="76">MBSC</asp:ListItem>
<asp:ListItem Value="77">ALG</asp:ListItem>
<asp:ListItem Value="78">TRG</asp:ListItem>
<asp:ListItem Value="43">FREN</asp:ListItem>
<asp:ListItem Value="41">FMECH</asp:ListItem>
<asp:ListItem Value="42">FREAD</asp:ListItem>
<asp:ListItem Value="47">GERM</asp:ListItem>
<asp:ListItem Value="45">GMECH</asp:ListItem>
<asp:ListItem Value="46">GREAD</asp:ListItem>
<asp:ListItem Value="51">SPAN</asp:ListItem>
<asp:ListItem Value="49">SMECH</asp:ListItem>
<asp:ListItem Value="50">SREAD</asp:ListItem>
<asp:ListItem Value="9">LATIN</asp:ListItem>
<asp:ListItem Value="94">W-ENG</asp:ListItem>
<asp:ListItem Value="96">W-MBS</asp:ListItem>
<asp:ListItem Value="97">W-ALG</asp:ListItem>
<asp:ListItem Value="98">W-TRG</asp:ListItem>
<asp:ListItem Value="91">W-FRE</asp:ListItem>
<asp:ListItem Value="92">W-GER</asp:ListItem>
<asp:ListItem Value="93">W-SPA</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ddlScoreCode"
ErrorMessage="Scorecode cannot be blank." ForeColor="Black" SetFocusOnError="True"
ValidationGroup="ScoreValidation">**</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="SCORE">
<EditItemTemplate>
<asp:TextBox ID="txtScore" runat="server" Text='<%# Bind("SCORE") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblScore" runat="server" Text='<%# Bind("SCORE") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddScore" runat="server" Width="48px"> </asp:TextBox>
<asp:RangeValidator ID="ScoreRangeValidator" runat="server" ControlToValidate="txtAddScore"
ErrorMessage="Score must be greater than 149 and less than 851." ForeColor="Black" MaximumValue="850" MinimumValue="150"
SetFocusOnError="True" ValidationGroup="ScoreValidation">**</asp:RangeValidator>
<asp:RequiredFieldValidator ID="ScoreRequiredFieldValidator" runat="server" ControlToValidate="txtAddScore"
ErrorMessage="Please enter a score." ForeColor="Black" SetFocusOnError="True"
ValidationGroup="ScoreValidation">**</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="CNAME1">

<ItemTemplate>

</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add Score" OnClick="btnAdd_Click" ValidationGroup="ScoreValidation" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="CODENO" Visible="False">
<EditItemTemplate>
<asp:TextBox ID="CodeNo" runat="server" Text='<%# Bind("CODENO") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCodeNo" runat="server" Text='<%# Bind("CODENO") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:CommandField
ButtonType = "Button"
ShowEditButton = "True"
ShowDeleteButton = "True"
/>

</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
&nbsp;
<asp:SqlDataSource ID="ScoresDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:dbTNEplsConnectionString %>"
SelectCommand="SELECT jmgrinva.code.CNAME, jmgrinva.score.SCORE, jmgrinva.score.codeno FROM jmgrinva.code INNER JOIN jmgrinva.score ON jmgrinva.code.CODENO = jmgrinva.score.CODENO WHERE (jmgrinva.score.TestID = @TESTID) ORDER BY jmgrinva.code.CTYPE"
DeleteCommand="DELETE FROM jmgrinva.score WHERE (TestID = @TESTID) AND (CODENO = @CODENO)"
InsertCommand="INSERT INTO jmgrinva.score(TestID, CODENO, SCORE) VALUES (@TESTID,@CODENO,@SCORE)"
UpdateCommand="UPDATE jmgrinva.score SET SCORE = @SCORE WHERE (TestID = @TESTID) AND (CODENO = @CODENO)">
<SelectParameters>
<asp:ControlParameter ControlID="txtTestid" Name="TESTID" PropertyName="Text" />
</SelectParameters>
<DeleteParameters>
<asp:ControlParameter ControlID="txtTestid" Name="TESTID" PropertyName="Text" />
<asp:parameter Name="CODENO" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:ControlParameter ControlID="txtTestid" Name="TESTID" PropertyName="Text" />
<asp:parameter Name="CODENO" Type="Int32" />
<asp:parameter Name="SCORE" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:ControlParameter ControlID="txtTestid" Name="TESTID" PropertyName="Text" />
<asp:parameter Name="SCORE" />
<asp:parameter Name="CODENO" />
</UpdateParameters>
</asp:SqlDataSource>
&nbsp;
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" CausesValidation="True" CommandName="Update"
OnClick="btnUpdate_Click" OnClientClick="return confirm('Are you sure you want to update this record?')" Text="Update Examinee Data" />
<asp:Button ID="btnDeleteData" runat="server" OnClick="btnDeleteData_Click" OnClientClick="return confirm('Are you sure you want to remove this examinee and scores from PLS?')" Text="Delete Data" />
<asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" OnClientClick="return confirm('Are you sure you want to exit with saving changes?')" Text="Cancel" />
</EditItemTemplate>

</asp:TemplateField>

</Fields>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<EditRowStyle BackColor="#2461BF" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<HeaderStyle BackColor="#507CD1" Font-Size="Larger" Font-Bold="True" ForeColor="White" />
<HeaderTemplate>
Update Placement Information
</HeaderTemplate>
<AlternatingRowStyle BackColor="White" />

</asp:DetailsView>
&nbsp;&nbsp;
<br />
<asp:Button ID="btnDelete" runat="server" Text="Delete Examinee and Score Data" onclick = "btnDelete_Click" OnClientClick="return confirm('Are you sure you want to delete this record?');"/>
<asp:Button ID="btnReturn" runat="server" Text="Cancel" Onclick="btnReturn_click" onclientclick = "return confirm ('Are you sure you want to exit without saving changes?')"/>
<asp:ValidationSummary ID="ExamValidationSummary" runat="server" ForeColor="Black"
HeaderText="The following errors were found:" ShowMessageBox="True" ShowSummary="False" />
<asp:ValidationSummary ID="ScoreValidationSummary" runat="server" ForeColor="Black"
ShowMessageBox="True" ShowSummary="False" ValidationGroup="ScoreValidation" />
<br />
&nbsp;&nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbTNEplsConnectionString %>"
SelectCommand="SELECT [FIRSTNAME], [MIDINIT], [LASTNAME], [TESTDATE], [BIRTHDATE] , [TESTLOC], [ATTEND] FROM [jmgrinva]. [exam] WHERE ([TESTID] = @TESTID)"
DeleteCommand="DELETE FROM jmgrinva.exam WHERE (TESTID = @TESTID)"
UpdateCommand="UPDATE jmgrinva.exam SET LASTNAME = @LASTNAME, FIRSTNAME = @FIRSTNAME, MIDINIT = @MIDINIT, TESTDATE = @TESTDATE, BIRTHDATE = @BIRTHDATE, ATTEND = @ATTEND, TESTLOC = @TESTLOC where testid = @TESTID">
<SelectParameters>
<asp:ControlParameter ControlID="txtTestid" Name="TESTID" PropertyName="Text" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:ControlParameter ControlID="txtTestid" Name="TESTID" PropertyName="Text" />
<asp:parameter Name="TESTID" />
</DeleteParameters>
<UpdateParameters>
<asp:ControlParameter ControlID="txtTestid" Name="TESTID" PropertyName="Text" />
<asp:parameter Name="LASTNAME" />
<asp:parameter Name="FIRSTNAME" />
<asp:parameter Name="MIDINIT" />
<asp:parameter Name="TESTDATE" />
<asp:parameter Name="BIRTHDATE" />
<asp:parameter Name="ATTEND" />
<asp:parameter Name="TESTLOC" />

</UpdateParameters>
</asp:SqlDataSource>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top