views:

177

answers:

1

I am trying to use a text box input as a SqlParameter but it only goes into DataSelecting when the page first loads. Not after the from is submitted.

Here is the code on the aspx page.

protected void DataSelecting(object sender, SqlDataSourceSelectingEventArgs e) { e.Command.Parameters["@zip"].Value = ZipBox.Text; }

"
SelectCommand="SELECT Name FROM Names WHERE (ZipCode = @zip)" OnSelecting="DataSelecting"> SelectParameters> parameter Name="zip" DefaultValue="1" /> SelectParameters> SqlDataSource>

FORM

id="ZipSearch" runat="server" action="Default.aspx" method="post">

TextBox ID="ZipBox" runat="server" />

Button id="btnSubmit" Text="Submit" runat="server" />

FORM

Thanks for your help,

Matt

A: 

You need to place that code in the button click event. Selecting event is for different purpose.

Old reply (Before OP's comment) : What do you have in button click event? The Selecting event would fire before your select command is executed. Hence if your button click event is firing any command, the Selecting event won't be fired.

danish
If I put e.Command.Parameters["@zip"].Value = ZipBox.Text; in the btnSubmit_Click(object sender, EventArgs e) I get an error that it can't find "Command"
Droter
Yes. That is because "e" is EveentArgs. Use the ID of your datasource and look into its properties to set the command parameters.
danish
Thanks Danish. It works now using a btnSumbit_Click method:{SqlDataSourceID.SelectParameters["zip"].DefaultValue = ZipBox.Text;}
Droter