I am trying to retrieve data's from SQL server using Json object in Vb.Net.My retrieved values are added to datatable But the added values are not displaying in the aspx page. Why??
My code :
Client-Side :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>View</title>
</head>
<body>
<form id="form1" runat="server">
<table border="0" >
<tr>
<td>
<asp:Label ID= "lblName" runat="server" Text="Name" ></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" runat="server" Text="" /><br />
</td>
</tr>
<tr>
<td>   </td>
</tr>
<tr>
<td colspan ="2" >
<asp:Button ID="btnShow" Text="Show" runat="server" />
</td>
</tr>
</table>
<hr />
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#3AC0F2"
HeaderStyle-ForeColor="White" RowStyle-BackColor="#A1DCF2">
</asp:GridView>
</form>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/json2/0.2/json2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
BindGridView();
});
function BindGridView() {
$("[id*=btnShow]").bind("click", function () {
var user = {};
user.Name = $("[id*=txtName]").val();
$.ajax({
type: "POST",
url: "View.aspx/ViewUser",
data: '{user: ' + JSON.stringify(user) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
response(data.t);
$("#gvUsers").empty();
if (data.d.length > 0) {
$("#gvUsers").append("<tr><th>Username</th><th>Password</th></tr>");
for (var i = 0; i < response.d.length; i++) {
$("#gvUsers").append("<tr><td>" +
response.d[i].Username + "</td> <td>" +
response.d[i].Password + "</td></tr>");
}
}
}
});
return;
});
};
</script>
</body>
</html>
In Code Behind :
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Services
Imports System.Web.Script.Services
Imports System.Collections.Generic
Partial Class View
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindDummyItem()
End If
End Sub
Public Sub BindDummyItem()
Dim dtGetData As New DataTable()
dtGetData.Columns.Add("Username")
dtGetData.Columns.Add("Password")
dtGetData.Rows.Add()
gvUsers.DataSource = dtGetData
gvUsers.DataBind()
End Sub
<WebMethod()> _
<ScriptMethod()> _
Public Shared Sub ViewUser(user As Users)
Dim Detail As New List(Of Users)()
Dim dt As New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT * FROM Users WHERE Username = @Name")
Using sda As New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Name", user.Name)
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
For Each dtRow As DataRow In dt.Rows
Dim DataObj As New Users()
DataObj.Name = dtRow("Username").ToString()
DataObj.Password = dtRow("Password").ToString()
Detail.Add(DataObj)
Next
End Using
End Using
End Using
Return
End Sub
End Class
Public Class Users
Public Property Name() As String
Get
Return _Name
End Get
Set(value As String)
_Name = value
End Set
End Property
Private _Name As String
Public Property Password As String
Get
Return _Password
End Get
Set(Value As String)
_Password = Value
End Set
End Property
Private _Password As String
End Class
When I execute this code the retrieved values are added in Detail list but it does not displaying in the web page. Tell me it is the correct procedure to retrieve the data?? Thanks in advance.
How about this:
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Name", user.Name)
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
gvUsers.DataSource = dt
gvUsers.DataBind()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.