Dynamically Bind data to jquerymobile listview by passing parameters using c# and sqlserver
Here i am going to explain, How to bind the data Dynamically to jquerymobile listview from sqlserver by passing parameters with csharp. Let's start First, Create a New asp.net website. Next, Add one asmx webservice form from choose items dialog box and change the name as myservice.asmx Next, Open the myservice.asmx.cs page and add the following namespaces.
using System.Collections.Generic; using System.Web.Services; using System.Web.Script.Serialization; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections;Next, Add one class and one Web method in order get the data from database. Following is the complete code.
///Next, Select one new html page and one add div (Questions) inside the body tags/// Summary description for myservice /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [System.Web.Script.Services.ScriptService] public class myservice : System.Web.Services.WebService { SqlConnection con = new SqlConnection(); SqlCommand com = new SqlCommand(); public myservice () { //Uncomment the following line if using designed components //InitializeComponent(); } public class Questions { public string QuestionId { get; set; } public string Question { get; set; } public string CID { get; set; } } [WebMethod] public string GetQuestions(string Category) { // Category = "1"; con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestQuiz"].ToString()); if (con.State != ConnectionState.Open) { con.Open(); } com = new SqlCommand("select * from TestQuestions where CategoryId='" + Category + "' order by QuestionId asc", con); SqlDataAdapter da = new SqlDataAdapter(com); DataTable ds = new DataTable(); da.Fill(ds); JavaScriptSerializer serializer = new JavaScriptSerializer(); List_data = new List (); string jsondata = " { " + "\"AllQuestion\"" + " : "; foreach (DataRow dtRow in ds.Rows) { _data.Add(new Questions() { QuestionId = dtRow["QuestionId"].ToString(), Question = dtRow["Questions"].ToString(), CID = dtRow["Questions"].ToString() }); } string json = serializer.Serialize(_data.ToArray()); jsondata += json + "}"; //write string to file //System.IO.File.WriteAllText(@"F:\Tanisha\Practice\Questions.json", jsondata); return json; } }
Next,Add the following script inside the HEAD tags.
That's it. Happy coding...
Labels: .Net Tutorials, Aspnet, C#, CSharp, JQuery, jquerymobile, json, listview
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home