Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ASP.NET
web form desıgner  
-------

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="onurty.aspx.cs" Inherits="JOIN_PROJEM.onurty" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>

<% while dr1.read() { %>
  <tr>
    <td> Ürün Adı :</td>
    <td> <%=dr1("urunadi");%> </td>
    <td> <%=dr1("urunlinki");%> </td>
  </tr>
<% } %>
</table>
    </div>
    </form>
</body>
</html>


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.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Text; 
using System.Drawing;
using System.Data.Common;
using System.Data.OleDb;
namespace JOIN_PROJEM
{
    public partial class onurty : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
     
        OleDbConnection connection = new OleDbConnection 
        (ConfigurationManager.ConnectionStrings["constr"].ToString());

            string query = "select * from urunler";
            OleDbCommand dr1= new OleDbCommand(query, connection);
            dr1.ExecuteReader();
        }
    }
} 


What I have tried:

Explanation: I cannot return the database data with the while oledbdatareader or sqldatareader in the web designer form and html table in Visual Studio 2015. The editor says dr1 variable gets the name dr1 does not exist context say error and I'm trying to make the code look like a list like MVC. I don't want to use placeholder dataview buildingstring datalist, etc. I couldn't manage to create a loop on the classic HTML table web designer side. In short, the loop should be on the web designer form side. I couldn't find any examples in movies or examples on the internet and how should the behind and web design side of my code be? I would appreciate it if you could help me. For example, in all code, behind side always has buildingsstring dataview placeholder, etc.
Posted
Updated 19-Sep-23 11:01am
v2

dr1 is a local variable within the Page_Load event handler. It will not be available outside of that method.

https://www.pluralsight.com/guides/understanding-scope-and-visibility-in-c[^]

I'd recommend using a ListView control instead:
ListView Class (System.Web.UI.WebControls) | Microsoft Learn[^]
ASP.NET
<asp:ListView runat="server" DataSourceID="urunlerDataSource">
<LayoutTemplate>
    <table>
    <tr id="itemPlaceholder" runat="server"></tr>
    </table>
</LayoutTemplate>
<ItemTemplate>
    <tr>
        <td> Ürün Adı :</td>
        <td> <%# Eval("urunadi") %> </td>
        <td> <%# Eval("urunlinki") %> </td>
    </tr>
</ItemTemplate>
</asp:ListView>

<asp:SqlDataSource ID="urunlerDataSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:constr %>"
    SelectCommand="SELECT urunadi, urunlinki FROM urunler"
/>
 
Share this answer
 
Comments
[no name] 18-Sep-23 15:59pm    
I do not want to use datalist gridview placeholder buildingstring. The dr1 variable in the behind part of my code should be in a loop in the classic HTML table in the web design form.
Richard Deeming 19-Sep-23 3:19am    
That's a terrible idea, and not how ASP.NET was meant to work.

If you want to persist in writing PHP-style code in ASP.NET, then you need to move the data access code into the markup:
<table>
<% 
using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["constr"].ToString()))
using (OleDbCommand command = new OleDbCommand("SELECT urunadi, urunlinki FROM urunler", connection))
{
    connection.Open();
    using (OleDbDataReader dr = command.ExecuteReader())
    {
        while (dr.Read())
        {
%>
<tr>
    <td> Ürün Adı :</td>
    <td> <%= dr1["urunadi"] %< </td>
    <td> <%= dr1["urunlinki"] %> </td>
</tr>
<%
        }
    }
}
%>


Nobody - including yourself in six months time - will thank you for writing such horrendous spaghetti code.
[no name] 19-Sep-23 5:30am    
Your second example was perfect. It was very descriptive without using datalist building string append datalist gridview, but I wanted the loop to be in the behind code section of the query section and the loop in the web designer section. In short, you gave it all in one place, sir. If the loop loops only in the html table in the web designer section, it will be great, sir. I could not pass the dr1 variable here, vs2015 editor gives an error, I think it is not possible to do this, we cannot separate the web designer and code behind parts of the code, great teacher.
Richard Deeming 19-Sep-23 5:44am    
That's really not how ASP.NET is designed to work.

About the closest you'll get would be:

Code-behind:
public IEnumerable<IDataRecord> ListUrunlers()
{
    using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["constr"].ToString()))
    using (OleDbCommand command = new OleDbCommand("SELECT urunadi, urunlinki FROM urunler", connection))
    {
        connection.Open();
        using (OleDbDataReader dr = command.ExecuteReader())
        {
            while (dr.Read())
            {
                yield return dr;
            }
        }
    }
}
Markup:
<table>
<% foreach (IDataRecord record in ListUrunlers()) { %>
<tr>
    <td> Ürün Adı :</td>
    <td> <%= record["urunadi"] %> </td>
    <td> <%= record["urunlinki"] %> </td>
</tr>
<% } %>
</table>

But you're really fighting against the system to make this work.

If you're going to stick with WebForms, then you should get used to working with controls like the ListView. If you don't like the way they work, then consider switching to a different framework like MVC or Razor Pages.
[no name] 19-Sep-23 5:40am    
The king of the code, your second work on these special signs is excellent, but I wish they were all in one place, the html part and the behind part were separate, my great teacher, this would be a great example on the net, this class series will probably return in html like a loop in web design, but how could I not find it on the internet, my great teacher? gridview append buildind string etc. In the examples, the codes are written in two parts, html web designer and behind code. I wanted this to be in the query behind code side and the report in the html web design part. Best regards
Even though some sites say that it can be done using class level definitions and arrays, they do not give examples. I could not find anything on this subject on the net. Moreover, there is nothing containing square brackets, curly brackets and special signs <%. I do not like elements such as datalist gridview append datalist.
 
Share this answer
 
Comments
[no name] 19-Sep-23 6:58am    
Dear great teacher, believe me, there is no such beautiful explanation on the internet, there is neither a movie nor a transcript. It was a great code explanation, explaining how special signs are used <%, how various bracket types are used in the code, and it is a real way of programming without the need for elements such as datalist buildingstring append on the classical HTML designer side. I am grateful to you for showing me the direction. I understood the importance of knowing these special signs very well. In the light of all this information, could only the classic for loop loop be used instead of foreach in the web designer part? Best regards and was an inumerable structure required? were there any other ways, my great teacher, how else could it be done for the design html code side? without transferring the list with and What is my debt to the site, sir, you have put so much effort into this site, I congratulate you, it is a real forum site.
[no name] 19-Sep-23 7:00am    
I may not be able to repay you, but my great teacher, this site is the greatest site in the world.
[no name] 19-Sep-23 11:00am    
table >
<% foreach (IDataRecord record in ListUrunlers()) { %>
Product Name: <%= record["urunadi"] %> <%= record["productlinki"] %> <% } %>

I implemented the code you wrote, here Idatarecord I get an error, it says this on the web designer side
: CS0246: The type or namespace name 'IDataRecord' could not be found (are you missing a using directive or an assembly reference?) whereas all libraries are installed

usingSystem;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Text;
using System.Drawing;
using System.Data.Common;
using System.Data.OleDb; It is loaded with structures such as system.data and its derivatives, especially the Idataread structure. In the web form designer section, it says type or namespace name 'IDataRecord' could not be found (are you missing a using directive or an assembly reference?) Idatarecord is not qualified for some reason, there is no missing reference, but best regards. Sir, I wonder where I am making a mistake. I need to include the inumarable structure in the load part of the code. Dear sir, the inumarable part is error-free in the back end, for some reason, the vs2015 editor gives this error in the web design section. I owe my respects.
[no name] 19-Sep-23 13:42pm    
Dear sir, I gave you a lot of headache, but unfortunately this idatarecord is not described in the form section. There are many elements that start with idata and are derived from it, but for some reason this is not there. I looked at this issue on the internet and I did not understand why it is missing in this library. Is there this in vs2015? However, I had updated it in vs2015.

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