How can i bind a Database field value to a hidden field inside a gridview
I use the following to bind a field from the table to a hidden field inside a gridview but i am getting the error as System.Data.DataRowView' does not contain a property with the name 'AccountType'.
This is how I assigned
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnAccntType" runat="Server" Value='<%#Eval("AccountType") %>' />
</ItemTemplate>
</asp:TemplateField>
Is it correct or do I have to make any corrections?
My stored procedure:
CREATE DEFINER=`root`@`%` PROCEDURE `uspGetEmployeeBankDate`(_EmpID INT(11),
_BankTypeID varchar(10),_AccountType varchar(10))
BEGIN
select EmpID,BankTypeID,DATE_FORMAT(StartDate, '%Y-%m-%d')
as StartDate,DATE_FORMAT(EndDate, '%Y-%m-%d') as EndDate
from tblemployeebankdata where EmpID=_EmpID and BankTypeID=_BankTypeID
and AccountType=_AccountType;
END
Sample code
if (mlocal_ds.Tables[0].Rows.Count != 0)
{
foreach (DataRow drRow in mlocal_ds.Tables[0].Rows)
{
if (drRow["BankTypeID"].ToString() == "DbalC")
{
pnlGrid.Visible = false;
grdBank.Visible = true;
//grdData.Visible = false;
strEmpID = HiddenField1.Value;
string AccntType = drRow["AccountType"].ToString();
string strBankTypeID = drRow["BankTypeID"].ToString();
string mlocal_strStoredProcName = StoredProcNames.tblEmployeeBankdetails_uspEmployeeBankdetailsDate;
mlocal_ds = new DataSet();
oEmployee.SelectId(out mlocal_ds, mlocal_strStoredProcName, strEmpID, strBankTypeID,AccntType); // here the stored procedure is executed
grdBank.DataSource = mlocal_ds;
grdBank.DataBind();
pnlChckAcc.Visible = false;
pnlPrimary.Visible = false;
pnlSecond.Visible = false;
pnlSecondary.Visible = false;
pnlFirst.Visible = false;
pnlEditInfo.Visible = false;
break;
}
My grid view
<asp:GridView ID="grdBank" runat="server" AutoGenerateColumns="False" CellPadding="4"
CssClass="grid" ForeColor="#333333" GridLines="None" Width="349px" Visible="False">
<Columns>
<asp:TemplateField>开发者_运维知识库
<ItemTemplate>
<asp:RadioButton ID="rdbtnBank" runat="server" AutoPostBack="true" OnCheckedChanged="rdbtnBank_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hdnAccntType" runat="Server" Value='<%# DataBinder.Eval(Container.DataItem, "AccountType") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmpID" HeaderText="EmpID">
<ItemStyle CssClass="intitalitefname" />
</asp:BoundField>
<asp:BoundField DataField="BankTypeID" HeaderText="BankTypeID">
<ItemStyle CssClass="intitalitefname" />
</asp:BoundField>
<asp:BoundField DataField="StartDate" HeaderText="Start Date">
<ItemStyle CssClass="intitalitefname" />
</asp:BoundField>
<asp:BoundField DataField="EndDate" HeaderText="End Date">
<ItemStyle CssClass="intitalitefname" />
</asp:BoundField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EmptyDataTemplate>
nodata to display
</EmptyDataTemplate>
</asp:GridView>
As Muhammed Akhtar said your original datasource doesn't include the AccountType field so you can't bind this to anything
You need to change your Stored Procedure to:
CREATE DEFINER=`root`@`%` PROCEDURE `uspGetEmployeeBankDate`(_EmpID INT(11),
_BankTypeID varchar(10),_AccountType varchar(10))
BEGIN
select AccountType, EmpID,BankTypeID,DATE_FORMAT(StartDate, '%Y-%m-%d')
as StartDate,DATE_FORMAT(EndDate, '%Y-%m-%d') as EndDate
from tblemployeebankdata where EmpID=_EmpID and BankTypeID=_BankTypeID
and AccountType=_AccountType;
END
The I've modified is right at the beginning of your "select" which now includes the AccountType field. If you don't include this field in your original select it's not going to be included in the datasource you get back which in turn means you can't bind it
Hope that helps
Dave
you should use the notation: <%# DataBinder.Eval(Container.DataItem, "AccountType") %>
精彩评论