我使用SQL数据库中的数据填充以下DataGrid
<asp:DataGrid ID="dgUsers" BackColor="#F7F9FD" AutoGenerateColumns="false"
OnItemCommand="dgItemCommand" runat="server" >
<Columns>
<asp:BoundColumn HeaderText="Username" DataField="Username" />
<asp:BoundColumn HeaderText="First Name" DataField="FirstName" />
<asp:BoundColumn HeaderText="Last Name" DataField="LastName" />
<asp:TemplateColumn HeaderText="Details">
<ItemTemplate>
<asp:Button runat="server" Text="Open" CommandName="OpenDetails"
CommandArgument='<%# Eval("Username")%>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
我从数据库中的两个不同的表中获取信息,FirstName和LastName来自一个表,而Username来自另一个表。
cmd = new SqlCommand("storedProcedure", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("Email", email);
cmd.Parameters.AddWithValue("FirstName", first);
cmd.Parameters.AddWithValue("LastName", last);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
dgUsers.DataSource = ds;
dgUsers.DataBind();
我收到的错误是在所选数据源上找不到名为'FirstName'的字段或属性。因此,它似乎正确获取用户名,但不是FirstName或LastName。我有三倍检查我没有拼写错误,我的表中的字段确实是FirstName和LastName,以及我在下面显示的存储过程。有谁看到我在这里做错了什么?提前感谢您的帮助!!
@Email varchar(MAX),
@FirstName varchar(20),
@LastName varchar(35)
AS
SELECT Username
From AgentLogin.dbo.AgentLogin
WHERE AgentNumID IN (SELECT AgentNumID FROM AgentLogin.dbo.AgentDetail WHERE
((CHARINDEX(@FirstName, FirstName) != 0) OR (CHARINDEX(@LastName, LastName)
!= 0))) OR (CHARINDEX(@Email, Email) != 0)
SELECT FirstName, LastName
From AgentLogin.dbo.AgentDetail
WHERE AgentNumID IN (SELECT AgentNumID FROM AgentLogin.dbo.AgentLogin WHERE
(CHARINDEX(@Email, Email) != 0)) OR ((CHARINDEX(@FirstName, FirstName) != 0)
OR (CHARINDEX(@LastName, LastName) != 0))
答案 0 :(得分:1)
我认为你想使用内连接。
SELECT AgentLogin.Username,AgentDetail.FirstName,AgentDetail.LastName 来自订单INNER JOIN AgentLogin ON AgentDetail.AgentNumID = AgentLogin.AgentNumID
在尝试创建存储过程之前,尝试从后面的代码中调用函数中的Select。
protected DataTable getTable()
{
DataTable dt = new DataTable();
StringBuilder cmdText = new StringBuilder();
cmdText.Append("SELECT AgentLogin.Username, AgentDetail.FirstName, AgentDetail.LastName");
cmdText.Append("FROM Orders INNER JOIN");
cmdText.Append("AgentLogin ON AgentDetail.AgentNumID = AgentLogin.AgentNumID ");
return dt;
}