我有一个Designation
表:
CREATE TABLE TblDesignation
(
[DesgId] [int] IDENTITY(1,1) NOT NULL,
[Designation] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_TblDesignation] PRIMARY KEY CLUSTERED
(
[DesgId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Employee.aspx页面代码:
<div id="divView" align="center" runat="server" >
<asp:Panel ID="Panel1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="EmpId" HeaderText="EMP ID" />
<asp:BoundField DataField="EmpFirstName" HeaderText="FIRST NAME" />
<asp:BoundField DataField="EmpMiddleName" HeaderText="MIDDLE NAME" />
<asp:BoundField DataField="EmpLastName" HeaderText="LAST NAME" />
<asp:BoundField DataField="EmpGender" HeaderText="GENDER" />
<asp:BoundField DataField="EmpDesignation" HeaderText="DESIGNATION" />
<asp:BoundField DataField="EmpAddress" HeaderText="ADDRESS" />
<asp:BoundField DataField="EmpEmailId" HeaderText="EMAIL ID" />
<asp:BoundField DataField="EmpContactNo" HeaderText="CONTACT" />
<asp:BoundField DataField="EmpDOB" HeaderText="D.O.B" dataformatstring="{0:MM/dd/yyyy}"/>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MSAMBConnectionString %>"
SelectCommand="SELECT * FROM [TblEmployee]"></asp:SqlDataSource>
</asp:Panel>
<asp:Button ID="btnAddNew" runat="server" Text="ADD NEW EMPLOYEE"
onclick="btnAddNew_Click" />
</div>
<div id="divForm" runat="server" style="margin-top:4%;">
<table border="2" align="center">
<tr>
<td>
<asp:Label ID="LblEmpId" runat="server" Text="EMP ID"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmpId" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblEmpFirstName" runat="server" Text="FIRST NAME : "></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmpFirstName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblEmpMiddleName" runat="server" Text="MIDDLE NAME : "></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmpMiddleName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblEmpLastName" runat="server" Text="LAST NAME : "></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmpLastName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblGender" runat="server" Text="GENDER : "></asp:Label>
</td>
<td>
<asp:RadioButton ID="rbMale" runat="server" Text="Male" GroupName="Gender" />
<asp:RadioButton ID="rbFemale" runat="server" Text="Female" GroupName="Gender" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblEmpDesignation" runat="server" Text="DESIGNATION : "></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddrDesgn" runat="server" DataSourceID="SqlDataSource2"
DataTextField="Designation" DataValueField="DesgId"
onselectedindexchanged="ddrDesgn_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
**<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MSAMBConnectionString3 %>
SelectCommand="SELECT * FROM [TblDesignation]"></asp:SqlDataSource>**
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblEmpAddress" runat="server" Text="ADDRESS : "></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmpAddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblEmpEmailId" runat="server" Text="EMAIL ID : "></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmpEmailId" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblEmpContactNo" runat="server" Text="CONTACT NO : "></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmpContactNo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblEmpDOB" runat="server" Text="DOB : "></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmpDOB" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="txtEmpDOB_CalendarExtender" runat="server"
Enabled="True" TargetControlID="txtEmpDOB">
</asp:CalendarExtender>
</td>
</tr>
<tr>
<td colspan="1" align="center">
<asp:Button ID="btnSubmit" runat="server" Text="SAVE"
onclick="btnSubmit_Click" /></td>
<td><asp:Button ID="btnUpdate" runat="server" Text="UPDATE"
onclick="btnUpdate_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Employee.aspx">BACK</asp:HyperLink>
</td>
</tr>
</table>
</div>
Employee.aspx.cs C#代码:
On protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (GridView1.SelectedRow.Cells[5].Text == "Male")
{
rbMale.Checked = true;
}
else {
rbFemale.Checked = true;
}
txtEmpId.Text = GridView1.SelectedRow.Cells[1].Text;
txtEmpFirstName.Text = GridView1.SelectedRow.Cells[2].Text;
txtEmpMiddleName.Text = GridView1.SelectedRow.Cells[3].Text;
txtEmpLastName.Text = GridView1.SelectedRow.Cells[4].Text;
ddrDesgn.DataTextField = GridView1.SelectedRow.Cells[6].Text;
txtEmpAddress.Text = GridView1.SelectedRow.Cells[7].Text;
txtEmpEmailId.Text = GridView1.SelectedRow.Cells[8].Text;
txtEmpContactNo.Text = GridView1.SelectedRow.Cells[9].Text;
txtEmpDOB.Text = GridView1.SelectedRow.Cells[10].Text;
divView.Visible = false;
divForm.Visible = true;
}
它给了我错误:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'MD'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'MD'.
来源错误:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
答案 0 :(得分:0)
当源数据中没有这样的东西时,似乎你的网格认为你试图绑定到一个叫做'MD'的东西,虽然我在你的代码中看不到,试试看Visual Studio,Server Exolorer(Ctrl) -Alt-S)创建与数据库的连接,导航到你的表,确保你在ASP页面上处于GUI模式,然后将表拖到页面上,VS将自动生成SQL和Gridview,从那里你应该能够使用数据源或网格添加/删除列,直到得到你需要的为止。