Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The web design:

HTML
%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

    <title>
    
    <div>
     
    <% foreach (item c in this.item)
        { %>
 
         <a href="detayulke.aspx?idno=<%=c.countryid %>"
         >ulke detayı </a><% } %>
         <table><tbody><tr>        <td>            
         <%=c.countryid%></td>        <td>            
         </td>        <td>            </td>        
         <td>             /></td>        <td>             
         /></td>     </tr>    </tbody></table>
         </div>

The behind code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient; 

public partial class Default6 : System.Web.UI.Page
{
    public List<country> item()
    {
        string sqlStatment = "select  from country";
        string constr = 
        System.Configuration.ConfigurationManager.ConnectionStrings
        ["constr"].ConnectionString;
        using (System.Data.SqlClient.SqlConnection con = 
               new System.Data.SqlClient.SqlConnection(constr))
        {
            using (System.Data.SqlClient.SqlCommand cmd = 
            new System.Data.SqlClient.SqlCommand(sqlStatment, con))
            {
                cmd.Connection.Open();
                System.Data.SqlClient.SqlDataReader reader = 
                       cmd.ExecuteReader();
                List<country> emp = new List<country>();
                while (reader.Read())
                {
                    Country country = new Country();
                    country.countryid = Convert.ToInt32(reader.GetValue(0));
                    country.ulkeismi= reader.GetValue(1).ToString();
                    country.photo= reader.GetValue(2).ToString();
                    emp.Add(country);
                }

                reader.Close();
                cmd.Connection.Close();
                return emp;
            }
        }
    }

    public class Country
    {
        public int countryid { get; set; }
        public string ulkeismi { get; set; }
        public string photo { get; set; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (!this.IsPostBack)
            {
                this.Item= this.item();
            }
        }
    } 
}


What I have tried:

There is a list error, I couldn't find it.
Posted
Updated 2-Oct-23 2:40am
v4
Comments
Richard MacCutchan 29-Sep-23 7:38am    
"There is a list error, I couldn't find it"
Sorry, with a question that vague I doubt that anyone can. Please use the Improve question link above, and add complete details of what is not working.

It's not a list error, it's an SQL syntax error:
string sqlStatment = "select  from country";
Should include a list of the columns you wish to return, or "*" for them all:
string sqlStatment = "select countryid, ulkeismi, photo from country";
Or
string sqlStatment = "select * from country";
(But that is a bad practice and should be avoided)
 
Share this answer
 
Comments
[no name] 29-Sep-23 8:49am    
I corrected the part you mentioned, but the error still persists. VBstudio8 does not recognize the places where the item clause is located. In the list class page load and web designer section, .item in the foreach clause. unknown great teacher
Quote:
ASPX
foreach (item c in this.item)
C#
public List<country> item()
item is a method, but you are trying to access it like a property.

Add the missing parentheses to the method call:
ASPX
foreach (item c in this.item())
 
Share this answer
 
Comments
[no name] 29-Sep-23 10:37am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

public partial class Default6 : System.Web.UI.Page
{


public List<country> item()
{
string sqlStatment = "select * from country";
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlStatment, con))
{
cmd.Connection.Open();
System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
List<country> emp = new List<country>();
while (reader.Read())
{
Country country = new Country();
country.countryid = Convert.ToInt32(reader.GetValue(0));
country.ulkeismi= reader.GetValue(1).ToString();
country.photo= reader.GetValue(2).ToString();
emp.Add(country);
}

reader.Close();
cmd.Connection.Close();
return emp;
}
}
}

public class Country
{
public int countryid { get; set; }
public string ulkeismi { get; set; }
public string photo { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (!this.IsPostBack)
{
this.Item= this.item();
}
}
}
}


----------------------


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>





<title>





<% foreach (item c in this.item())
{ %>





ulke detayı <% } %>
<%=c.countryid%> /> />




-------------

I did what you said .item. I still get errors in the places where there are foreach, the line under the word foreach has been corrected, but the places with .item are in the page load section and foreach, there are no errors in any other places, foreach has been corrected, the .item in foreach. In the back end page load section, it says "but is used like" for the error: this.Item= this.item(); The left side of the equation gives an error, the right is normal
Richard Deeming 29-Sep-23 11:01am    
this.Item= this.item();

You haven't declared a property called Item. The error suggests you have instead written:
this.item = this.item();

which obviously won't work.

Remove the Page_Load method from your code-behind.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900