JQUERY :-
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/jscript">
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "BindgridusingWebMethod.aspx/BindDatatable",
data: "{}",
dataType: "json",
success: function (data) {
for (var i = 0; i < data.d.length; i++) {
$("#gvDetails").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>
ASPX CODE :-
<asp:Button ID="btnshowgrid" runat="server" Text="Show Gridview" OnClick="btnshowgrid_Click" /> <asp:Button
ID="clear" runat="server" Text="Clear" onclick="clear_Click" />
<asp:GridView ID="gvDetails" runat="server" >
<HeaderStyle BackColor="#474747" Font-Bold="true" ForeColor="White" />
</asp:GridView>
NAMESPACE :-
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Collections.Generic;
CODE BEHIND :-
private void BindColumnToGridview()
{
DataTable dt = new DataTable();
dt.Columns.Add("fullname");
dt.Columns.Add("DOB");
dt.Columns.Add("Gender");
dt.Rows.Add();
gvDetails.DataSource = dt;
gvDetails.DataBind();
gvDetails.Rows[0].Visible = false;
}
[WebMethod]
public static entity[] BindDatatable()
{
DataTable dt = new DataTable();
List<entity> details = new List<entity>();
using (SqlConnection con = new SqlConnection("Data Source=10.1.1.1;Initial Catalog=DailyTaskoskar;Persist Security Info=True;User ID=Username;Password=Password"))
{
using (SqlCommand cmd = new SqlCommand("SELECT FullName,CONVERT(varchar(30),DOB,101) AS DOB,Gender FROM form ", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dtrow in dt.Rows)
{
entity user = new entity();
user.UserId = dtrow["fullname"].ToString();
user.UserName = dtrow["DOB"].ToString();
user.Location = dtrow["Gender"].ToString();
details.Add(user);
}
}
}
return details.ToArray();
}
protected void btnshowgrid_Click(object sender, EventArgs e)
{
BindColumnToGridview();
}
protected void clear_Click(object sender, EventArgs e)
{
gvDetails.DataSource = null;
gvDetails.DataBind();
}
Click on Button :-
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/jscript">
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "BindgridusingWebMethod.aspx/BindDatatable",
data: "{}",
dataType: "json",
success: function (data) {
for (var i = 0; i < data.d.length; i++) {
$("#gvDetails").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>
ASPX CODE :-
<asp:Button ID="btnshowgrid" runat="server" Text="Show Gridview" OnClick="btnshowgrid_Click" /> <asp:Button
ID="clear" runat="server" Text="Clear" onclick="clear_Click" />
<asp:GridView ID="gvDetails" runat="server" >
<HeaderStyle BackColor="#474747" Font-Bold="true" ForeColor="White" />
</asp:GridView>
NAMESPACE :-
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Collections.Generic;
CODE BEHIND :-
private void BindColumnToGridview()
{
DataTable dt = new DataTable();
dt.Columns.Add("fullname");
dt.Columns.Add("DOB");
dt.Columns.Add("Gender");
dt.Rows.Add();
gvDetails.DataSource = dt;
gvDetails.DataBind();
gvDetails.Rows[0].Visible = false;
}
[WebMethod]
public static entity[] BindDatatable()
{
DataTable dt = new DataTable();
List<entity> details = new List<entity>();
using (SqlConnection con = new SqlConnection("Data Source=10.1.1.1;Initial Catalog=DailyTaskoskar;Persist Security Info=True;User ID=Username;Password=Password"))
{
using (SqlCommand cmd = new SqlCommand("SELECT FullName,CONVERT(varchar(30),DOB,101) AS DOB,Gender FROM form ", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dtrow in dt.Rows)
{
entity user = new entity();
user.UserId = dtrow["fullname"].ToString();
user.UserName = dtrow["DOB"].ToString();
user.Location = dtrow["Gender"].ToString();
details.Add(user);
}
}
}
return details.ToArray();
}
protected void btnshowgrid_Click(object sender, EventArgs e)
{
BindColumnToGridview();
}
protected void clear_Click(object sender, EventArgs e)
{
gvDetails.DataSource = null;
gvDetails.DataBind();
}
Click on Button :-
No comments:
Post a Comment