Click here to Skip to main content
15,922,650 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to fetch the data from database and bind to asp gridview by using JSON?
Posted

1 solution

 
Share this answer
 
Comments
RiswanulZaman 12-Feb-14 0:20am    
Thank u for ur reply...
i have done something... but i am facing error on that coding...
This is my code


<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "BindGridViewusingJQuery.aspx/GetUserInfoData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert("Failure : " + response.d);
},
error: function (response) {
alert("Error : " + response.d);
}
});
});

function OnSuccess(response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var users = xml.find("Table");
//create a new row from the last row of gridview
var row = $("[id*=grid] tr:last-child").clone(true);
//remove the lst row created by binding the dummy row from code behind on page load
$("[id*=grid] tr").not($("[id*=grid] tr:first-child")).remove();
var count = 1;
$.each(users, function () {
var users = $(this);
$("td", row).eq(1).html($(this).find("Name").text());
$("td", row).eq(2).html($(this).find("Subject").text());
$("td", row).eq(3).html($(this).find("Description").text());
$("td", row).eq(0).html($(this).find("Id").text());
$("[id*=grid]").append(row);
//define the background stryle of newly created row
if (count == 1 || (count % 2 != 0)) {
$(row).css("background-color", "#ffffff");
}
else {
$(row).css("background-color", "#D2CDCD");
}
count = count + 1;
row = $("[id*=grid] tr:last-child").clone(true);
});
}

</script>


<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" DataKeyNames="Id"
DataMember="Id">
<columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Subject" HeaderText="Subject" />
<asp:BoundField DataField="Description" HeaderText="Description" />




Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then

Call BindDummyRow()

End If

End Sub

Private Sub BindDummyRow()

Try

Dim mTable As New DataTable
mTable.Columns.Add("Name")
mTable.Columns.Add("SubJect")
mTable.Columns.Add("Description")
mTable.Columns.Add("Id")
mTable.Rows.Add()
grid.DataSource = mTable
grid.DataBind()
Catch ex As Exception
MsgBox(ex.ToString)
End Try

End Sub


<webmethod()> _
Public Shared Function GetUserInfoData() As String
Dim mString As String = ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString
Dim ds As New DataSet
Using con As New SqlConnection(mString)
con.Open()
Using cmd As SqlCommand = con.CreateCommand
cmd.CommandType = Data.CommandType.Text
cmd.CommandText = " SELECT * FROM [dbo].[TEMP_User]"
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds, "Table")
Return ds.GetXml()
con.Close()
End Using
End Using
End Function
JoCodes 12-Feb-14 0:32am    
Update the Code in the question so that CP members can notice it and will be to help you. Also , specify what error you are getting.
RiswanulZaman 12-Feb-14 0:56am    
i got the error in script block...
that is Error:undefined

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