Name既不是数据列也不是表的数据关系。 [英] Name is neither a datacolumn nor a datarelation for table .

查看:400
本文介绍了Name既不是数据列也不是表的数据关系。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击gridview中的编辑按钮时,我无法在下拉列表中获得以前的值。



我尝试过:



I am not able to get previous value in dropdownlist when i click on edit button in gridview.

What I have tried:

if (e.Row.RowType == DataControlRowType.DataRow && gvcallRegister.EditIndex == e.Row.RowIndex)
      {

           if ((e.Row.RowState & DataControlRowState.Edit) > 0)
           {
          using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
          {
              DropDownList ddlprod = (DropDownList)e.Row.FindControl("DropDownList1");
              con.Open();

              SqlCommand cmd = new SqlCommand("select name from registeruser where dept='Sales'", con);
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              DataSet ds = new DataSet();
              da.Fill(ds);
              ddlprod.DataSource = ds;
              ddlprod.DataTextField = "name";
              ddlprod.DataValueField = "name";
              ddlprod.DataBind();

              DataRowView dr = e.Row.DataItem as DataRowView;
              ddlprod.SelectedValue = dr["name"].ToString();// default selected value

                  }
              }

          }
      }

推荐答案

查看错误消息:

Look at the error message:
引用:

名称既不是数据列也不是表的数据化

Name is neither a datacolumn nor a datarelation for table

然后查看代码:

Then look at your code:

              SqlCommand cmd = new SqlCommand("select name from registeruser where dept='Sales'", con);
...
              ddlprod.DataTextField = "name";
              ddlprod.DataValueField = "name";
...
              ddlprod.SelectedValue = dr["name"].ToString();

现在检查一下你的数据库:它是否在registeruser表中有一个名为name的列?如果是,请使用调试器检查DataRowView并确切地查看它包含的单元格。那里有名字吗?



错误信息说其中一个没有。

Now check your DB: does it have a column called "name" in the table "registeruser"? If it does, use the debugger to check the DataRowView and see exactly what cells it contains. Is "name" there?

The error message says one of them doesn't.


这篇关于Name既不是数据列也不是表的数据关系。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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