InsertCommand and Parameters

N

newsgroups1

Hi All,

Strange problem, but I didn't find a solution:

I created a C# Form. Because it's some easy stuff to handle here, I
used the drag-n-drop-Function to create DataConnection, DataSet &
DataAdapter.
So far, so well.

Update and Delete functions work fine. BUT the Insert function won't
do it's work as expected.

Here's a code-snippet:

<snip>
private void Broadcast_Load(object sender, System.EventArgs e)
{
if(Broadcast_Key > 0)
{
button11.Text = "Update";
daBCast.SelectCommand.Parameters["@Broadcast_Key"].Value =
Broadcast_Key;
daBCast.Fill(dsBcast1);
}
cm1 = (CurrencyManager) this.BindingContext[dsBcast1, "Broadcast"];
if(Broadcast_Key < 0)
{
button11.Text = "Insert";
cm1.AddNew();
}
}

private void clickSave(object sender, System.EventArgs e)
{
this.daBCast.InsertCommand.Parameters["@FK_Client_Key"].Value =
Client_Key;
cm1.EndCurrentEdit();

if(dsBcast1.HasChanges())
{
try
{
int m = daBCast.Update(dsBcast1.Broadcast);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
this.Close();
}

</snap>


IMHO, the Update command should now have the correct Parameter in it.
Isn't it?
Well, when debugging this line-by-line, the @FK_Client_Key has always
the correct value. BUT when inserting the values in the database, the
value inserted is 0. No idea why...

In the database the FK_Client_Key has a foreign key constraint to
another table.

The variable Client_Key is a int variable. And it really has (the
correct) value.

Any help would be appreciated !

Regards,
Marc
 
W

William Ryan eMVP

Mark:

Just to verify that it's not a problem with Client_Key add this line right
before you call update

debug.Assert(daBCast.InsertCommand.Parameters["@FK_Client_Key"].Value != 0);

If the value is 0 right before the update, you should get a big ugly
Assertion box telling you the assertion failed...ergo the value is in fact
0. The other thing I'd check is the Insert Command and the Bindings...it's
possible that the param value is mapped to a control and that's overriding
the other code ..

Is there any other place the update may be getting called from where that
value could be getting overwritten?
 
M

Marc

William

Also the assertion says, that my Client_Key must have
another value than 0....

And there's only 0, exactly 1 place in the whole code,
where this value will be written. And also 1 place, from
where it's called.

Thanks !
Marc


Mark:

Just to verify that it's not a problem with Client_Key add this line right
before you call update

debug.Assert(daBCast.InsertCommand.Parameters["@FK_Client_Key"].Value != 0);

If the value is 0 right before the update, you should get a big ugly
Assertion box telling you the assertion failed...ergo the value is in fact
0. The other thing I'd check is the Insert Command and the Bindings...it's
possible that the param value is mapped to a control and that's overriding
the other code ..

Is there any other place the update may be getting called from where that
value could be getting overwritten?
Hi All,

Strange problem, but I didn't find a solution:

I created a C# Form. Because it's some easy stuff to handle here, I
used the drag-n-drop-Function to create DataConnection, DataSet &
DataAdapter.
So far, so well.

Update and Delete functions work fine. BUT the Insert function won't
do it's work as expected.

Here's a code-snippet:

<snip>
private void Broadcast_Load(object sender, System.EventArgs e)
{
if(Broadcast_Key > 0)
{
button11.Text = "Update";
daBCast.SelectCommand.Parameters["@Broadcast_Key"].Value =
Broadcast_Key;
daBCast.Fill(dsBcast1);
}
cm1 = (CurrencyManager) this.BindingContext[dsBcast1, "Broadcast"];
if(Broadcast_Key < 0)
{
button11.Text = "Insert";
cm1.AddNew();
}
}

private void clickSave(object sender, System.EventArgs e)
{
this.daBCast.InsertCommand.Parameters["@FK_Client_Key"].Value =
Client_Key;
cm1.EndCurrentEdit();

if(dsBcast1.HasChanges())
{
try
{
int m = daBCast.Update(dsBcast1.Broadcast);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
this.Close();
}

</snap>


IMHO, the Update command should now have the correct Parameter in it.
Isn't it?
Well, when debugging this line-by-line, the @FK_Client_Key has always
the correct value. BUT when inserting the values in the database, the
value inserted is 0. No idea why...

In the database the FK_Client_Key has a foreign key constraint to
another table.

The variable Client_Key is a int variable. And it really has (the
correct) value.

Any help would be appreciated !

Regards,
Marc
 

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