user6882159
user6882159

Reputation:

select the number of records to show mvc 5

I am creating a web app in which i have 6000 employees and i want to show all the employees on a table in my web app,

record is very large in number thats why it is taking too much time to show me the data

here is my web service

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public void showrecd()
        {
            List<object> employeedata = new List<object>();
            SqlCommand cmd = new SqlCommand("select * from dbo.employeedetails order by id desc",con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while(dr.Read())
            {
                employeedata.Add(new{
                    id=dr["id"].ToString(),
                    empname=dr["empname"].ToString(),
                    address = dr["address"].ToString(),
                    jobdescrib =dr["jobdescrib"].ToString(),
                    brandname=dr["brandname"].ToString(),
                    location=dr["location"].ToString()
                });
            }
            var json = js.Serialize(employeedata);
            Context.Response.Write("{" + '"' + "info" + '"' + ":" + json + "}");
            con.Close();
        }

from here I am Fetching the data's,

now i want to make work much easier, i have taken a textbox

<input type="text" ng-model="records" ng-change="showrecordsasperthistextbox()"/>

now if if a user enter(500) in the above textbox, only 500 records must be fetched by the webservice and if a user enters(1) only one record should be fetch by the webservice,

what and how i need to do this???

Upvotes: 0

Views: 84

Answers (1)

user6656728
user6656728

Reputation:

let's declare an int

Int number=20;// Declare this as dynamic
SqlCommand cmd = new SqlCommand("select top (cast('" + number + "' as int)) * from dbo.employeedetails order by id",con);

and now your sqlcommand

//If You are getting error in (top('"+number+"')) this command

try this

(cast('"+number+"' as int))

let me know

Upvotes: 1

Related Questions