Do you really see the Country names in the DDL using that code ?

IMHO, without setting the DisplayValueField and DisplayTextField
properties of the DropDownList, all you should see is a result of
ToString() being called on each item within the IEnumerable
datasource... in this case, it might be "DataRowView".

On Jul 1, 1:12 pm, vickyk <[email protected]> wrote:
> There is a table in a database having country names and their id.
> I fetched the data from the table to a dropdownlist,which i did by
> writing:
>
> public partial class _Default : System.Web.UI.Page
> {
>     protected void Page_Load(object sender, EventArgs e)
>     {
>         SqlConnection con = new SqlConnection("Persist Security
> Info=False;Integrated Security=SSPI;server=CODENAME;database=try");
>         SqlCommand com = new SqlCommand("Select CountryId,CountryName
> from country", con);
>         SqlDataAdapter da = new SqlDataAdapter(com);
>         DataSet ds = new DataSet();
>         da.Fill(ds);
>         countryddl.DataSource = ds;
>         countryddl.DataBind();
>     }
>
> now i want to display the corresponding ID of the country when the
> country is selected so i
> wrote this and postback is ON.
>
> protected void countryddl_SelectedIndexChanged(object sender,
> EventArgs e)
>     {
>         int sel_val = Convert.ToInt32(Convert.ToString
> (countryddl.SelectedValue));
>         Response.Write(sel_val);
>
> }
>
> still it always gives ID of the 1st country no matter what i choose.
> how to solve this ??

Reply via email to