Friday, October 25, 2013

Dynamically Bind data to jquerymobile listview by passing parameters using c# and sqlserver

In this article i would like to explain how to Dynamically Bind data to jquerymobile listview by passing parameters using c# and sqlserver.In my previous jquerymobile articles, i have already explained "2 different ways to bind the data with clientside code and Bind data to listview from database using jquerymobile,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 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.
/// Summary description for myservice
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
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 
   public class Questions
        public string QuestionId { get; set; }
        public string Question { get; set; }
        public string CID { get; set; }
        public string GetQuestions(string Category)
           // Category = "1";
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestQuiz"].ToString());
            if (con.State != ConnectionState.Open)
            com = new SqlCommand("select * from TestQuestions where CategoryId='" + Category + "' order by QuestionId asc", con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataTable ds = new DataTable();

            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, Select one new html page and one add div (Questions) inside the body tags
Next,Add the following script inside the HEAD tags.

That's it. Happy coding...

Labels: , , , , , , ,


At September 16, 2014 at 4:14 PM , Blogger Affity Solutions said...

Thanks for your ideas. You can also find the details on Affity Solutions, at the C Developers. The main object of the Affity Solutions is to provide quality web services and is among the few software development company in Nagpur.


Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home