Brian
asked on
Trouble Updating Data
Hell Experts,
I have the following code below. I tested all Stored Procedures and they all execute fine. Just trouble with the code (I believe it has something to do with PostBack(s).
What happens is that every time I try to fire the onclick="btnUpdateCourseDe scription_ Click" code the page refreshes itself and post the data that I try to edit back to it's original copy.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RetrieveCoursesValues();
}
RetrieveCoursesDesc();
}
protected void RetrieveCoursesValues()
{
using (SqlConnection conn = new SqlConnection(Configuratio nManager.C onnectionS trings["He althCourse s"].Connec tionString ))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "HealthCourses_RetrieveCou rsesValues ";
cmd.CommandType = CommandType.StoredProcedur e;
cmd.Connection = conn;
DataTable dtCoursesValues = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
try
{
conn.Open();
adp.SelectCommand = cmd;
adp.Fill(dtCoursesValues);
ddlCourseUpdate.DataSource = dtCoursesValues;
ddlCourseUpdate.DataValueF ield = "ghaoc_id";
ddlCourseUpdate.DataTextFi eld = "ghaco_name";
ddlCourseUpdate.DataBind() ;
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
conn.Close();
}
}
}
protected void RetrieveCoursesDesc()
{
using (SqlConnection conn = new SqlConnection(Configuratio nManager.C onnectionS trings["He althCourse s"].Connec tionString ))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "HealthCourses_RetrieveCou rsesDescBy ID";
cmd.CommandType = CommandType.StoredProcedur e;
cmd.Connection = conn;
cmd.Parameters.AddWithValu e("@ghaoc_ id", SqlDbType.Int).Value = ddlCourseUpdate.SelectedVa lue;
DataTable dtCoursesDesc = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
try
{
conn.Open();
adp.SelectCommand = cmd;
adp.Fill(dtCoursesDesc);
if ((dtCoursesDesc != null))
{
DataRow data = dtCoursesDesc.Rows[0];
TextBox1.Text = data["ghaco_desc"].ToStrin g();
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
conn.Close();
}
}
}
protected void btnUpdateCourseDescription _Click(obj ect sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(Configuratio nManager.C onnectionS trings["He althCourse s"].Connec tionString ))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "HealthCourses_UpdateGener alHealthAw arenessOne Courses";
cmd.CommandType = CommandType.StoredProcedur e;
cmd.Connection = conn;
cmd.Parameters.AddWithValu e("@ghaoc_ id", SqlDbType.Int).Value = hf_ghaoc_id.Value;
cmd.Parameters.AddWithValu e("@ghaco_ desc", SqlDbType.VarChar).Value = TextBox1.Text;
try
{
conn.Open();
cmd.ExecuteNonQuery();
Server.Transfer("courses_u pdate_succ ess.aspx") ;
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
conn.Close();
}
}
}
I have the following code below. I tested all Stored Procedures and they all execute fine. Just trouble with the code (I believe it has something to do with PostBack(s).
What happens is that every time I try to fire the onclick="btnUpdateCourseDe
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RetrieveCoursesValues();
}
RetrieveCoursesDesc();
}
protected void RetrieveCoursesValues()
{
using (SqlConnection conn = new SqlConnection(Configuratio
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "HealthCourses_RetrieveCou
cmd.CommandType = CommandType.StoredProcedur
cmd.Connection = conn;
DataTable dtCoursesValues = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
try
{
conn.Open();
adp.SelectCommand = cmd;
adp.Fill(dtCoursesValues);
ddlCourseUpdate.DataSource
ddlCourseUpdate.DataValueF
ddlCourseUpdate.DataTextFi
ddlCourseUpdate.DataBind()
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
conn.Close();
}
}
}
protected void RetrieveCoursesDesc()
{
using (SqlConnection conn = new SqlConnection(Configuratio
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "HealthCourses_RetrieveCou
cmd.CommandType = CommandType.StoredProcedur
cmd.Connection = conn;
cmd.Parameters.AddWithValu
DataTable dtCoursesDesc = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
try
{
conn.Open();
adp.SelectCommand = cmd;
adp.Fill(dtCoursesDesc);
if ((dtCoursesDesc != null))
{
DataRow data = dtCoursesDesc.Rows[0];
TextBox1.Text = data["ghaco_desc"].ToStrin
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
conn.Close();
}
}
}
protected void btnUpdateCourseDescription
{
using (SqlConnection conn = new SqlConnection(Configuratio
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "HealthCourses_UpdateGener
cmd.CommandType = CommandType.StoredProcedur
cmd.Connection = conn;
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
try
{
conn.Open();
cmd.ExecuteNonQuery();
Server.Transfer("courses_u
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
conn.Close();
}
}
}
ASKER
Hi sonawanekiran,
Not sure what you mean. Server.Transfer("courses_u pdate_succ ess.aspx") ; is used to send user to a Success page letting them know that they updated the data fine. I don't even see that page though.
Not sure what you mean. Server.Transfer("courses_u
ASKER
Also, I just put a breakpoint on line below and noticed the value was "". Could this be why it cannot update? If so, how can I fix that?
cmd.Parameters.AddWithValu e("@ghaoc_ id", SqlDbType.Int).Value = hf_ghaoc_id.Value;
cmd.Parameters.AddWithValu
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes hf_ghaoc_id is a hidden field.
>> Check why not value is assigned to hidden field hf_ghaoc_id
I'm not sure why it's not set. Can you assist me in setting that value to page_load? That may be the problem because the Update is depending on that value ghaoc_id.
>> Check why not value is assigned to hidden field hf_ghaoc_id
I'm not sure why it's not set. Can you assist me in setting that value to page_load? That may be the problem because the Update is depending on that value ghaoc_id.
ASKER
I figured it out using a different method.
Server.Transfer("courses_u
Instead call below method
RetrieveCoursesDesc();
OR
RetrieveCoursesValues();