数据绑定到dropdownlist在asp.net中获取无效对象名称的错误

本文关键字:对象 无效 错误 获取 dropdownlist asp net 数据绑定 | 更新日期: 2023-09-27 18:27:12

SqlDataAdapter da = new SqlDataAdapter("select * from state1",con);
DataSet ds = new DataSet();
da.Fill(ds,"state1");------------getting error in this line like invalid object name,satate1
ddlstate.DataSource = ds;
ddlstate.DataTextField = "sname";
ddlstate.DataValueField = "sid" ;
ddlstate.DataBind();

数据绑定到dropdownlist在asp.net中获取无效对象名称的错误

我认为错误是"object name,state1",在这种情况下,错误是从数据库中抛出的,因为它找不到名为state1的表或视图。请检查您是否连接到了正确的数据库,或者您的表是否在dbo之外的另一个架构中,是否使用了架构限定名称,例如schemaname.state1

我认为这可能是问题所在:

SqlDataAdapter da = new SqlDataAdapter("select * from state1",con);
DataSet ds = new DataSet();
da.Fill(ds,"state1");
ddlstate.DataSource = ds;//------------------> I think this one is worng. It should be table i.e. ds.Tables[0]/ds.Tables["state1"] if "ds" is not null.
ddlstate.DataTextField = "sname";
ddlstate.DataValueField = "sid" ;
ddlstate.DataBind();