Hi Got ServerSide Processing for ASP.net C# with sql 2008?

Hi Got ServerSide Processing for ASP.net C# with sql 2008?

RavenRaven Posts: 28Questions: 0Answers: 0
edited January 2012 in General
http://datatables.net/development/server-side/asp_net
Cant a person just translate this to C#? then you got a C# server side for your gallary?

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    If you know C#, then yes - please feel free to translate and I'll post it up.

    Allan
  • RavenRaven Posts: 28Questions: 0Answers: 0
    Lol my translate is gonna be based on the ASP.net version. will give it when im done.
  • RavenRaven Posts: 28Questions: 0Answers: 0
    Hi allan This is as far as i could get There are errors and im still novice but If there is anyone who can help with this will be awesome... I got a NullException happening by
    Request["iDisplayLength"].ToString();
    Its under the "The request is built with a method which allow pagination with sql Server" section
    im showing it the a ////////////ERROR!!!!!!!!!.

    This coding is in my page load behind the page thats using the Datatable repeater
    i got my repeater code like this:
    [code]

    $(document).ready(function () {
    fillOscars();
    tblRepeaterLOOK();
    });
    function fillOscars() {
    $('#tblCustomers').dataTable({
    "bProcessing": true,
    "bServerSide": true,
    "bPaginate": true,
    "sAjaxSource": "server_processing.aspx"
    });
    }
    function tblRepeaterLOOK() {
    $('#tblOscarNominees').dataTable({ "oLanguage": { "sSearch": "Search the nominees:" },
    "iDisplayLength": 10,
    "aaSorting": [[0, "asc"]]
    });
    };

    [/code]
  • RavenRaven Posts: 28Questions: 0Answers: 0
    [code]
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;
    using System.Configuration;
    using System.Data;
    using System.IO;
    using System.Data.SqlClient;
    using jQuery.datatable;

    namespace PractiseJquery
    {
    public partial class Main : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    //Creation of variables
    string str = ""; //will be the output json string
    string strClassement = ""; //Will be the sort column
    string strSens = ""; //ASC or DESC
    string strSensInverse = ""; //DESC or ASC
    int test;
    test = Convert.ToInt32(Request["iSortCol_0"]);
    //Detection of sorted column
    switch (test)
    {
    case 0:
    strClassement = "engine";
    break;
    case 1:
    strClassement = "browser";
    break;
    case 2:
    strClassement = "platform";
    break;
    case 3:
    strClassement = "version";
    break;
    case 4:
    strClassement = "grade";
    break;
    default:
    strClassement = "engine";
    break;
    }
    //detection of direction of the sort
    switch ("sSortDir_0")
    {
    case ("asc"):
    strSens = " ASC";
    strSensInverse = " DESC";
    break;
    case ("desc"):
    strSens = " DESC";
    strSensInverse = " ASC";
    break;
    }
    //SQL connection
    SqlConnection maConnexion = new SqlConnection();
    //string strConnect = string.Format("server=LocalHost; uid=Raven;pwd=Fish; database=TestDB").ToString();
    maConnexion.ConnectionString = ("Data Source = LocalHost; User id=; password=; initial Catalog = DatabaseName;");
    maConnexion.Open();

    DataSet dataset = new DataSet();

    //SQL request creation
    string strRequeteA = ("");
    string strRequeteC = ("");
    string strRequeteB_1 = ("");
    string strRequeteB_2 = ("");
    string strRequeteB_3 = ("");

    //The request is built with a method which allow pagination with sql Server
    //http://Troels.arving.dk/db/rdms
    strRequeteA += "SELECT * FROM (";
    strRequeteA += "SELECT TOP ";
    strRequeteA += Request["iDisplayLength"].ToString();//////////////////////////////ERROR!!!!!!!!!!!!!!
    strRequeteA += " * FROM (";
    strRequeteB_1 += "SELECT ";
    strRequeteB_2 += "TOP " +(Convert.ToInt32(Request["IDisplayStart"]) + Convert.ToInt32(Request["IDisplayLength"])).ToString();
    strRequeteB_3 += " id, engine, browser, platform, version, grade ";
    strRequeteB_3 += "FROM ajax ";

    if (Request["sSearch"].Trim() != string.Empty)
    {
    if (Request["sSearch"].Trim() != string.Empty)
    {

    strRequeteB_3 += "WHERE ";
    string chaine;
    for(int i = 0 ; i < Request["sSearch"].ToString().Split(' ').Length ;i++)
    {
    chaine = Request["sSearch"].ToString().Split(' ').ToString();
    if (chaine != "")
    {
    strRequeteB_3 += "AND (";
    }
    else
    {
    strRequeteB_3 += "(";
    }
    strRequeteB_3 += "engine LIKE '%" + chaine + "%'";
    strRequeteB_3 += " OR ";
    strRequeteB_3 += "browser LIKE '%" + chaine + "%'";
    strRequeteB_3 += " OR ";
    strRequeteB_3 += "platform LIKE '%" + chaine + "%'";
    strRequeteB_3 += " OR ";
    strRequeteB_3 += "version LIKE '%" + chaine + "%'";
    strRequeteB_3 += " OR ";
    strRequeteB_3 += "grade LIKE '%" + chaine + "%'";
    strRequeteB_3 += ") ";
    }
    }
    }
    strRequeteC += " ORDER BY ";
    strRequeteC += strClassement;
    strRequeteC += strSens;

    strRequeteC += ") AS foo ORDER BY ";
    strRequeteC += strClassement;
    strRequeteC += strSens;

    strRequeteC += ") AS bar ORDER BY ";
    strRequeteC += strClassement;
    strRequeteC += strSens;

    //Request execution
    SqlCommand MaCommande = new SqlCommand(strRequeteA + strRequeteB_1 + strRequeteB_2 + strRequeteB_3 + strRequeteC,maConnexion);
    SqlDataAdapter MonAdapteur = new SqlDataAdapter(MaCommande);
    MonAdapteur.Fill(dataset);
    MaCommande.Dispose();

    if (dataset.Tables.Count > 0 )
    {
    DataTable table;
    table = dataset.Tables[0];

    //JSON data writing
    str = "{";
    str += "\"sEcho\": " + Request["sEcho"] + ",";

    //Request Finding the total records number
    strRequeteA = "SELECT COUNT(id) FROM ajax";
    dataset = new DataSet();
    MaCommande = new SqlCommand(strRequeteA, maConnexion);
    MonAdapteur = new SqlDataAdapter(MaCommande);
    MonAdapteur.Fill(dataset);
    MaCommande.Dispose();
    str += "\"iTotalRecords\": " + dataset.Tables[0].Rows[0][0].ToString() + ",";

    str += "\"aaData\": [";

    //Procedure avoiding double records in last page
    int nbRowANePasAfficher = 0;
    if ((Convert.ToInt32(Request["iDisplayStart"]) + Convert.ToInt32(Request["iDisplayLength"])) > Convert.ToInt32(dataset.Tables[0].Rows[0][0]))
    {
    nbRowANePasAfficher = (Convert.ToInt32(Request["iDisplayStart"]) + Convert.ToInt32(Request["iDisplayLength"])) - Convert.ToInt32(dataset.Tables[0].Rows[0][0]);
    }
    if (Convert.ToInt32(Request["iDisplayStart"]) == 0 )
    {
    nbRowANePasAfficher = 0;
    }

    //JSON data writing
    bool autre = false;
    foreach(DataRow row in table.Rows)
    {
    if(nbRowANePasAfficher > 0)
    {
    nbRowANePasAfficher -= 1;
    continue;
    }
    if(autre == true)
    {
    str += ",";
    }
    str += "[";
    str += "\"" + row["engine"].ToString() + "\",";
    str += "\"" + row["browser"].ToString() + "\",";
    str += "\"" + row["platform"].ToString() + "\",";
    str += "\"" + row["version"].ToString() + "\",";
    str += "\"" + row["grade"].ToString() + "\"";
    str += "]";
    autre = true;
    }
    str += "]";
    str += "}";

    Response.Write(str);
    maConnexion.Close();
    }
    }

    }
    }
    [/code]
  • RavenRaven Posts: 28Questions: 0Answers: 0
    So if there is someone who can help with this, will be Amazing
  • RavenRaven Posts: 28Questions: 0Answers: 0
    Hi Allan, i came accross this:
    http://activeengine.wordpress.com/2010/12/19/how-to-create-server-side-paging-for-datatables-net-with-asp-net/

    It might interest you.
    maby you can explain to me some things about server side processing for a bit.
    im already binding data to my repeater, i add jqueryDataTables to my table for more functionality.
    but but from my understanding, server side processing, does the paging and sorting on the server side, not the client side, so does that make my coding, where i bind data to the repeater redundant?
    [code]
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
    Nullable NullInt = null;

    List ListUsers = NursesUserController.SearchNursesUsers(txtUserNames.Text.Trim(), txtUserSurname.Text.Trim(), NullInt, Currentuser.Nurses_ID).ToList();
    if (ListUsers.Count > 0)
    {
    rptUsers.DataSource = ListUsers;
    rptUsers.DataBind();
    }
    else
    {
    rptUsers.DataSource = null;
    rptUsers.DataBind();
    }
    }
    [/code]

    Exactly what are my steps? is it only Datatables plugin i download or are there more to download to make server side work? after all my searching im feeling really lost right now
  • mac2sysmac2sys Posts: 1Questions: 0Answers: 0
    Hi Raven / Allen,

    I am a novice in programming but I tried Datatables.net with C# and it actually is cool. I struggled for beginning. After trying for almost two days I am able to use it with C#. I also faced the Null Exception at Request["iDisplayLength"].ToString(); I just converted it to Int32 using 'Convert.ToInt32()' and used it. I am not sure how helpful this tip is, but Datatables.net is really helpfull. Thanks
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Hi mac2sys - great tip - thanks!

    Allan
This discussion has been closed.