Reputation: 357
I am populating the following DataGrid with data from my SQL Database
<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>
I am getting the information from two different tables in my database, FirstName and LastName come from one table and Username comes from a different table.
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();
I get the error that A field or property with the name 'FirstName' was not found on the selected data source. So it appears to get Username correctly but not FirstName or LastName. I have tripled checked that I have no typos, the fields in my table are indeed FirstName and LastName, as well as in my stored procedure which I show below. Does anyone see what I could be doing wrong here? Thanks in advance for any help!!
@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))
Upvotes: 1
Views: 3581
Reputation: 56
I think you want to use an inner join.
SELECT AgentLogin.Username, AgentDetail.FirstName, AgentDetail.LastName FROM Orders INNER JOIN AgentLogin ON AgentDetail.AgentNumID = AgentLogin.AgentNumID
Before you try to create a stored procedure try calling the Select in a function from code behind.
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;
}
Upvotes: 1