Ragazzi, non capisco una cosa: utilizzando il codice quì sotto la Textbox rimane vuota:
codice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
public partial class Binding : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] list = new string[] {"Primo", "Secondo", "Terzo" };
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "localhost\\SQLExpress";
builder.IntegratedSecurity = true;
builder.InitialCatalog = "Daniele";
using(SqlConnection conn = new SqlConnection(builder.ConnectionString))
{
conn.Open();
using (SqlCommand query = new SqlCommand("SELECT * FROM Products WHERE Cognome='Marino'", conn))
{
using (SqlDataReader lettura = query.ExecuteReader())
{
DropDownList1.DataSource = lettura;
DropDownList1.DataValueField = "Nome";
DropDownList1.DataTextField = "Cognome";
DropDownList1.DataBind();
while (lettura.Read())
{
TextBox1.Text = lettura["Nome"].ToString();
}
}
}
}
}
}
invece facendo in questo modo, la TextBox viene correttamente popolata mentre la DropDownList resta vuota:
codice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
public partial class Binding : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] list = new string[] {"Primo", "Secondo", "Terzo" };
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "localhost\\SQLExpress";
builder.IntegratedSecurity = true;
builder.InitialCatalog = "Daniele";
using(SqlConnection conn = new SqlConnection(builder.ConnectionString))
{
conn.Open();
using (SqlCommand query = new SqlCommand("SELECT * FROM Products WHERE Cognome='Marino'", conn))
{
using (SqlDataReader lettura = query.ExecuteReader())
{
DropDownList1.DataSource = lettura;
DropDownList1.DataValueField = "Nome";
DropDownList1.DataTextField = "Cognome";
while (lettura.Read())
{
TextBox1.Text = lettura["Nome"].ToString();
}
DropDownList1.DataBind();
}
}
}
}
Mi spiegate qual'è il problema? Come si può notare, l'unica cosa che ho fatto è stato spostare la riga di binding della Dropdown
codice:
DropDownList1.DataBind()
qualche riga più in basso, ma non capisco il nesso con la TextBox.