SqlDataSource not passing parameter first time called

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

Here is my SqlDataSource. You can see that I have a SelectCommand,
onSelecting and onDataBinding.


<asp:SqlDataSource ID="SqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectString %>"
SelectCommand="GetResponseFileExceptions"
SelectCommandType="StoredProcedure"
OnSelecting="SqlDataSource_Selecting"
OnDataBinding="SqlDataSource_DataBinding"
UpdateCommand="UpdateDummy"
UpdateCommandType="StoredProcedure">
</asp:SqlDataSource>

In my GridView, I have:

<asp:GridView ID="GridView1"
DataSourceID="SqlDataSource"
AutoGenerateColumns="False"
SkinID="mGridViewSkin"
CssClass="Grid100"
AllowPaging="True"
AllowSorting="True"
PageSize="15"
PagerSettings-Mode="NumericFirstLast"
PagerStyle-CssClass="GridViewPager"
OnPageIndexChanged="mGridView_PageIndexChanged"
OnRowEditing="GridView1_RowEditing"
OnRowDataBound="ItemDataBoundEventHandler1"
OnRowUpdating="GridView1_RowUpdating"
runat="server">


In my code I have the following:

protected void SqlDataSource_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
SqlDataSource.SelectParameters.Clear();
SqlDataSource.SelectParameters.Add("ClientID", TypeCode.Int32,
"36");
}
protected void SqlDataSource_DataBinding(object sender, EventArgs e)
{
}

When I initially run my page, the SelectCommand is executed. But before it
executes it calls the SqlDataSource_Selecting event where it adds a
parameter. I can tell by looking at the Profiler that the command is not
sent to Sql until after the parameter is added. But the Store Procedure is
sent without the parameter.

When I press the page number at the bottom of the grid it again goes to the
SqlDataSource_Selecting procedure and this time it calls the Stored
Procedure with the parameter.

Why didn't the parameter get sent on the first call? It was set.

Also, what caused the command to be set at all?

I never did a databind.

Also, I have an event set for onDataBinding, but it never gets called. Why
is that?

Thanks,

Tom
 
I made a change to the code behind to use one value if not postback and
another value if a postback.

The event is now:

protected void SqlDataSource_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
if (!Page.IsPostBack)
{
SqlDataSource.SelectParameters.Clear();
SqlDataSource.SelectParameters.Add("ClientID",
TypeCode.Int32, "36");
}
else
{
SqlDataSource.SelectParameters.Clear();
SqlDataSource.SelectParameters.Add("ClientID",
TypeCode.Int32, "66");
}
}

1) On the first pass, the event sets the value to 36, but the store
procedure is called with no parameters:
2) On the 2nd pass, the value is set to 66, but the stored procedure is sent
with one parameter set to 36 (the previous time the event was called).

In MSDN:

Handle the Selecting event to perform additional initialization operations
that are specific to your application, to validate the values of parameters,
or to change the parameter values before the SqlDataSource control performs
the select operation. The select arguments are available from the
SqlDataSourceSelectingEventArgs object that is associated with the event.

It says that the call is done BEFORE the SqlDataSource performs the select
operation. But it doesn't use the changed value until the next time it is
called.

Why is that?

Thanks,

Tom
 
Back
Top