我是ASP.NET的新手,我在页面加载时尝试从select
设置query
中的数据
这是mi表:
区域
ID_Area|ID_Formula|Nombre
现在我的代码
ASPX
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label >Nombre de Area</label>
<asp:TextBox runat="server" ID="area" CssClass="form-control" required="true"></asp:TextBox>
</div>
<div class="form-group">
<label>Formula asociada</label>
<select id="Select1" runat="server" name="D1"> </select>
</div>
<div class="form-group">
<asp:Button ID="btn_login" OnClick="docreatearea" CssClass="btn btn-primary btn-lg btn-block" Text="Guardar" runat="server"/>
</div>
</div>
</div>
</div>
ASPX.CS
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
string query = @"SELECT * FROM FORMULA";
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BancoEstadoConnectionString"].ToString()))
{
SqlCommand comand = new SqlCommand(query, conn);
DataSet ds = new DataSet();
comand.Fill(ds, "Area");
conn.Open();
Select1.DataSource = ds;
Select1.DataTextField = "Nombre";
Select1.DataValueField = "Nombre";
Select1.DataBind();
}
}
}
问题出现在fill function
,因为SqlCommand object
我该怎么办呢?修复它?
答案 0 :(得分:1)
au_fname 。将其更改为表格中的有效列名称。
答案 1 :(得分:0)
Fill命令位于SqlDataAdapter类中。
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(ds);
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter(v=vs.110).aspx
答案 2 :(得分:0)
您需要使用SqlDataAdapter来填充DataSet:
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BancoEstadoConnectionString"].ToString()))
{
SqlCommand comand = new SqlCommand(query, conn);
DataSet ds = new DataSet();
conn.Open();
var adapter = new SqlDataAdapter(command);
adapter.Fill(ds, "Authors");
Select1.DataSource = ds;
Select1.DataTextField = "au_fname";
Select1.DataValueField = "au_fname";
Select1.DataBind();
}
答案 3 :(得分:0)
用户下面的页面加载方法代码
if(!IsPostBack){
string query = @"SELECT * FROM FORMULA";
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BancoEstadoConnectionString"].ToString()))
{
SqlCommand comand = new SqlCommand(query, conn);
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
Select1.DataSource = ds;
Select1.DataTextField = "au_fname";
Select1.DataValueField = "au_fname";
Select1.DataBind();
}
}