始终仅从数据库显示第一个id数据 [英] Always only 1st id data is showing from database

查看:102
本文介绍了始终仅从数据库显示第一个id数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我,每当我从组合框中选择任何ID时,它只会显示第一条记录.为什么?
我需要从组合框中选择的一条记录.我的代码是:

Please someone tell that whenever I select any id from combobox, it shows only 1st record. Why?
I need a record which I have selected from the combobox. my code is:

cmd = new SqlCommand("select * from Images where id=" + DropDownList1 .SelectedValue + "",con);
            dr = cmd.ExecuteReader();

            while(dr.Read ())
            {
                TextBox1.Text = dr[1].ToString();

            }



请有人告诉解决方案

谢谢



Pls someone tell the solution

Thanks

推荐答案

只需检查DropDownList1的绑定,因为可能是"DropDownList1.SelectedValue"总是返回第一个项目值,因为在它们之间,在运行之前,它们之间已再次绑定代码.

因此,您将始终获得第一条记录.
Just check the binding of the DropDownList1 because may be "DropDownList1.SelectedValue" is returning always the first item value because in between it has again binded before running the code.

So you are getting always the 1st record.


类似的声音是一个Web应用程序,并且您在每次回发时都将数据源分配给dropdownlist.

使用页面属性IsPostback避免在选择更改的事件上重新绑定,以使下拉列表保留选择.
Sounds like this is a web application and you are assigning the datasource to dropdownlist on every postback.

Use page property IsPostback to avoid re-binding on selection changed event such that the dropdownlist maintains the selection.


这篇关于始终仅从数据库显示第一个id数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆