Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i'm trying to call this method in the client side but how can i do that?
public string codeBehind()
       {
           string htmlStr = "";
           SqlConnection thisConnection = new SqlConnection(dbConnection);
           SqlCommand thisCommand = thisConnection.CreateCommand();
           thisCommand.CommandText = "SELECT * from tableTest";
           thisConnection.Open();
           SqlDataReader reader = thisCommand.ExecuteReader();

           while (reader.Read())
           {
               int id = reader.GetInt32(0);
               string Name = reader.GetString(1);
               string Pass = reader.GetString(2);
               htmlStr += "<tr><td>" + id + "</td><td>" + Name + "</td><td>" + Pass + "</td></tr>";
           }

           thisConnection.Close();
           return htmlStr;
       }



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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" >
    <table width="100%" align="center" cellpadding="2" cellspacing="2" border="0" bgcolor="#EAEAEA">
        <tr align="left" style="background-color:#004080;color:White;" >
            <td> ID </td>                        
            <td> Name </td>            
            <td>Pass</td>                        
        </tr>

    -----> code behind supposed to be here

    </table>
    </form>
</body>
</html>


What I have tried:

i tried call my method using this
<%Response.Write(codeBehind())%>

and or this
<%=codeBehind()%>


but for some reason it can't be detected am i missing something?

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" >
    <table width="100%" align="center" cellpadding="2" cellspacing="2" border="0" bgcolor="#EAEAEA">
        <tr align="left" style="background-color:#004080;color:White;" >
            <td> ID </td>                        
            <td> Name </td>            
            <td>Pass</td>                        
        </tr>

    <%Response.Write(codeBehind())%>

    </table>
    </form>
</body>
</html>
Posted
Updated 2-Oct-19 23:19pm

1 solution

You should just need

<%=codeBehind()%>


if it can't find the codeBehind method then you probably don't have that method on the code-behind class as defined in the "Page" directive at the top, or the "Inherits" attribute doesn't match the namespace and class in that code-behind file.
 
Share this answer
 
v2
Comments
Reden Rodriguez 3-Oct-19 5:56am    
I just created a new web form but it's always like that

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




using System;
using System.Web.UI;

namespace WebApplication3
{
public partial class Test : Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
public string codeBehind()
{
string htmlStr = "";
SqlConnection thisConnection = new SqlConnection(dbConnection);
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT * from tableTest";
thisConnection.Open();
SqlDataReader reader = thisCommand.ExecuteReader();

while (reader.Read())
{
int id = reader.GetInt32(0);
string Name = reader.GetString(1);
string Pass = reader.GetString(2);
htmlStr += "" + id + "" + Name + "" + Pass + "";
}

thisConnection.Close();
return htmlStr;
}
}
}
F-ES Sitecore 3-Oct-19 6:03am    
Is the method being called ok? If you put a breakpoint in it and debug, does the breakpoint get hit? Either the code is running but is returning an empty string, or the code isn't running due to something about your project set-up.
Reden Rodriguez 3-Oct-19 6:20am    
i can't run it because of the error that the method doesn't exist but it exist and the client side can't even detect it, btw i put my experiment code in my buddy's work and it goddamn work and i tried to create a new web form in his work and we got the same error again, and tried it in my past works and just insert the code in the client side and the code behind and it works but for some reason whenever i create a new webform or application it can be detected.
Reden Rodriguez 3-Oct-19 9:05am    
i finally get it now, my visual studio sucks haha, first i commented all my code the lunch the webapplication and i uncomment all my codes and it works hahaha
F-ES Sitecore 3-Oct-19 9:17am    
Glad you got it sorted in the end :)

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