How could I show more than the value of a column in a dropdown?
I tried the following method:
private void BindDropDownList()
{
DataTable dt = new DataTable();
string localidade = string.Empty;
string distrito = string.Empty;
string newName = string.Empty;
SqlConnection connection;
string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
connection = new SqlConnection(connectionString);
try
{
connection.Open();
string sqlStatement = "SELECT * FROM [Moradas] WHERE ([IDUser] = @IDUser)";
SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
localidade = dt.Rows[i]["Localidade"].ToString();
distrito = dt.Rows[i]["Distrito"].ToString();
newName = localidade + " ---- " + distrito;
ddlMoradaSecd.Items.Add(new ListItem(newName, localidade));
}
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
connection.Close();
}
}
Without WHERE
it works fine, but I need WHERE
, just to show the addresses of that user, I need a WHERE
with sessionparameter
and sessionfield
, just as I am using sqldatasource
%:
<asp:SqlDataSource ID="SqlDataSourceMoradaSecd" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Moradas] WHERE ([IDUser] = @IDUser) ORDER BY [Morada]">
<SelectParameters>
<asp:SessionParameter Name="IDUser" SessionField="IDUtilizador" Type="String" />
</SelectParameters>
</asp:SqlDataSource>