//这样动态邦定数据.listitem是自己定义的,数据成员一个是文本,另一个是值
private void BindPosition()
{
/*pt.Components.Database db = new pt.Components.Database();
SqlDataReader dr_position = null;
db.RunProc("sp_GetAllPosition",out dr_position);
cboPosition.DataSource = dr_position;
cboPosition.DisplayMember = "Position_Name";
cboPosition.ValueMember = "Position_ID";
//cboPosition.DataBind();
*/
SqlDataReader dr; //存放人物的数据
Database mySQL = new Database();
SqlParameter[] parameters = {

};

mySQL.RunProc("sp_GetAllPosition",parameters,out dr); while(dr.Read())
{
cboPosition.Items.Add(new ListItem(dr["Position_Name"].ToString(),dr["Position_ID"].ToString()));


}
dr.Close();
dr = null;
}
//
cboPosition.Items[cboPosition.SelectedIndex].ToString()//这样读出的是文本
cboPosition.selectedvalue.tostring();//这样运行时报错
怎么读书值?我需要动态添加数据,所以用了items.add().

解决方案 »

  1.   

    cboPosition.selecteditem.tostring();可以不?
      

  2.   

    ListItem item = cboPosition.SelectedItem as ListItem;
    然后从item中读取你的值
      

  3.   

    有关调用实时(JIT)调试而不是此对话框的详细信息,
    请参阅此消息的结尾。************** 异常文本 **************
    System.NullReferenceException: 未将对象引用设置到对象的实例。
       at pt.Form4.cboPosition_SelectedIndexChanged_1(Object sender, EventArgs e) in e:\web\qt\pt\form4.cs:line 638
       at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
       at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
       at System.Windows.Forms.ComboBox.WndProc(Message& m)
       at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    ************** 已加载的程序集 **************
    mscorlib
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///c:/winnt/microsoft.net/framework/v1.1.4322/mscorlib.dll
    ----------------------------------------
    pt
        程序集版本: 1.0.3064.20716
        Win32 版本: 1.0.3064.20716
        基本代码: file:///E:/Web/qt/pt/bin/Debug/pt.exe
    ----------------------------------------
    System.Windows.Forms
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///c:/winnt/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
    ----------------------------------------
    System
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///c:/winnt/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
    ----------------------------------------
    System.Drawing
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///c:/winnt/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
    ----------------------------------------
    System.Xml
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///c:/winnt/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
    ----------------------------------------
    System.Data
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///c:/winnt/assembly/gac/system.data/1.0.5000.0__b77a5c561934e089/system.data.dll
    ----------------------------------------
    System.EnterpriseServices
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///c:/winnt/assembly/gac/system.enterpriseservices/1.0.5000.0__b03f5f7f11d50a3a/system.enterpriseservices.dll
    ----------------------------------------
    mscorlib.resources
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///c:/winnt/assembly/gac/mscorlib.resources/1.0.5000.0_zh-chs_b77a5c561934e089/mscorlib.resources.dll
    ----------------------------------------
    System.Windows.Forms.resources
        程序集版本: 1.0.5000.0
        Win32 版本: 1.1.4322.573
        基本代码: file:///c:/winnt/assembly/gac/system.windows.forms.resources/1.0.5000.0_zh-chs_b77a5c561934e089/system.windows.forms.resources.dll
    ----------------------------------------************** JIT 调试 **************
    计算机的配置文件(machine.config)的 
     system.windows.forms 节中必须设置 jitDebugging 值。
    编译应用程序时还必须启用\r\n调试。\r\n\r\n例如: \r\n\r\n<configuration>\r\n    <system.windows.forms jitDebugging="true" />\r\n</configuration>\r\n\r\n启用 JIT 调试后,任何未处理的异常\r\n都将被发送到此计算机上注册的 JIT 调试器,\r\n而不是由此对话框处理。\r\n
      

  4.   

    strValue = (System.Web.UI.WebControls.ListItem)comboBox1.SelectedItem).Value
    strText = (System.Web.UI.WebControls.ListItem)comboBox1.SelectedItem).Text
      

  5.   

    dr["Position_ID"]的有内容?
    cboPosition.Items[cboPosition.SelectedIndex]取的是 item.Text的内容,而SelectedValue取的是 item.Value的内容。从错误看是 SelectedValue是 null 对象。