3

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();

    }


}

4 Answers 4

3

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();
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

2 Comments

I got an error saying that I already have a datareader open, associated to command. What does this mean?
Ok fixed it with this code and by closing the connetion at page init and then reopen. Thanks!
0

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();

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.