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!

Odd Data overwrite issue 1

Status
Not open for further replies.

faust13

Programmer
Aug 7, 2001
176
0
0
US
I have an edit page, where the page load runs a query and I set the values of the text field from the query. The user then hits the update button and I call a SP that updates the record. Sounds easy enough, but for some strange reason, whatever the user enters in the textboxes, it is overwritten by the old data.

Ideas? Theories?


Thanks,

faust13
Because Han Shoots First
 
If you update is working correctly (check the DB) then you have to refresh the page and load it again to show the updated info.
 
I'm doing a trace right before the SP call, and it text values are still for the old values, not the user entered values.

So the SP is getting passed the old values.


Thanks,

faust13
Because Han Shoots First
 
Here's my code:

protected void Page_Load(object sender, EventArgs e)
{
string connStr = WebConfigurationManager.ConnectionStrings["OJCConnectionString"].ConnectionString;

SqlConnection cn = new SqlConnection(connStr);

string sql = "SELECT * FROM j3_incoming_unit INNER JOIN taskforce_dfn ON taskforce_dfn.ID = j3_incoming_unit.tf_assignment WHERE unit_ID = " + Request.QueryString["unit_ID"].ToString() + ";";

SqlCommand cmd = new SqlCommand(sql, cn);

cn.Open();

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
string state = reader["unit_state"].ToString();
string tf_name = reader["name"].ToString();
string tf_value = reader["tf_assignment"].ToString();

txtUnitName.Text = reader["unit_name"].ToString();
ddlState.Items.Add(state);
txtProjPax.Text = reader["projected_pax"].ToString();
txtActualPax.Text = reader["actual_pax"].ToString();
ddlBranch.Items.Add(reader["branch"].ToString());

ddlTF.Items.Add(new ListItem(tf_name, tf_value));

txtArrivalDate.Text = reader["arrival_date"].ToString();
txtDepartureDate.Text = reader["depature_date"].ToString();
}

reader.Close();

cn.Close();
}

protected void btnSubmit_Click(object sender, EventArgs e)
{

String connStr = WebConfigurationManager.ConnectionStrings["OJCConnectionString"].ConnectionString;

SqlConnection cn = new SqlConnection(connStr);

Trace.Warn("Updating incoming unit");
Trace.Warn("PRE UPDATE The new unit name is: " + txtUnitName.Text.ToString());

using (cn)
{
SqlCommand cmd = new SqlCommand("incomingUnitUpdate", cn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@unit_name", SqlDbType.VarChar).Value = txtUnitName.Text.ToString();

Trace.Warn("The new unit name is: " + txtUnitName.Text.ToString());

cmd.Parameters.Add("@unit_state", SqlDbType.Char).Value = ddlState.SelectedValue.ToString();
cmd.Parameters.Add("@projected_pax", SqlDbType.Int).Value = int.Parse(txtProjPax.Text.ToString());

if (txtActualPax.Text.Length != 0)
{
cmd.Parameters.Add("@actual_pax", SqlDbType.Int).Value = int.Parse(txtActualPax.Text.ToString());
}
else
{
cmd.Parameters.Add("@actual_pax", SqlDbType.Int).Value = DBNull.Value;
}

cmd.Parameters.Add("@branch", SqlDbType.VarChar).Value = ddlBranch.SelectedValue.ToString();
cmd.Parameters.Add("@arrival_date", SqlDbType.DateTime).Value = txtArrivalDate.Text.ToString();
cmd.Parameters.Add("@depature_date", SqlDbType.DateTime).Value = txtDepartureDate.Text.ToString();

cmd.Parameters.Add("@tf_assignment", SqlDbType.VarChar).Value = ddlTF.SelectedValue.ToString();
cmd.Parameters.Add("@modified_by", SqlDbType.VarChar).Value = Profile.UserName.ToString();
cmd.Parameters.Add("@unit_ID", SqlDbType.Int).Value = int.Parse(Request.QueryString["unit_ID"].ToString());

cn.Open();

int ret = cmd.ExecuteNonQuery();

Trace.Warn("The unit_ID Is: " + cmd.Parameters["@unit_ID"].Value.ToString());

Server.Transfer("incoming_units.aspx");
}
}


Thanks,

faust13
Because Han Shoots First
 
It looks like whatever value I set for the textboxes.text properties during the page load is what is being preserved on the submit.

I'm new to ASP.Net, is there a better way to do this?


Thanks,

faust13
Because Han Shoots First
 
The Page_Load fires everytime the page is submitted. Proctect your initial page loading code with IsPostback.

I'm a VB guy... so in VB it would be.

Private Sub Page_Load
If Not IsPostBack Then
'Load your controls
End if
End Sub

Senior Software Developer
 
AH! You're exactly right. I added:

if (!Page.IsPostBack)

and it works perfectly.

I remember even being warned about this. Thanks for the help.


Thanks,

faust13
Because Han Shoots First
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top