简体   繁体   中英

Razor page ASP.NET displaying data with a stored procedure

I am trying to dynamically display the results of the stored procedure call when the page is loaded. I am having troubles in the for loop in the Page model. How do I fix this to have it only displayed once for each column?

Also how would I display the picture which is an image in SQL Server that is "0x151C2F0002000000..."

Razor page

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Dynamic Display Sample</title>
    <script></script>
</head>
<body>
    <table>
        @foreach (var SampleObject in Model.SampleObjectCollection)
        {
        <tr>
            <td>@SampleObject.CategoryName</td>
            <td>@SampleObject.Description</td>
            <td>@SampleObject.Picture</td>
        </tr>
        }
    </table>
</body>
</html>

Class

public class Categories
{
    public string CategoryName { get; set; }
    public string Description { get; set; }
    public string Picture { get; set; }
}

PageModel

    private List<Categories> _sampleObjectCollection = new List<Categories>();

    public List<Categories> SampleObjectCollection
    {
        get
        {
            return _sampleObjectCollection;
        }
    }

    public void OnGet()
    {
        SqlConnection MyDataSource = new SqlConnection();
        MyDataSource.ConnectionString = @"Persist Security Info=False;
                                        Database=Northwind;
                                        User ID=***;
                                        Password=***;
                                        server=***;
                                        TrustServerCertificate=true";
        MyDataSource.Open();

        SqlCommand GetCommand = new SqlCommand
        {
            Connection = MyDataSource,
            CommandType = CommandType.StoredProcedure,
            CommandText = "jhong14.GetNorthwindCategories"
        };

        Categories SampleObject;

        SqlDataReader GetDataReader;
        GetDataReader = GetCommand.ExecuteReader();

        if (GetDataReader.HasRows)
        {
            while (GetDataReader.Read())
            {
                for (int Index = 0; Index < GetDataReader.FieldCount; Index++)
                {
                    SampleObject = new Categories
                    {
                        CategoryName = GetDataReader[Index].ToString(),
                        Description = GetDataReader[Index].ToString(),
                        Picture = GetDataReader[Index].ToString()
                    };
                    SampleObjectCollection.Add(SampleObject);
                }
            }
        }

        GetDataReader.Close();
        MyDataSource.Close();
}

SQL Server stored procedure:

CREATE PROCEDURE jhong14.GetNorthwindCategories
AS
BEGIN
    SELECT CategoryName, Description, Picture 
    FROM Categories
    ORDER BY CategoryName DESC
END

enter image description here

you have a bug when you read data, try this

if (GetDataReader.HasRows)
        {
            while (GetDataReader.Read())
            {
               
                    SampleObject = new Categories
                    {
                        CategoryName = GetDataReader[0].ToString(),
                        Description = GetDataReader[1].ToString(),
                        Picture = GetDataReader[2].ToString()
                    };
                    SampleObjectCollection.Add(SampleObject);
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM