[英]Cant get sql data into listview from a different foreign table (2 tables)
Been stuck on the same problem for days and i have been searching around the web for an answer. 几天被困在同一问题上,我一直在网上寻找答案。
My question is : In the code below i got two tables that has 1 foreignkey. 我的问题是 :在下面的代码中,我得到了两个具有1个Foreignkey的表。 I want to display the nvarchar in that table not the ID. 我想在该表中显示nvarchar而不是ID。
In my aspx page i got a Listview. 在我的aspx页面中,我得到了一个Listview。
<asp:ListView ID="ListView_Fruit" ItemType="DifferentFruit.Model.Fruit"
SelectMethod="ListView_GetData" DataKeyNames="FruitID" runat="server"
DeleteMethod="ListView_Del"
UpdateMethod="ListView_Update" >
<LayoutTemplate>
<table>
<tr>
<th>FruitID</th>
<th>Fruit Name</th>
<th>TypeOfFruitID</th>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Item.FruitID %></td>
<td><%# Item.FruitName %></td>
<td><%# Item.TypeOfFruitID %></td>
</tr>
</ItemTemplate>
Database 数据库
Fruits
FruitID(int) Pk
FruitName nvarchar(20)
FruitsTypeID(int)
FruitsType
FruitsTypeID(int) (fk)
FruitType(nvarchar(20)
Now i display every fruit from a stored procedure. 现在,我将显示存储过程中的所有水果。
Getting all data.
SELECT FruitID, FruitName, TypeOfFruitID
FROM Fruits
Getting all data from FruitsType
SELECT FruitsTypeID, FruitType FROM FruitsType
WHERE FruitsTypeID = CAST(FruitsTypeID as nvarchar(20))
How can i display the data from the FruitsType? 如何显示FruitsType中的数据? Now it only display the FruitsTypeID not the other row in the FruitsType. 现在,它仅显示FruitsTypeID,而不显示FruitsType的另一行。 I want to display FruitType 我想显示FruitType
My fruit Databaselayer 我的水果数据库层
public IEnumerable<Fruits> GettingAllFruits(int startRow, int maxRow, out int totalRows)
{
using (SqlConnection conn = Create())
{
SqlCommand cmd = new SqlCommand("appSchema.app_getFruits", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@PageIndex", startRow / maxRow + 1);
cmd.Parameters.Add("@PageSize", maxRow);
cmd.Parameters.Add("@RecordCount", ParameterDirection.Output)
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
int fruitID = reader.GetOrdinal("FruitID");
int fruitName = reader.GetOrdinal("FruitName");
int fruitType = reader.GetOrdinal("TypeOfFruitID");
while (reader.Read())
{
Fruits fruit = new Fruits();
fruit.fruitID = reader.GetInt32(fruitID);
fruit.fruitName = reader.GetString(fruitName);
fruit.typeOfFruitID = reader.GetInt32(fruitType);
fruitslist.Add(fruits);
}
}
totalRows = (int)cmd.Parameters["@RecordCount"].Value;
fruitlist.TrimExcess();
return fruitlist;
}
}
}
My stored Procedure (Updated) 我的存储过程(已更新)
BEGIN SELECT ROW_NUMBER()
OVER ( ORDER BY
[FruitID] DESC
)AS RowNumber
,[FruitID]
,[FruitName]
,[FruitsTypeID]
INTO #FruitsRes
FROM [Fruits]
INNER JOIN FruitsType ON Fruits.TypeOfFruitID = FruitsType.FruitsTypeID
WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
DROP TABLE #Results
END
The stored procedure is fine but when the stored procedure is called back to the application i getting a convertion error. 存储过程很好,但是当存储过程被调用回应用程序时,出现转换错误。 That says cannot convert from 'Apple' To int. 也就是说,无法从“ Apple”转换为int。 Error here --> 错误在这里->
int fruitType = reader.GetOrdinal("TypeOfFruitID");
You are basically looking for an Inner Join , Try this:- 您基本上是在寻找内部联接 ,请尝试以下操作:-
SELECT FruitID, FruitName, FruitType
FROM Fruits INNER JOIN FruitsType
ON Fruits.TypeOfFruitID = FruitsType.FruitsTypeID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.