Reputation: 153
I have the following procedure that serves to show in a DropDownList the "Descricao" values in my database:
public void descricaoEntrada()
{
StringBuilder sql = new StringBuilder();
sql.Append("SELECT [Descricao], [IDEntrada] FROM [dbo].[Entrada] where IDEntradaTipo = 2 order by Descricao");
SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringSQL"].ConnectionString);
try
{
connection.Open();
SqlDataReader dr = Utils.DatabaseManagement.ExecuteReader(sql.ToString(), connection);
descricao.DataSource = dr;
while (dr.Read())
{
descricao.Items.Add(new ListItem(dr["Descricao"].ToString(), dr["IDEntrada"].ToString()));
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
My query also retrieves another field called "IDEntrada" and I retrieve it in:
descricao.Items.Add(new ListItem(dr["Descricao"].ToString(), dr["IDEntrada"].ToString()));
Then over my code I'm trying to retrieve the value of the "Descricao" and the value of the "IDEntrada" through the command:
descricao.Text
But the only thing that is retrieved is the value of "IDEntrada", and I want to be able to separately recover both the "IDEntrada" and the value "Descricao"
I want to retrieve values separately in variables.
How can I do this?
Upvotes: 0
Views: 32
Reputation: 1281
You don't need following line.
descricao.DataSource = dr; //remove this line
You are setting Descricao
as your dropdown option's text and IDEntrada
as its value.
So in your case, to get selected item's text (i.e. Descricao
) use:
descricao.SelectedItem.Text
And To get selected item's value (i.e. IDEntrada
) use:
descricao.SelectedItem.Value
or
descricao.SelectedValue
Upvotes: 1