The name 'getStudentData' does not exist in the current context

Posted by Cpatil1000 under ASP.NET on 1/3/2017 | Points: 10 | Views : 527 | Status : [Member] | Replies : 2
hi,

I am trying bind database to hmtl. Because i want use bootstrap datatable jquery.
I am getting following error. I found this coding in online.

'The name 'getStudentData' does not exist in the current context'


<head runat="server">
<meta charset="utf-8" />
<title>Employees Data Fill From Database</title>
<meta name="description" content="Creating a Employee table with Twitter Bootstrap. Learn with example of a Employee Table with Twitter Bootstrap.">
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"
rel="stylesheet" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<link href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css" rel="stylesheet" />
<script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<%-- <script type="text/javascript">
$(document).ready(function () {
$('#studentTable').dataTable();
});
</script>--%>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="studentTable" width="100%" align="center" cellpadding="2" cellspacing="2"
border="0" bgcolor="#EAEAEA">
<tr align="left" style="background-color: #004080; color: White;">
<td>
StudentId
</td>
<td>
Name
</td>
<td>
EmailAddress
</td>
<td>
Gender
</td>
</tr>
<%=getStudentData()%>
</table>
</div>
</form>
</body>
private string getStudentData()
{
string data = "";
int incVal;
int Sl;
int datas;
int heading1;
int heading2;

DataTable dt = new DataTable();

dt.Columns.Add("Sl");
dt.Columns.Add("datas");
dt.Columns.Add("heading1");
dt.Columns.Add("heading2");

for (int i = 0; i < 100; i++)
{
dt.Rows.Add(new object[] { i, 123 * i, 4567 * i, 2 * i, });
}

for (incVal = 0; incVal < dt.Rows.Count; incVal++)
{
Sl = Convert.ToInt32(dt.Rows[incVal]["Sl"].ToString());
datas = Convert.ToInt32(dt.Rows[incVal]["datas"].ToString());
heading1 = Convert.ToInt32(dt.Rows[incVal]["heading1"].ToString());
heading2 = Convert.ToInt32(dt.Rows[incVal]["heading2"].ToString());

data += "<tr><td>" + Sl + "</td><td>" + datas + "</td><td>" + heading1 + "</td><td>" + heading2 + "</td></tr>";
}

return data;
}




Responses

Posted by: Sheonarayan on: 1/3/2017 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
It seems like you have not declared getStudentData() function on this page or in the code behind page. If you are declaring this function in code behind page, it's modifiers must be internal, protected or public.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Cpatil1000, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 1/4/2017 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi you need to use the Jquery ajax functionally to call the getStudentData().
Also make sure that getStudentData() should be public and it should be [WebMethod]. In you code, it is private.
I have highlight the changes in bold.

see the below link
http://www.aspdotnet-suresh.com/2012/03/bind-data-and-display-datatable-on-aspx.html

Example:-


<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/BindDatatable",
data: "{}",
dataType: "json",
success: function(data) {
for (var i = 0; i < data.d.length; i++) {
$("#tbDetails").append("<tr><td>" + data.d[i].UserId + "</td><td>" + data.d[i].UserName + "</td><td>" + data.d[i].Location + "</td></tr>");
}
},
error: function(result) {
alert("Error");
}
});
});
</script>

[WebMethod]
public
static UserDetails[] BindDatatable()
{
DataTable dt = new DataTable();
List<UserDetails> details = new List<UserDetails>();
// Test of the code
}


Example in MVC:-

$('#studentTable').DataTable({
"ajax": {
"url": "@Url.Action("list", "devices")",
"type": "POST"
},
"deferRender": true,
"serverSide": true,
"searchDelay": 800,
"autoWidth": true,
"stateSave": true
});



Cpatil1000, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response