我有一个GridView,它允许我在数据库表中插入一个新行。我需要使用来自同一数据库表的值填充'Approved By'的下拉列表,但在表中,approved_by列存储为唯一的ID号。名为“GetWeldID”的存储过程会将数字转换为需要在网站上显示的名称。
这是我的代码: aspx页面:
<asp:TemplateField HeaderText="Approved By">
<ItemTemplate>
<asp:Label ID="lbl_Approved" runat="server" Text='<%#Eval("approved_by") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="tb_approved_by" runat="server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<EmptyDataTemplate>
<tr>
<th></th>
<th>Serial Number</th>
<th>Approved By</th>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="btInsert" Text="Insert New In Scope Record" OnClick="Add" CommandName="EmptyDataTemplate" Class="Button" />
<br />
<asp:Button runat="server" ID="btInsertOut" Text="Insert New Out of Scope Record" OnClick="AddOut" CommandName="EmptyDataTemplate" Class="Button" />
</td>
<td>
<asp:TextBox runat="server" ID="tb_Serial_Number" CssClass="text"></asp:TextBox>
</td>
<td><asp:DropDownList ID="tb_approved_by" runat="server">
</asp:DropDownList> </td>
</tr>
</EmptyDataTemplate>
aspx.cs页面:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.EmptyDataRow)//for empty template
{
DropDownList ddlnewuser = (DropDownList)e.Row.FindControl("tb_approved_by");
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT distinct [dbo].[getWeldName](approved_by) from " + databaseName + "", con);
DataTable ds = new DataTable();
da.Fill(ds);
ddlnewuser.DataSource = ds;
ddlnewuser.DataTextField = "approved_by";
ddlnewuser.DataValueField = "approved_by";
ddlnewuser.DataBind();
}
if (e.Row.RowType == DataControlRowType.Footer)//for footer template
{
DropDownList tb_approved_by = (DropDownList)e.Row.FindControl("tb_approved_by");
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT distinct [dbo].[getWeldName](approved_by) from " + databaseName + "", con);
DataTable ds = new DataTable();
da.Fill(ds);
tb_approved_by.DataSource = ds;
tb_approved_by.DataTextField = "approved_by";
tb_approved_by.DataValueField = "approved_by";
tb_approved_by.DataBind();
}
}
我得到的错误是
其他信息:DataBinding:'System.Data.DataRowView'不包含名为'approved_by'的属性。
一旦我有了这个工作,我需要将下拉列表中的所选项目添加到我的数据库,但是将名称转换回数字ID号码!!
我真的不知道我做错了什么,所以任何帮助都会非常感激,因为我是ASP.NET,C#和SQL Server的初学者。
提前致谢!
答案 0 :(得分:1)
[dbo] .getWeldName是我假设的用户定义函数。在这种情况下,您有一个未命名的列。尝试为该列添加别名,例如SELECT distinct [dbo] .getWeldName AS&#39; approved_by&#39;