DataSet update problem

A

Alastair Anderson

I have created a very simple form with which I would like to update a
single
value in a single row of a database as a proof of concept.

The relevant parts of the form are a DBWebTextBox (which is hooked
into a
column ORGANISATION.SHORT_NAME) and a Button which I am using as a
submit
button.

I have the form updating the database correctly, but only after the
submit
button is clicked twice, which is not the intended behaviour for this
application.

I placed a break point within the OnClick method to see what is
happening, however the first time I click the button the event handler
is
never run, but I also have a DBWebNavigator control on my form and
that seems
to update correctly (allowing the Undo of the update) on the first
click, but
only on the second click does the event handler get run.

The event handler code is as follows...

private void btnSubmit_Click(object sender, System.EventArgs e)
{
int rowsUpdated;
bdpDataAdapter1.UpdateCommand.CommandText =
"UPDATE ORGANISATION SET SHORT_NAME = '" +
dbWebTextBox2.Text +
"' WHERE NAME = '" +
dbWebTextBox1.Text +
"'";

rowsUpdated = bdpDataAdapter1.Update(dataSet1);
bdpConnection1.Open();
bdpUpdateCommand1.ExecuteNonQuery();
bdpConnection1.Close();
dataSet1.AcceptChanges();
}

the only other event handler that I have is the Page_Load method,
however that does not have any code within it.

I was just wondering if I am missing anything in the order of
execution

Many thanks in advance
 
W

W.G. Ryan MVP

alistair, do you have parameters or column mappings for your update
statement? Also, wtha's the purpose of calling Update and then
executenonquery? Finally, if your update works correctly, acceptchanges
gets called on each row as it's updated so that's not necessary.

Very that your dataset HasChanges() first to make sure that there's
something to update, then we'll work on the update command.

BTW, paramaterize that query regardless - there's all downside to not using
parameters.
 
D

Dave

I placed a break point within the OnClick method to see what is
happening, however the first time I click the button the event handler
is never run

It doesn't sound like a DB issue to me. It sounds like a post-back issue (i'm assuming this is a web form).

Is "Button" a custom type or a System.Web.UI.WebControls.Button?

Either way, the "click" event should be hooked up in the OnLoad method.
 
A

Alastair Anderson

Thanks for your input William,

W.G. Ryan MVP said:
alistair, do you have parameters or column mappings for your update
statement?

I do not have any parameters or column mappings
for the update, the piece of code that is listed is not going to be
the final
code in terms of SQL, as I understand the importance of parameterising
it.
Also, wtha's the purpose of calling Update and then
executenonquery?

I am totally new to this side of database development, I have used the
client dataset and data provider methods that are used within Delphi
and C# data modules, so my thinking at the time of posting this could
be wrong, but re-reading the code it looks like it is doing the same
thing as I have set the UpdateCommand of the DataAdapter to be the
bdpUpdateCommand, and I am guessing that calling Update and
ExecuteNonQuery do the same thing?
Finally, if your update works correctly, acceptchanges
gets called on each row as it's updated so that's not necessary.

Very that your dataset HasChanges() first to make sure that there's
something to update, then we'll work on the update command.

Again similar to the parameterised query issue, I realise the
inefficiency in this and will optimise it once I get the messy version
working :), but other than my initial misunderstanding of
ExecuteNonQuery and Update, would these affect the need to click the
button twice for submitting data, as would these not be executed no
matter how inefficent the logic is?
 
A

Alastair Anderson

Hi Dave,
The button is of type System.Web.UI.WebControls.Button and I have now
changed the OnClick event to be "hooked" up within the Page_Load
method and the behaviour is still the same, two clicks before the
OnClick event is even fired.

Which I agree sounds like a post back issue but I am still stumped as
to what.
 
D

Dave

That does sound strange, but nothing that I haven't experienced :)

It's tough for me to say what the issue is without seeing the code, but I can suggest a couple of possiblities:

1. Make sure that you are checking that Page.IsPostBack returns false when initializing variables to their default values. Not
checking this property is a common mistake which can lead to bugs like yours. You may be reinitializing your variables every time
the page loads (even upon a postback) which can cause the "state" of your app to be invalid. This differs from initialization which
MUST occur prior to the Page.Load event, for instance in a DataGrid Control.

2. All Control-fired event handlers must be hooked-up on, or before the Page.Load event occurs otherwise they will not be invoked by
the Page or it's Controls.

3. Make sure that all Controls (added without the aid of the designer) are part of the Page.Controls collection on, or before the
Page.Init event.


GL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top