位置0处没有任意行,求解!
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = GetData();
if (!IsPostBack)
{
if (ds.Tables[0].Rows.Count > 0)
{
int k = ds.Tables[0].Rows.Count;//这句是我为了找错误的原因而写的,快速监视k=14;
DataTable companyname = new DataTable();
companyname.Columns.Add("Company", System.Type.GetType("System.String"));//创建列并给列取名和指定类型
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i]["Company"] != null && Convert.ToString(ds.Tables[0].Rows[i]["Company"]) != "")
{
companyname.Rows[i]["Company"] = ds.Tables[0].Rows[i]["Company"];
}
}
DropDownList1.DataSource = companyname.DefaultView;
DropDownList1.DataTextField = "Company";
DropDownList1.DataValueField = "Company";
}
}
}
public DataSet GetData()
{
string connectionString = ConfigurationManager.ConnectionStrings["DiscountConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select Company from Protection", conn);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
conn.Close();
return dataset;
}
[解决办法]
companyname 只看到你新增列了,
没有看到你新增行,
没有添加行,当然不能操作