David_Ford
Member
- Joined
- Apr 23, 2017
- Messages
- 12
- Programming Experience
- 10+
Hi. Trying to teach myself some Blazor coding, and I'm having a generally rough time, with some success here and there. I could really use a hand here.
I wrote one app that was able to talk to the AdventureWorks db, and spit back some addresses on the screen. I used the "foreach" method to loop through.
I'm trying it with another app now, and though I thought I modelled the code off the original, something is wrong. I'm getting a couple of errors I didn't get before:
Severity Code Description Project File Line Suppression State
Error (active) CS1579 foreach statement cannot operate on variables of type 'DataAccessService' because 'DataAccessService' does not contain a public instance or extension definition for 'GetEnumerator' BlazorServerApp C:\Users\DSNoS\source\repos\BlazorServerApp\BlazorServerApp\Pages\FetchData.razor 30
Severity Code Description Project File Line Suppression State
Error (active) CS0120 An object reference is required for the non-static field, method, or property 'DataAccessService.GetPersonList()' BlazorServerApp C:\Users\DSNoS\source\repos\BlazorServerApp\BlazorServerApp\Pages\FetchData.razor 47
Here is the code to my class module. I tried adding GetEnumerator, but evidently either I didn't do it right (likely), or something else is wrong and I'm getting a bogus error message.
===============================================
And here is the code of the module doing the calling:
===============================================
I wrote one app that was able to talk to the AdventureWorks db, and spit back some addresses on the screen. I used the "foreach" method to loop through.
I'm trying it with another app now, and though I thought I modelled the code off the original, something is wrong. I'm getting a couple of errors I didn't get before:
Severity Code Description Project File Line Suppression State
Error (active) CS1579 foreach statement cannot operate on variables of type 'DataAccessService' because 'DataAccessService' does not contain a public instance or extension definition for 'GetEnumerator' BlazorServerApp C:\Users\DSNoS\source\repos\BlazorServerApp\BlazorServerApp\Pages\FetchData.razor 30
Severity Code Description Project File Line Suppression State
Error (active) CS0120 An object reference is required for the non-static field, method, or property 'DataAccessService.GetPersonList()' BlazorServerApp C:\Users\DSNoS\source\repos\BlazorServerApp\BlazorServerApp\Pages\FetchData.razor 47
Here is the code to my class module. I tried adding GetEnumerator, but evidently either I didn't do it right (likely), or something else is wrong and I'm getting a bogus error message.
C#:
using Microsoft.Extensions.Configuration;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
namespace BlazorServerApp.Data
{
public class DataAccessService
{
private readonly IConfiguration config;
public DataAccessService(IConfiguration configuration)
{
config = configuration;
}
private string ConnectionString
{
get
{
string _server = config.GetValue<string>("DbConfig:ServerName");
string _database = config.GetValue<string>("DbConfig:DatabaseName");
//string _username = config.GetValue<string>("DbConfig:UserName");
//string _password = config.GetValue<string>("DbConfig:password");
//return ($"Server={_server};Database={_database};User ID={_username};Password={_password};Trusted_Connection=true;MultipleActiveResultSets=true;");
return ($"Server={_server};Database={_database};Trusted_Connection=true;MultipleActiveResultSets=true;");
}
}
/* IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator) GetEnumerator();
}
public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
public interface IEnumerator
{
bool MoveNext();
object Current { get; }
void Reset();
}
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
*/
public async Task<List<Person>> GetPersonList()
{
List<Person> people = new List<Person>();
Person p;
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT a.[BusinessEntityID],a.[FirstName] ,a.[MiddleName] ,a.[LastName] ,a.[Suffix], a.StateProvinceID, a.[ModifiedDate] , " +
"d.addressline1, d.addressline2, d.city, d.postalcode, d.AddressID, ,d.StateProvinceID , e.[StateProvinceCode], e.[CountryRegionCode]" +
" FROM[AdventureWorks2019].[Person].[Person] a inner join AdventureWorks2019.Person.BusinessEntityAddress c on" +
" a.BusinessEntityID = c.BusinessEntityID inner join AdventureWorks2019.person.Address d on c.addressid = d.addressid" +
"inner join AdventureWorks2019.person.StateProvince e on d.stateprovinceid = e.StateProvinceID", con);
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
p = new Person();
p.BusinessEntityID = Convert.ToInt32(row["BusinessEntityID"]);
p.FirstName = row["FirstName"].ToString();
p.Middle = row["Middle"].ToString();
p.LastName = row["LastName"].ToString();
p.Suffix = row["Suffix"].ToString();
p.StateProvinceID = Convert.ToInt32(row["StateProvinceID"]);
p.AddressID = Convert.ToInt32(row["AddressId"]);
p.AddressLine1 = row["AddressLine1"].ToString();
p.AddressLine2 = row["AddressLine2"].ToString();
p.City = row["City"].ToString();
p.StateProvinceID = Convert.ToInt32(row["StateProvinceID"]);
p.StateProvinceCode = row["StateProvinceCode"].ToString();
p.CountryRegionCode = row["CountryRegionCode"].ToString();
people.Add(p);
}
con.Close();
return await Task.FromResult(people);
}
}
public class Person
{
public int BusinessEntityID { get; set; }
public string? FirstName { get; set; }
public string? Middle { get; set; }
public string? LastName { get; set; }
public string? Suffix { get; set; }
public int AddressID { get; set; }
public string? AddressLine1 { get; set; }
public string? AddressLine2 { get; set; }
public string? City { get; set; }
public int StateProvinceID { get; set; }
public string? StateProvinceCode { get; set; }
public string? CountryRegionCode { get; set; }
}
}
===============================================
And here is the code of the module doing the calling:
===============================================
C#:
@page "/fetchdata"
<PageTitle>People Edit</PageTitle>
@using BlazorServerApp.Data
@inject DataAccessService PeopleService
<h1>People Edit</h1>
<p>This component demonstrates fetching data from a service.</p>
@if (PeopleService == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>FirstName</th>
<th>Middle</th>
<th>LastName</th>
<th>Suffix</th>
</tr>
</thead>
<tbody>
@foreach (var Person in PeopleService)
{
<tr>
<td>@Person.Firstname</td>
<td>@Person.Middle</td>
<td>@Person.LastName</td>
<td>@Person.Suffix</td>
</tr>
}
</tbody>
</table>
}
@code {
private DataAccessService[]? Person;
protected override async Task OnInitializedAsync() => Person = await DataAccessService.GetPersonList();
}
Last edited by a moderator: