Using Visual Studio 2012 and SQL Server 2012
Here is the situation:
A website I am building is not updating data that is loaded into fields from a SQLDataSource. The data loads just fine into their appropriate text boxes, but when I go to edit the data and activate a button click, it looks like the revisions are cleared and the page refreshes to the original loaded data. As far as I can tell, my code looks correct, but that is obviously not the case.
Addition info:
I have the page set up in a containerplaceholder with a master page.
Here is my mark up:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<!-- Job Title -->
<div class="lbljobtitle">
<asp:Label ID="lblJobTitle" runat="server" Text="Job Title"></asp:Label>
</div>
<div class="txtjobtitle">
<asp:TextBox ID="txtJobTitle" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>
<!-- Employee Type -->
<div class="lblemployeetype">
<asp:Label ID="lblEmployeeType" runat="server" Text="Employee Type"></asp:Label>
</div>
<div class="txtemployeetype">
<asp:TextBox ID="txtEmployeeType" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>
<!-- Location -->
<div class="lbllocation">
<asp:Label ID="lblLocation" runat="server" Text="Location"></asp:Label>
</div>
<div class="txtlocation">
<asp:TextBox ID="txtLocation" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>
<!-- Job Description -->
<div class="lbljobdescription">
<asp:Label ID="lblJobDescription" runat="server" Text="Job Description"></asp:Label>
</div>
<div class="txtjobdescription">
<asp:TextBox ID="txtJobDescription" runat="server" Height="160px" Width="250px" TextMode="MultiLine"></asp:TextBox>
</div>
<!-- Requirements -->
<div class="lblrequirements">
<asp:Label ID="lblRequirements" runat="server" Text="Requirements"></asp:Label>
</div>
<div class="txtrequirements">
<asp:TextBox ID="txtRequirements" runat="server" Height="160px" Width="250px" TextMode="MultiLine"></asp:TextBox>
</div>
<!-- Experience -->
<div class="lblexperience">
<asp:Label ID="lblExperience" runat="server" Text="Experience"></asp:Label>
</div>
<div class="txtexperience">
<asp:TextBox ID="txtExperience" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>
<!-- Date Posted -->
<div class="lbldateposted">
<asp:Label ID="lblDatePosted" runat="server" Text="Date Posted"></asp:Label>
</div>
<div class="txtdateposted">
<asp:TextBox ID="txtDatePosted" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>
<!-- Start Date -->
<div class="lblstartdate">
<asp:Label ID="lblStartDate" runat="server" Text="Start Date"></asp:Label>
</div>
<div class="txtstartdate">
<asp:TextBox ID="txtStartDate" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>
<asp:Button ID="btnUpdate" runat="server" Text="Update Job" OnClick="btnUpdate_Click" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:bgrecruit_d01ConnectionString %>"
SelectCommand="SELECT job_title, employee_type, location, job_description, requirements, experience, date_posted, start_date, requisition_id FROM jobopening_tbl WHERE (requisition_id = @requisition_id)"
UpdateCommand="UPDATE [jobopening_tbl] SET [job_title] = @job_title, [employee_type] = @employee_type, [location] = @location, [job_description] = @job_description, [requirements] = @requirements, [experience] = @experience, [date_posted] = @date_posted, [start_date] = @start_date WHERE [requisition_id] = @requisition_id">
<SelectParameters>
<asp:QueryStringParameter Name="requisition_id" Type="String" QueryStringField="req_id" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="job_title" Type="String" />
<asp:Parameter Name="employee_type" Type="String" />
<asp:Parameter Name="location" Type="String" />
<asp:Parameter Name="job_description" Type="String" />
<asp:Parameter Name="requirements" Type="String" />
<asp:Parameter Name="experience" Type="String" />
<asp:Parameter Name="date_posted" Type="String" />
<asp:Parameter Name="start_date" Type="String" />
<asp:Parameter Name="requisition_id" />
</UpdateParameters>
</asp:SqlDataSource>
And my C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class UpdateDetail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataRowView row = dv[0];
txtJobTitle.Text = row["job_title"].ToString();
txtEmployeeType.Text = row["employee_type"].ToString();
txtLocation.Text = row["location"].ToString();
txtJobDescription.Text = row["job_description"].ToString();
txtRequirements.Text = row["requirements"].ToString();
txtExperience.Text = row["experience"].ToString();
txtDatePosted.Text = row["date_posted"].ToString();
txtStartDate.Text = row["start_date"].ToString();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataRowView row = dv[0];
SqlDataSource1.UpdateParameters["job_title"].DefaultValue = txtJobTitle.Text.ToString();
SqlDataSource1.UpdateParameters["employee_type"].DefaultValue = txtEmployeeType.Text.ToString();
SqlDataSource1.UpdateParameters["location"].DefaultValue = txtLocation.Text.ToString();
SqlDataSource1.UpdateParameters["job_description"].DefaultValue = txtJobDescription.Text.ToString();
SqlDataSource1.UpdateParameters["requirements"].DefaultValue = txtRequirements.Text.ToString();
SqlDataSource1.UpdateParameters["experience"].DefaultValue = txtExperience.Text;
SqlDataSource1.UpdateParameters["date_posted"].DefaultValue = txtDatePosted.Text;
SqlDataSource1.UpdateParameters["start_date"].DefaultValue = txtStartDate.Text;
//requisition_id = @requisition_id
SqlDataSource1.UpdateParameters["requisition_id"].DefaultValue = row["requisition_id"].ToString();
SqlDataSource1.Update();
}
}
Add the following
ifblock so the code in your page_load looks like this:The problem is that page_load runs on every post back (including button clicks). I suspect you only want to run that code when the page originally loads which is what adding the
ifdoes.Remember this, because you will run into this issue approximately 28972 more times if you keep programming with .NET.