Hi all,
what i'm trying to do here is when i open a page, it goes to a db and pull some info and display it in a textbox, then if i edit that text box, and hit save, it gets updated it in database.
I'm establishing a connection to the db under protected void Page_Load (object sender, EventArgs e) and it is getting the value correctly, below is the code for that.
[code=cpp]
SqlConnection conn;
conn = new SqlConnection(" Data Source=SQLServe r;Initial Catalog=I3_IC;P ersist Security Info=True;User ID=username;Pas sword=password" );
SqlDataReader rdr = null;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("sel ect Updateable_Mess age from UpdateableMessa ge", conn);
rdr = cmd.ExecuteRead er();
while (rdr.Read())
{
TextBox.Text = rdr[0].ToString();
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}
// Close the connection
if (conn != null)
{
conn.Close();
}
}[/code]
Below is my code for the save button
[code=cpp]
protected void ButtonSave_Clic k(object sender, EventArgs e)
{
SqlDataSource dashDataSource = new SqlDataSource() ;
dashDataSource. ConnectionStrin g = ConfigurationMa nager.Connectio nStrings["CRMConnectionS tring1"].ToString();
dashDataSource. InsertCommandTy pe = SqlDataSourceCo mmandType.Text;
dashDataSource. InsertCommand = "Update UpdateableMessa ge set Updateable_Mess age = @strUpdateMessa ge";
dashDataSource. InsertParameter s.Add("strUpdat eMessage", TextBox.Text);
int rowsAffected = 0;
try
{
rowsAffected = dashDataSource. Insert();
}
catch (Exception ex)
{
Server.Transfer ("Problem.aspx" );
}
finally
{
dashDataSource = null;
}
if (rowsAffected != 1)
{
//Server.Transfer ("Problem.aspx" );
LabelResult.Tex t = "The Hot Topic Message hasn't been updated successfully";
}
else
{
LabelResult.Tex t = "The Hot Topic Message has been updated successfully";
TextBox.Text = "";
// Server.Transfer ("AddRecords.as px");
}
}
[/code]
Now, if i hit save, it doesn't update!
if i removed the code from the Page_load, and when i first load the page, i don't see what is in the db Obviously and if i hit save the data in the text file gets saved successfully.
it is like either or :(
I'm sure there is way around it, but how?
any feedback is appriciated.
thanks in advance.
what i'm trying to do here is when i open a page, it goes to a db and pull some info and display it in a textbox, then if i edit that text box, and hit save, it gets updated it in database.
I'm establishing a connection to the db under protected void Page_Load (object sender, EventArgs e) and it is getting the value correctly, below is the code for that.
[code=cpp]
SqlConnection conn;
conn = new SqlConnection(" Data Source=SQLServe r;Initial Catalog=I3_IC;P ersist Security Info=True;User ID=username;Pas sword=password" );
SqlDataReader rdr = null;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("sel ect Updateable_Mess age from UpdateableMessa ge", conn);
rdr = cmd.ExecuteRead er();
while (rdr.Read())
{
TextBox.Text = rdr[0].ToString();
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}
// Close the connection
if (conn != null)
{
conn.Close();
}
}[/code]
Below is my code for the save button
[code=cpp]
protected void ButtonSave_Clic k(object sender, EventArgs e)
{
SqlDataSource dashDataSource = new SqlDataSource() ;
dashDataSource. ConnectionStrin g = ConfigurationMa nager.Connectio nStrings["CRMConnectionS tring1"].ToString();
dashDataSource. InsertCommandTy pe = SqlDataSourceCo mmandType.Text;
dashDataSource. InsertCommand = "Update UpdateableMessa ge set Updateable_Mess age = @strUpdateMessa ge";
dashDataSource. InsertParameter s.Add("strUpdat eMessage", TextBox.Text);
int rowsAffected = 0;
try
{
rowsAffected = dashDataSource. Insert();
}
catch (Exception ex)
{
Server.Transfer ("Problem.aspx" );
}
finally
{
dashDataSource = null;
}
if (rowsAffected != 1)
{
//Server.Transfer ("Problem.aspx" );
LabelResult.Tex t = "The Hot Topic Message hasn't been updated successfully";
}
else
{
LabelResult.Tex t = "The Hot Topic Message has been updated successfully";
TextBox.Text = "";
// Server.Transfer ("AddRecords.as px");
}
}
[/code]
Now, if i hit save, it doesn't update!
if i removed the code from the Page_load, and when i first load the page, i don't see what is in the db Obviously and if i hit save the data in the text file gets saved successfully.
it is like either or :(
I'm sure there is way around it, but how?
any feedback is appriciated.
thanks in advance.
Comment