Get data from sql query into textbox

2019-07-02 05:46发布

问题:

Fixed it by closing the connetion at page init and then reopen at Dropdownlist.

I've searched for an answer to this question but without any luck.

I want to get data from a selected dropdownlist item to a textbox. I've been trying to execute this sql query without any success.

Here's my code:

    public partial class EditContact : System.Web.UI.Page
{
    SqlConnection connection = new SqlConnection("SqlConnection");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Page_Init(object sender, EventArgs e)
    {
        connection.Open();
        SqlCommand SqlCommandDD = new SqlCommand("SELECT FirstName + ' ' + LastName AS 'TextField', Contact_ID, Email, PhoneNumber, CompanyID FROM ContactPerson");
        SqlCommandDD.Connection = connection;

        DropDownList2.DataSource = SqlCommandDD.ExecuteReader();
        DropDownList2.DataValueField = "Contact_ID";
        DropDownList2.DataTextField = "TextField";
        DropDownList2.DataBind();

    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string fNameTemp = DropDownList2.SelectedValue;


        string sqlquery = ("SELECT FirstName FROM ContactPerson WHERE (Contact_ID = " + fNameTemp + ")");

        SqlCommand command = new SqlCommand(sqlquery, connection);

        SqlDataReader sdr = command.ExecuteReader();

        fNameTextBox.Text = sdr.ToString();

    }


}

回答1:

The “ExecuteReader” returns complex/non-scalar value. Use the “ExecuteScalar” method instead:

SqlCommand command = new SqlCommand(sqlquery, connection);
fNameTextBox.Text = command.ExecuteScalar().ToString();


回答2:

Try modifying the code this way:

string sqlquery = "SELECT FirstName FROM ContactPerson WHERE Contact_ID = " + fNameTemp;

SqlCommand command = new SqlCommand(sqlquery, connection);

SqlDataReader sdr = command.ExecuteReader();

while ( sdr.Read() )
{
    fNameTextBox.Text = sdr[ "FirstName" ].ToString();
}


回答3:

If we are going to fetch a single value from a table.. Then we can go for execute scalar instead of data adapter or data reader..

Method 1:

 fNameTextBox.Text = command.ExecuteScalar().ToString();

Method 2:(If you use any common function)

object scalarobject;
   scalarobject = command.ExecuteScalar();
fNameTextBox.Text = scalarobject.ToString();


回答4:

There is several way to achieve this, close the connection by hardcode was not the best practice instead of using connection.close() you can place the connection inside using tag, this your code with using tag

using (SqlConnection connection = new SqlConnection("SqlConnection"))
{
        connection.Open();
        SqlCommand SqlCommandDD = new SqlCommand("SELECT FirstName + ' ' + LastName AS 'TextField', Contact_ID, Email, PhoneNumber, CompanyID FROM ContactPerson");
        SqlCommandDD.Connection = connection;

        DropDownList2.DataSource = SqlCommandDD.ExecuteReader();
        DropDownList2.DataValueField = "Contact_ID";
        DropDownList2.DataTextField = "TextField";
        DropDownList2.DataBind();
}

no need to close the connection manually