首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

无法将数据绑定到TextBOx上面,该怎么解决

2012-02-28 
无法将数据绑定到TextBOx上面privatevoidcomboBoxID_SelectedValueChanged(objectsender,EventArgse){try{

无法将数据绑定到TextBOx上面
private   void   comboBoxID_SelectedValueChanged(object   sender,   EventArgs   e)
                {
                        try
                        {
                                //SqlCommand   selectCmd   =   new   SqlCommand( "select   *   from   employee   where   cID= ' "   +   comboBoxID.SelectedItem   +   " ' ",   DBConnect.SqlConnect());
                                SqlDataAdapter   da   =   new   SqlDataAdapter( "select   *   from   employee   where   cID= ' "   +   comboBoxID.SelectedItem   +   " ' ",   DBConnect.SqlConnect());
                              //   da.SelectCommand   =   selectCmd;
                                DataSet   ds   =   new   DataSet();
                                da.Fill(ds,   "table1 ");
                                txtID.Text   =   ds.Tables[ "teble1 "].Rows[0][ "cID "].ToString();
                                txtName.Text   =   ds.Tables[ "table1 "].Rows[1][ "cName "].ToString();
                                txtPwd.Text   =   ds.Tables[ "table1 "].Rows[2][ "vPassword "].ToString();
                                dateTimePicker1.Text   =   ds.Tables[ "table1 "].Rows[3][ "dBirthday "].ToString();
                                txtAge.Text   =   ds.Tables[ "table1 "].Rows[4][ "cAge "].ToString();
                                comboBoxSex.SelectedItem   =   ds.Tables[ "table1 "].Rows[5][ "cSex "].ToString();
                                txtNation.Text   =   ds.Tables[ "table1 "].Rows[6][ "cNation "].ToString();
                                txtXueli.Text   =   ds.Tables[ "table1 "].Rows[7][ "cXueli "].ToString();
                                txtVisage.Text   =   ds.Tables[ "table1 "].Rows[8][ "cVisage "].ToString();
                                txtAddress.Text   =   ds.Tables[ "table1 "].Rows[9][ "cAddress "].ToString();


                                txtGongling.Text   =   ds.Tables[ "table1 "].Rows[10][ "cGongling "].ToString();
                                comboBoxType.SelectedItem   =   ds.Tables[ "table1 "].Rows[11][ "cType "].ToString();
                                comboBoxDepart.SelectedItem   =   ds.Tables[ "table1 "].Rows[12][ "cDepartment "].ToString();
                                comboBoxDuty.SelectedItem   =   ds.Tables[ "table1 "].Rows[13][ "cDuty "].ToString();
                                DBConnect.SqlConnect().Close();
                        }
                        catch   (Exception   e1)
                        {
                                MessageBox.Show(e1.Message);
                        }
                }

提示我索未将对象引用设置到对象的实例...     我看不懂了啊!!求救

[解决办法]
你在绑定comboBoxID的数据的时候要这样...
comboBoxID.DataSource = ds.Tables[ "table1 "];
comboBoxID.ValueMember = [ "cID "];
comboBoxID.DisplayMember = [ "cID "];

然后在下面的代码做如下更改
private void comboBoxID_SelectedValueChanged(object sender, EventArgs e)
{
try
{
SqlDataAdapter da = new SqlDataAdapter( "select * from employee where cID= ' " + comboBoxID.SelectedValue + " ' ", DBConnect.SqlConnect());
DataSet ds = new DataSet();
da.Fill(ds, "table1 ");

if (ds.Tables[ "table1 "].Rows.Count > 0)
{
txtID.Text = ds.Tables[ "teble1 "].Rows[0][ "cID "].ToString();
txtName.Text = ds.Tables[ "table1 "].Rows[0][ "cName "].ToString();
txtPwd.Text = ds.Tables[ "table1 "].Rows[0][ "vPassword "].ToString();
dateTimePicker1.Text = ds.Tables[ "table1 "].Rows[0][ "dBirthday "].ToString();
txtAge.Text = ds.Tables[ "table1 "].Rows[0][ "cAge "].ToString();
comboBoxSex.Text = ds.Tables[ "table1 "].Rows[0][ "cSex "].ToString();
txtNation.Text = ds.Tables[ "table1 "].Rows[0][ "cNation "].ToString();
txtXueli.Text = ds.Tables[ "table1 "].Rows[0][ "cXueli "].ToString();
txtVisage.Text = ds.Tables[ "table1 "].Rows[0][ "cVisage "].ToString();
txtAddress.Text = ds.Tables[ "table1 "].Rows[0][ "cAddress "].ToString();
txtGongling.Text = ds.Tables[ "table1 "].Rows[0][ "cGongling "].ToString();
comboBoxType.Text = ds.Tables[ "table1 "].Rows[0][ "cType "].ToString();


comboBoxDepart.Text = ds.Tables[ "table1 "].Rows[0][ "cDepartment "].ToString();
comboBoxDuty.Text = ds.Tables[ "table1 "].Rows[0][ "cDuty "].ToString();
DBConnect.SqlConnect().Close();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}

热点排行