填充数据库中的下拉列表c#aspx

时间:2018-05-22 12:15:19

标签: asp.net

我正在努力填写我的下拉列表,有人能告诉我哪里出错了吗?

ASPX CODE在获取连接字符串后继续循环 - 返回连接字符串方法。

protected void Page_Load(object sender, EventArgs e)
{                
  if (!IsPostBack)                
  {
    BindDropDownList();
  }
}

        private string GetConnectionString()
        {

            using (DataManager dmgr = new DataManager())
            {
                dmgr.Connect(ConfigurationManager.AppSettings["ProductionKey"]);
                return BindDropDownList();

            }
        }




        public string BindDropDownList()
        {
            DataTable dt = new DataTable();
            SqlConnection connection = new SqlConnection(GetConnectionString());
            try
            {
                connection.Open();
                string sqlStatement = "SELECT * FROM Itemseriesmaster";
                SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
                SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

                sqlDa.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataTextField = "Description"; // the items to be displayed in the list items
                    DropDownList1.DataValueField = "ID"; // the id of the items displayed
                    DropDownList1.DataBind();
                }
            }
            catch (SqlException ex)
            {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                connection.Close();
            }

             return AppRelativeTemplateSourceDirectory;
        }

DataManager Code - where the code calls into

public DataSet ItemSeriesMaster(int id, string description)
        {
            object[] args = new object[2] { id, description };
            return CallSp(MethodBase.GetCurrentMethod(), args) as DataSet; // i know this is not an sp call.. just testing
        }
    }
}

我正在尝试访问我的数据库并列出清单。

0 个答案:

没有答案