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 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.
/// 
/// 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, 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: , , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home