3
How could it show more than the value of a column of 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 the WHERE
works correctly, but I need the WHERE
, to just show the addresses of that user, I need a WHERE
with sessionparameter
and sessionfield
, as I am using in 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>
What error is occurring? Apparently your code is right.
– Ulysses Alves
The error is that the scalar variable @Iduser is missing
– Chirag Geiantilal