简体   繁体   中英

Pass variable to SqlDataSource.SelectParameter, then pass query result to new string ASP.NET

I have the following SqlDataSource defined as a DataReader, with a blank Default Value:

<asp:SqlDataSource ID="getPhoneNumber" runat="server" ConnectionString="<%$ ConnectionStrings:MobileLeasesConnectionString %>" DataSourceMode="DataReader" SelectCommand="SELECT Phone FROM MobileLeases WHERE OwnerName = @techName">
        <SelectParameters>
            <asp:Parameter Name="techName" Type="string" />
        </SelectParameters>
    </asp:SqlDataSource>

I have a button click event that grabs the value of a Form field at the time the button is clicked, and puts it in string techName ...

protected void submitter_Click(object sender, EventArgs e)
{        
    //grab the value of ftechName at the time the button is clicked...        
    string techName = Request.Form["ftechName"]; //grab the techs name}

I need to pass the value of techName to getPhoneNumber... and return the single result to a new string, call it string phoneNum .

I have tried several things and just need to be advised from scratch, because it is all a blur now.

Big picture, it should look like this...

protected void submitter_Click(object sender, EventArgs e)
{        
    //grab the value of ftechName at the time the button is clicked...        
    string techName = Request.Form["ftechName"]; //grab the techs name}

    //run select command with techName as the SELECT parameter...
    getPhoneNumber > Select Phone WHERE OwnerName = techName
    string phoneNum = result of getPhoneNumber query

What is the best method for passing techName and getting the result into phoneNum ?

Thank you so much for any advice.

Not sure, because I don't use much ASP.NET:

getPhoneNumber.SelectParameters.Add("@techName", System.Data.DbType.String, techName);

** UPDATE: **

Here is some code I wrote that works without that asp:SqlDataSource control:

protected void Submit_Clicked(object sender, EventArgs e)
{
    var techName = ftechName.Text.Trim();
    // assuming your phone number control on your ASP.NET page is "rPhonenumber"
    rPhonenumber.Text = GetPhoneNumber(techName);
}

private const String SQL_CONNECTION = "ConnectionStrings:MobileLeasesConnectionString";

private String GetPhoneNumber(String techName)
{
    var result = String.Empty;
    using (var con = new System.Data.SqlClient.SqlConnection(SQL_CONNECTION))
    {
        // Give it your SQL command
        var sql = "SELECT Phone FROM MobileLeases WHERE OwnerName = @techName";
        // Create an SqlCommand instance
        var cmd = new System.Data.SqlClient.SqlCommand(sql, con);
        // Supply it with your parameter (data type and size should match whatever the value is for [MobileLeases].[OwnerName] in SQL)
        cmd.Parameters.Add("@techName", System.Data.SqlDbType.VarChar, 40).Value = techName;
        // Don't forget to open the connection!
        cmd.Connection.Open();
        // String.Format handles the case of the SELECT command returning NULL - converting that to an empty string
        result = String.Format("{0}", cmd.ExecuteScalar());
        // optionally, you can close the connection, but the 'using' statement should take care of that.
        cmd.Connection.Close();
    }
    // return your results
    return result;
}

Much simpler to understand what's going on, in my opinion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM